Specific Built-in Types
This section covers numbers, strings, lists, dictionaries, tuples, and files. Compound data types (e.g., lists, dictionaries, tuples) can nest inside each other arbitrarily.
Numbers
This section covers basic number types (integers, floating-point), as well as more advanced types (complex, unlimited-precision long integers).
Constants
Numbers are written in a variety of numeric constant forms:
1234, -24, 0
Normal integers (C longs, at least 32 bits)
99999999L, 42l
Long integers (unlimited size)
1.23, 3.14e-10, 4E210, 4.0e+210
Floating-point (C doubles)
0177, 0x9ff
Octal and hex integer constants
3+4j, 3.0+4.0j, 3J
Complex numbers
Operations
Number types support all number operations (see Table 1-6). In mixed-type expressions, Python converts operands up to the type of the “highest” type, where integer is lower than long, which is lower than floating-point, which is lower than complex.
In 2.2, integer operations are automatically promoted to longs
instead of overflowing, and there are two flavors of division
(/
and //
).
Strings
Strings are immutable (unchangeable) arrays of characters, accessed by offset.
Constants
Strings are written as a series of characters in quotes:
"Python's"
,'Python"s'
Double and single quotes work the same, and each can embed unescaped quotes of the other kind.
"""This is a
multiline block"""
Triple-quoted blocks collect lines into a single string, with end-of-line markers between the original lines.
'Python\'s\n'
Backslash escape code sequences (see ...
Get Python Pocket Reference, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.