Any data that can be referred to in a Python code is considered an object. Objects are used to represent everything from atomic data, such as numbers, to very complex data structures, such as multidimensional arrays, database connections, and documents in several formats.
At the root of the object hierarchy are the numeric data types. These include the following:
Integers: There are three types of integers in Python.
Plain integers: They are represented in the native architecture, which, in most systems, will be either 32 or 64bit signed values.
Long integers: They are integers with unlimited range, subject to available memory. Most of the time, the programmer does not need to be concerned with the distinction between plain and long integers. Python deals with conversions between the types in a transparent way.
Booleans: They represent the values
False
andTrue
. In most situations, they are equivalent to0
and1
, respectively.
Floats: They represent the native doubleprecision floatingpoint numbers.
Complex: They represent complex numbers, represented as a pair of doubleprecision floatingpoint numbers.
The following table has examples of literals (that is, constants) for each data type:
Data type 
Literals 

Integers 

Real numbers (float) 

Complex 

The imaginary unit is represented by j
, but only if it follows a number literal (otherwise, it represents the variable named j
). So, to represent the imaginary unit we must use 1j
and the complex zero is 0j
. The real and imaginary part of a complex number are always stored as doubleprecision floatingpoint values.
Note
Note that the set of numeric types is greatly extended by NumPy
to allow efficient numeric computations.
The assignment statement is used to store values in variables, as follows:
a = 3 b = 2.28 c = 12 d = 1+2j
Python supports multiple simultaneous assignments of values, so the previous four lines of code could be equivalently written in a single line as follows:
a, b, c, d = 3, 2.28, 12, 1+2j
In a multiple assignment, all expressions in the righthand side are evaluated before the assignments are made. For example, a common idiom to exchange the values of two variables is as follows:
v, w = w, v
As an exercise, the reader can try to predict the result of the following statement, given the preceding variable assignments:
a, b, c = a + b, c + d, a * b * c * d print a, b, c, d
The following example shows how to compute the two solutions of a quadratic equation:
a, b, c = 2., 1., 4. x1, x2 = .5 * (b  (b ** 2  4 * a * c) ** 0.5), .5 * (b + (b ** 2  4 * a * c) ** 0.5) print x1, x2
Note that we force the variables a
, b
, and c
to be floatingpoint values by using a decimal point. This is good practice when performing numerical computations. The following table contains a partial list of Python operators:
Operators 
Python operators 

Arithmetic 

Comparison 

Boolean 

Bitwise Boolean 

Bitwise shift 
Note
Care should be taken with the division operator (/
). If the operands are integers, the result of this operation is the integer quotient. For example, 34/12
results 2
. To get the floating point result, we must either enter floating point operands, as in 34./12.
, or add the following statement:
from __future__ import division
The //
operator always represents integer division.
Arithmetic operators follow the rules for the order of operations that may be altered with the use of parenthesis. Comparison operators have lower precedence than arithmetic operators, and the or
, and
, and not
operators have even lower precedence. So, an expression like the following one produces the expected result:
2 + 3 < 5 ** 2 and 4 * 3 != 13
In other words, the preceding command line is parsed as follows:
(((2 + 3) < (5 ** 2)) and ((4 * 3) != 13))
The logical operators and
and or
short circuit, so, for example, the second comparison is never evaluated in the command:
2 < 3 or 4 > 5
The precedence rules for the bitwise and shift operators may not be as intuitive, so it is recommended to always use parenthesis to specify the order of operations, which also adds clarity to the code.
Python also supports augmented assignments. For example, the following command lines first assign the value 5
to a
, and then increment the value of a
by one:
a = 5 a += 1
All Python operators have a corresponding augmented assignment statement. The general semantic for any operator $
is the following statement:
v $= <expression>
The preceding statement is equivalent to the following:
v = v $ (<expression>)