bool
: A boolean value of either True
or False
. Logical operations like and
, or
, not
can be performed on booleans.
x or y # if x is False then y otherwise x
x and y # if x is False then x otherwise y
not x # if x is True then False, otherwise True
In Python 2.x and in Python 3.x, a boolean is also an int
. The bool
type is a subclass of the int
type and True
and False
are its only instances:
issubclass(bool, int) # True
isinstance(True, bool) # True
isinstance(False, bool) # True
If boolean values are used in arithmetic operations, their integer values (1
and 0
for True
and False
) will be used to return an integer result:
True + False == 1 # 1 + 0 == 1
True * True == 1 # 1 * 1 == 1
int
: Integer numbera = 2
b = 100
c = 123456789
d = 38563846326424324
Integers in Python are of arbitrary sizes.
Note: in older versions of Python, a `long` type was available and this was distinct from `int`. The two have been unified.
float
: Floating point number; precision depends on the implementation and system architecture, for CPython the float
datatype corresponds to a C double.a = 2.0
b = 100.e0
c = 123456789.e1
complex
: Complex numbersa = 2 + 1j
b = 100 + 10j
The \\<
, <=
, \\>
and >=
operators will raise a TypeError
exception when any operand is a complex number.
str
: a unicode string. The type of 'hello'
bytes
: a byte string. The type of b'hello'