Py 2
Py 2
the previous indentation to be a multiple of 8 spaces. Since it is common that editors are configured
to show tabs
Citing PEP 8:
When invoking the Python 2 command line interpreter with the -t option, it issues warnings about
code
that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are
highly recommended!
Many editors have "tabs to spaces" configuration. When configuring the editor, one should
differentiate between
The tab character should be configured to show 8 spaces, to match the language semantics - at least
in cases
when (accidental) mixed indentation is possible. Editors can also automatically convert the tab
character to
spaces.
However, it might be helpful to configure the editor so that pressing the Tab key will insert 4 spaces,
Python source code written with a mix of tabs and spaces, or with non-standard number of
indentation spaces can
be made pep8-conformant using autopep8. (A less powerful alternative comes with most Python
installations:
reindent.py)
Built-in Types
Booleans
bool: A boolean value of either True or False. Logical operations like and, or, not can be performed on
booleans.
If boolean values are used in arithmetic operations, their integer values (1 and 0 for True and False)
will be used to
True + False == 1 # 1 + 0 == 1
True * True == 1 # 1 * 1 == 1
Numbers
a=2
b = 100
c = 123456789
d = 38563846326424324
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
a = 2.0
b = 100.e0
c = 123456789.e1
a = 2 + 1j
b = 100 + 10j
The <, <=, > and >= operators will raise a TypeError exception when any operand is a complex
number.
Strings
Python differentiates between ordered sequences and unordered collections (such as set and dict).
a = reversed('hello')
a = (1, 2, 3)
a = [1, 2, 3]
a = {1, 2, 'a'}
a = {1: 'one',
2: 'two'}
method), and can be compared to other objects (it needs an __eq__() method). Hashable objects
which
Built-in constants
In conjunction with the built-in datatypes there are a small number of built-in constants in the built-
in namespace:
Ellipsis or ...: used in core Python3+ anywhere and limited usage in Python2.7+ as part of array
notation.
numpy and related packages use this as a 'include everything' reference in arrays.
NotImplemented: a singleton used to indicate to Python that a special method doesn't support the
specific
a = None # No value will be assigned. Any valid datatype can be assigned later
None doesn't have any natural ordering. Using ordering comparison operators (<, <=, >=, >) isn't
supported anymore
None is always less than any number (None < -32 evaluates to True).
In python, we can check the datatype of an object using the built-in function type.
a = '123'
print(type(a))
b = 123
print(type(b))
i=7
if isinstance(i, int):
i += 1
i = int(i)
i += 1
For information on the differences between type() and isinstance() read: Differences between
isinstance and
type in Python
x = None
if x is None:
For example, '123' is of str type and it can be converted to integer using int function.
a = '123'
b = int(a)
Converting from a float string such as '123.456' can be done using float function.
a = '123.456'
b = float(a)
c = int(a) # ValueError: invalid literal for int() with base 10: '123.456'
d = int(b) # 123
a = 'hello'
r'foo bar': results so called raw string, where escaping special characters is not necessary, everything
is
# bar
An object is called mutable if it can be changed. For example, when you pass a list to some function,
the list can be
changed:
def f(m):
x = [1, 2]
f(x)
An object is called immutable if it cannot be changed in any way. For example, integers are
immutable, since there's
def bar():
x = (1, 2)
g(x)
x == (1, 2) # Will always be True, since no function can change the object (1, 2)
Note that variables themselves are mutable, so we can reassign the variable x, but this does not
change the object
that x had previously pointed to. It only made x point to a new object.
Data types whose instances are mutable are called mutable data types, and similarly for immutable
objects and
datatypes.
str
bytes
tuple
frozenset
bytearray
list
set
dict
There are a number of collection types in Python. While types such as int and str hold a single value,
collection
Lists
The list type is probably the most commonly used collection type in Python. Despite its name, a list is
more like an
array in other languages, mostly JavaScript. In Python, a list is merely an ordered collection of valid
Python values. A