About Python

  • Created in 1991 by Guido van Rossum: a pragmatic take on the ABC language by Geurts, Meertens and Pemberton, which was designed for non-programmers;
  • Open Source and multi-platform: pre-installed in practically any Unix-like system;
  • Object-oriented but not strictly so: some functional programming traits;
  • Strongly typed (few implicit type conversions)
  • Dinamically typed (no type declarations for variables, functions)
  • Current versions are Python 2.7 and 3.5;
  • Minor but significant incompatibilities: there will be no Python 2.8.

Basic syntax

  • no type declarations
  • no semi-colons
  • no braces
  • comments (#) and docstrings

In [8]:
def fibonacci(n):
    """return Nth number in the Fibonacci series"""
    a, b = 0, 1
    while n:
        a, b = b, a + b
        n -= 1
    return a

In [10]:
for n in range(20):
    print(fibonacci(n))


0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181

In [13]:
for i, n in enumerate(range(20)):
    print('%2d -> %4d' % (i, fibonacci(n)))


 0 ->    0
 1 ->    1
 2 ->    1
 3 ->    2
 4 ->    3
 5 ->    5
 6 ->    8
 7 ->   13
 8 ->   21
 9 ->   34
10 ->   55
11 ->   89
12 ->  144
13 ->  233
14 ->  377
15 ->  610
16 ->  987
17 -> 1597
18 -> 2584
19 -> 4181

Control flow

Built-in functions and types

Sequence types

References and mutability

Dicts and sets

Comprehensions

Functions are objects


In [3]:
fibonacci


Out[3]:
<function __main__.fibonacci>

In [4]:
fibonacci.__doc__


Out[4]:
'return Nth number in the Fibonacci series'

In [5]:
help(fibonacci)


Help on function fibonacci in module __main__:

fibonacci(n)
    return Nth number in the Fibonacci series


In [6]:
fibonacci?

Generators

Modules and imports