In programming, you manipulate values using operators. For example, in the expression “1+2”, the addition operator (+) is
operating on the values 1 and 2 to produce the sum, 3. The
Python operators are described in Section 19, “Operators and expressions”, but let's look first at Python's
way of representing values.
Every Python value must have a type. For example, the type
of the whole number 1 is int, short for
“integer.”
Here is a table summarizing most of the commonly-used Python types.
Table 1. Python's common types
| Type name | Values | Examples |
|---|---|---|
int |
Integers in the range [-2147483648, 2147483647].
See Section 7.1, “Type int: Integers”.
|
42, -3,
1000000
|
long |
Integers of any size, limited only by the available
memory. See Section 7.2, “Type long: Extended-precision
integers”.
|
42L,
-3L,
100000000000000L
|
bool |
The two Boolean values True and
False. See Section 7.3, “Type bool: Boolean truth values”.
|
True, False
|
float |
Floating-point numbers; see Section 7.4, “Type float: Floating-point
numbers”.
|
3.14159,
-1.0,
6.0235e23
|
complex |
Complex numbers. If the idea of computing with the
square root of -1 bothers you, just ignore this
type, otherwise see Section 7.5, “Type complex: Imaginary numbers”.
|
(3.2+4.9j),
(0+3.42e-3j)
|
str |
Strings of 8-bit characters; see Section 9, “Type str: Strings of 8-bit
characters”. Strings can be empty: write
such as a string as “""” or “''”.
|
'Sir Robin', "xyz",
"I'd've"
|
unicode |
Strings of 32-bit Unicode characters; see
Section 10, “Type unicode: Strings of 32-bit
characters”.
|
u'Fred',
u'\u03fa'
|
list |
A mutable sequence of values; see Section 11, “Type list: Mutable sequences”.
|
['dot', 'dash'];
[]
|
tuple |
An immutable sequence of values; see
Section 12, “Type tuple: Immutable sequences”.
|
('dot', 'dash');
();
("singleton",)
|
dict |
Use dict values (dictionaries) to
structure data as look-up tables; see Section 16, “Type dict: Dictionaries”.
|
{'go':1, 'stop':2};
{}
|
bytearray |
A mutable sequence of 8-bit bytes;
see Section 14, “The bytearray type”.
|
bytearray('Bletchley')
|
file |
A file being read or written; see Section 17, “Type file: Input and output files”.
|
open('/etc/motd')
|
None |
A special, unique value that may be used where a
value is required but there is no obvious value.
See Section 18, “None: The special placeholder value”.
|
None
|