Book Image

IPython Notebook Essentials

By : Luiz Felipe Martins
Book Image

IPython Notebook Essentials

By: Luiz Felipe Martins

Overview of this book

Table of Contents (15 chapters)
IPython Notebook Essentials
About the Author
About the Reviewers

Basic types, expressions, and variables and their assignment

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 64-bit 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 and True. In most situations, they are equivalent to 0 and 1, respectively.

  • Floats: They represent the native double-precision floating-point numbers.

  • Complex: They represent complex numbers, represented as a pair of double-precision floating-point numbers.

The following table has examples of literals (that is, constants) for each data type:

Data type



0, 2, 4, …, 43882838388

5L, 5l (long integer)

0xFE4 (hexadecimal)

03241 (octal)

Real numbers (float)

5.34, 1.2, 3., 0

1.4e-32 (scientific notation)


1.0+3.4j, 1+2j, 1j, 0j, complex(4.3, 2.5)

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 double-precision floating-point values.


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 right-hand 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 floating-point values by using a decimal point. This is good practice when performing numerical computations. The following table contains a partial list of Python operators:


Python operators


+ (Addition)

- (Subtraction, unary minus)

* (Multiplication)

/ (Division, see the note below the table)

// (Integer division)

% (Remainder)


== (Equal to)

> (Greater than)

< (Less than)

>= (Greater than or equal to)

<= (Less than or equal to)

!= (Not equal to)





Bitwise Boolean

& (AND)

| (OR)

^ (XOR)

~ (NOT)

Bitwise shift

<< (Left shift)

>> (Right shift)


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


Python does not have increment/decrement operators, such as a++ and ++a, as in the C language.

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>)


Note that $ is not a valid Python operator, it is just being used as a placeholder for a generic operator.