Anda di halaman 1dari 59

Table

of Contents
Introduction

1.1

Basics

1.2

The IPython Shell


Data types

1.2.1
1.3

Numbers

1.3.1

Strings

1.3.2

Tuples

1.3.3

Lists

1.3.4

Dictionaries

1.3.5

Type Conversions

1.3.6

Indexing

1.3.7

Input and Output

1.4

Writing to the Screen

1.4.1

String formatting

1.4.2

Reading from the Keyboard

1.4.3

Reading and Writing Files

1.5

Reading text files

1.5.1

Writing text files

1.5.2

Working with directories

1.5.3

Control Flow

1.6

Conditional statements with if

1.6.1

Loops with for

1.6.2

Conditional Loops with while

1.6.3

Advanced Data Structures

1.7

Sorting

1.7.1

[Collection Types]

1.7.2

Functions

1.8

Functions

1.8.1

Decorators

1.8.2

Generators

1.8.3

Short functions

1.8.4

Builtin functions

1.8.5

Structuring Bigger Programs

1.9

Modules and packages

1.9.1

Handling Exceptions

1.9.2

Introspection

1.9.3

Leftovers

1.10

Introduction

Python 3 Beginners Reference


(c) 2016 Dr. Kristian Rother (krother@academis.eu)

Who is this document for?


This document contains brief descriptions and examples of common Python commands. You
can use it to review your knowledge, look up things quickly and accompany a beginners
course on Python 3.
This document is meant to help people learning Python as their first programming language.
Because of that, it contains many commands and expressions a seasoned programmer
might write differently. But I consider them nevertheless helpful to learn programming.
This document is not intended as an in-depth guide. Please refer to www.python.org/doc for
more detailed information.

Acknowledgements
This e-book contains many direct and indirect contributions by Allegra Via, Kaja Milanowska,
Anna Philips, Magdalena Rother and Tomasz Puton.

License
Distributed under the conditions of the Creative Commons Attribution Share-alike License
4.0
Sources of this document can be found on https://www.gitbook.com/book/krother/python-3reference.
Contributions welcome!

Basics

Basics
Python programs
A Python program is simply a text file that contains Python statements. The Python
interpreter reads the file and executes the statements line by line.
All program files should have the extension .py
Only one command per line is allowed.

Developing programs on Unix


When developing on Unix, the first line in each Python program should be:
#!/usr/bin env python3

Variables
Variables are 'named containers' used to store values within Python. Variable names may
be composed of letters, underscores and, after the first position, also digits. Lowercase
letters are common, uppercase letters are usually used for constants like PI .
Variables can be used for calculating in place of the values they contain.

Variable assignments
The operator = is used in Python to define a variable. A Python statement containing an
= is called a variable assignment. The value on the right side of the equal sign will be

stored in a variable with the name on the left side.

Changing variables
You may assign to the same variable name twice:

Basics

In [1]: emily = 25952


In [2]: emily = 222
In [3]: emily
Out[3]: 222

In this case, the first value is overwritten by the second assignment. There is no way to
obtain it afterwards.

Python Statements
The lines you are writing all the time are also called Python Statements. A Statement is the
smallest executable piece of code.
So far, you have seen at least three different kinds of statements:
Calculate a number
Put a number into a variable
Print the number from a variable on the screen

Reserved words
Some words like import , for and in are called reserved words. They have a special
meaning in Python, which means that you cannont call a variable for or in . There are 33
reserved words in Python 3.
You can see the complete list with the commands:
import keyword
keyword.kwlist

Code blocks and indentation


In Python, blocks of code are defined by indentation. They occur usually after a control
flow statement ( for , if ) or a structural element (e.g. a function). A code block contains
one or more line that belong to it. Python recognizes this code block by indentation,
meaning that each line starts with four extra spaces. All indented code blocks start with a
colon ( : ) at the end of the line.
Indentation is a central element of Python syntax. Indentation must not be used for
decorative purposes.

Basics

Comments
Comments are lines that are not executed. They allow you to document your code to make it
easier to read. Also, you can temporarily disable lines of code. There are different ways to
write comments:
# this is a one-line comment
"""This is also a one-line comment"""
"""
With triple quotes comments can
stretch over multiple lines.
"""
'''triple single quotes work like triple double quotes.'''

The IPython Shell

The IPython shell


The IPython shell allows you to enter Python commands one by one and executes them
promptly. The IPython shell is a luxury version of the simpler Python shell. It is also called
interactive mode or interactive Python command line.
You can use any Python command from the IPython Shell:
In [1]: 1 + 1
Out[1]: 2
In [2]: 4 * 16
Out[2]: 64
In [3]:

Results of each command are automatically printed to the screen.

How to leave the IPython shell?


You can leave the command line by Ctrl-z (Windows) or Ctrl-d (Linux).
If a program seems to be stuck, you can interrupt the shell with Ctrl-c.

Magic Methods
The IPython shell has a number of special commands or Magic Methods. Here are the
most common ones:
%run - execute a program
%paste - paste and execute code from the clipboard
%hist - show recently entered commands
ls - show files in the current directory
pwd - print the current working directory
cd <dir> - change the working directory to <dir>

Data types

Overview of Data types in Python


Immutable and mutable data types
In Python there are basic and composite data types. The values of basic data types cannot
be changed, they are immutable. Most of the composite data types are mutable.
The immutable data types in Python are:
Boolean ( True / False )
Integer ( 0 , 1 , -3 )
Float ( 1.0 , -0.3 , 1.2345 )
Strings ( 'apple' , "banana" ) - both single and double quotes are valid
None (aka an empty variable)
Tuples (multiple values in parentheses, e.g. ('Jack', 'Smith', 1990) )
The mutable data types are
List [1, 2, 2, 3]
Dictionary {'name': 'John Smith', 'year': 1990}
Set ()

Numbers

Numbers
Integer numbers
Numerical values without decimal places are called integers or ints. In Python, integers are
a predefined data type.
>>> a = 42

Floating-point numbers
Numbers with decimal places are called floating-point numbers or floats.
>>> b = 42.0
>>> pi = 3.14159

Arithmetical Operators
The arithmetical symbols like + - * / connecting two numbers are called operators. In
addition to the standard arithmetics a few other operators are available:
a = 7
b = 4
c = a - b
d = a * b
e = a / b
f = a % b # modulo, 3
g = a ** 2 # 49
h = 7.0 // b # floor division, 1.0

If you perform arithmetics with integer numbers, the result is also an integer. If one of the
numbers is a float, the result will also be a float. When you perform a division, the result is
always a floating-point number.

10

Strings

String
Text values are called strings. In Python, strings are defined by single quotes, double
quotes or three quotes of either. The following string assignments are equivalent:
first = 'Emily'
first = "Emily"
first = '''Emily'''
first = """Emily"""

Special characters
Some characters in Python require special attention:
Newline characters - writing \n into a string stands for the beginning of a new line.
Tabulators - writing \t into a string stands for a tabulator.
Backslashes - because the backslash ( \ ) has a special meaning in the above, you
need to write a double backslash ( \\ ) to represent a single backslash.
Language-specific characters - Python 3 stores Unicode characters including
German Umlauts, Chinese and Arab alphabets by default. However, they may be
interpreted in the same way in different environments. Just be a bit careful when using
them.

String concatenation
The operator + also works for strings, only that it concatenates the strings. It does not
matter whether you write the strings as variables or as explicit values.
With first = 'Emily' and last = 'Smith' the following three statements have the same
result:
name = first + last
name = first + "Smith"
name = "Emily" + "Smith"

Accessing single characters

11

Strings

Using square brackets, any character of a string can be accessed. This is called indexing.
The first character has the index [0] , the second [1] and the fourth has the index [3] .
name[0]
name[3]

With negative numbers, you can access single characters from the end, the index [-1]
being the last, [-2] the second last character and so on:
name[-1]
name[-2]

Note that none of these modify the contents of the string variable.

Creating substrings
Substrings can be formed by applying square brackets with two numbers inside separated
by a colon (slices). The second number is not included in the substring itself.
name = 'Emily Smith'
name[0:5]
name[1:4]
name[6:11]
name[:3]
name[-4:]

String methods
Every string in Python brings a list of functions to work with it. As the functions are contained
within the string they are also called methods. They are used by adding the . to the string
variable followed by the method name.
Below you find a few of the available methods:

Changing case
name = 'Manipulating Strings \n'
name.upper()
name.lower()

12

Strings

Removing whitespace at both ends


name.strip()

Cutting a string into columns


name.split(' ')

Searching for substrings


name.find('ing')

The method returns the start index of the match. The result -1 means that no match has
been found.

Replacing substrings
name.replace('Strings','text')

Removing whitespace at both ends


name.startswith('Man')
name.endswith('ings')

13

Tuples

Tuples
A tuple is a sequence of elements that cannot be modified. They are useful to group
elements of different type.
person = ('Emily', 'Smith', 23)

In contrast to lists, tuples can also be used as keys in dictionaries.

Indexing tuples
Elements of tuples can be indexed in the same way as lists:
person[0] person[-2] person[1:]

Iterating over tuples


You can run a for loop over a tuple:
for elem in person:
print(elem)

Packing and unpacking tuples


Enumerating multiple values separated by a comma implictly creates tuples:
person = 'Emily', 'Smith', 23

Tuples can be unpacked to multiple variables:


first, last, age = person

It is even possible to swap the value of variables that way:


first, last = last, first

14

Tuples

15

Lists

Lists
A list is a Python data type representing a sequence of elements. You can have lists of
strings:
names = ['Hannah', 'Emily', 'Madiosn', 'Ashley', 'Sarah']

and also lists of numbers:


numbers = [25952, 23073, 19967, 17994, 17687]

Accessing elements of lists


Using square brackets, any element of a list and tuple can be accessed. The first character
has the index 0.
print(names[0])
print(numbers[3])

Negative indices start counting from the last character.


print(names[-1])

Creating lists from other lists:


Lists can be sliced by applying square brackets in the same way as strings.
names = ['Hannah', 'Emily', 'Sarah', 'Maria']
names[1:3]
names[0:2]
names[:3]
names[-2:]

Copying a list
You can use slicing to create a copy:

16

Lists

girls = names[:]

Adding elements to a list


Add a new element to the end of the list:
names.append('Marilyn')

Removing elements from a list


Remove an element at a given index:
names.remove(3)

Remove the last element:


names.pop()

Replacing elements of a list


You can replace individual elements of a list by using an index in an assignment operation:
names[4] = 'Jessica'

Sorting a list
names.sort()

Counting elements
names = ['Hannah', 'Emily', 'Sarah', 'Emily', 'Maria']
names.count('Emily')

17

Lists

18

Dictionaries

Dictionaries
Dictionaries are an unordered, associative array. They have a set of key/value pairs. They
are very versatile data structures, but more difficult to use than lists if you are new to Python.
As the name implies, dictionaries are good for looking up things, or searching in general.

Creating dictionaries
Python dictionaries are defined with winged brackets. On the left side of each entry is the
key, on the right side the value:
ratios = {
'Alice': 0.75,
'Bob': 0.55,
'Charlie': 0.80
}

Accessing elements in dictionaries


By using square brackets and a key, you can retrieve the values from a dictionary. At least if
the key is present:
ratios['Alice'] # 0.75
ratios['Ewing'] # KeyError!

Retrieving values in a fail-safe way:


With the get() method you can assign an alternative value if the key was not found.
ratios.get('Alice')
ratios.get('Ewing', 'sorry not found')

Changing values in a dictionary


The contents of a dictionary can be modified. For instance if you start with an empty
dictionary:
19

Dictionaries

persons = {}

Now you can add values one key/value pair at a time:


persons['Emily'] = 1977

Setting values only if they dont exist yet:


persons.setdefault('Alice', 1980)
persons.setdefault('Emily', 1898)
# for 'Emily', nothing happens

Getting all keys or values:


ratios.keys()
ratios.values()
ratios.items()

Checking whether a key exists


The in operators checks whether a key exists in the dictionary.
if 'Bob' in ratios:
print('found it')

Note that you can use in for the same with a list as well. The dictionary is much faster!

Loops over a dictionary


You can access the keys of a dictionary in a for loop.
for name in ratios:
print(name)

However, there is no stable order unless you sort the keys explicitly:

20

Dictionaries

for name in sorted(ratios):


print(name)

What data can I use as keys?


Valid types for keys are strings, integers, floats, and tuples. You may mix keys of different
type in one dictionary. However, mutable data types such as lists and other dictionaries are
not allowed as keys.

21

Type Conversions

Type conversions
Converting numbers to strings
If you have an integer or float number i , you can make a string out of it with str(i) :
text = str(2000)

Alternatively, you can use a format string:


text = "{}".format(2000)"

Format strings pay off very quickly when converting floats:


text = "{:4.2f}".format(3.14159)

Converting strings to numbers


If you have a string s , you can make an integer out of it with int(s) :
number = int("2000")

The same works for a float:


number = float("3.14159")

Other type conversions


The functions int() , float() and str() change the type of the given data. They are
therefore called type conversions. There is a conversion functions for each Python data
type. Try the following:

22

Type Conversions

int('5.5')
float(5)
str(5.5)
list("ABC")
tuple([1, 2, 3])
dict([('A', 1), ('B', 2)])
set([1, 2, 2, 3])

23

Indexing

Indexing
Computers and people count differently.

Computers treat an address in memory as the starting point of a body of data. In the same
sense, an index in Python always refers to such a starting point, something that is in
between two objects in memory. We humans in contrast always count the objects
themselves.
This is why the indices used to slice list are a bit intuitive at first, e.g. in:
>>> s = "my fat cat"
>>> s[3:6]
'fat'

The above diagram provides a pracitcal model by which you can deduce indices yourself.

24

Writing to the Screen

Writing to the Screen


The command print() Writes textual output to the screen. It accepts one or more
arguments in parentheses - all things that will be printed. You can print both strings and
integer numbers. You can also provide variables as arguments to print() .
We need print because typing a variable name in a Python program does not give you any
visible output.
The Python print statement is very versatile and accepts many combinations of strings,
numbers, function calls, and arithmetic operations separated by commas.

Examples for print statements:


print('Hello World')
print(3 + 4)
print(3.4)
print("""Text that
stretches over
multiple lines.
""")
print('number', 77)
print()
a = "7"
print(a * 7)
print(int(a) * 7)
print("Hello", end=" ")
print("World")

25

String formatting

String formatting
Variables and strings can be combined, using formatting characters. This works also within a
print statement. In both cases, the number of values and formatting characters must be
equal. Here are some examples:
print('Hello {}%!'.format('Roger'))
print('Result: {4i}'.format(42))
print('{1:5s} {1:>5s}'.format('one', 'two'))
print('{2} {1}'.format('one', 'two'))
a = 5.099999
b = 2.333333
print('{:6.3f}/{:6.3f}'.format(a, b))

The winged brackets are placeholders for the parameters in the format function. They may
contain special instructions for formatting, consisting of two parts {a:b} . Part a . Both
parts are optional. Options include:
{:xd} an integer with x digits.
{:<xd} a left-formatted integer with x digits.
{x} the x-th parameter.
{:>5s} a right-aligned string of width 5.
{:6.2f} a float number with 6 digits (2 after the dot).

26

Reading from the Keyboard

Reading text from the keyboard


The input function
Text can be read from the keyboard with the input function. input works with and without
a message text. The value returned is always a string:
a = input()
b = input('Please enter a number')

Although the input command is rarely seen in big programs, it often helps to write small,
understandable programs, especially while learning Python.

27

Reading text files

Reading text files


Opening a file for reading
Text files can be accessed using the open() function. open() gives you a file object that
you can use in a for loop. The loop processes the contents of the file line by line:
f = open('my_file.txt')
for line in f:
print(line)

Alternatively, you can write both commands in one line:


for line in open('my_file.txt'):
print(line)

Reading an entire file to a string variable.


You can read the entire content of a file to a single string:
f = open('my_file.txt')
text = f.read()

Reading a table with multiple columns


Frequently you have data in text files separated into multiple columns, like this:
Emily;Smith;23
Charlie;Parker;45

This file can be read with the following simple pattern:


for line in open('my_file.txt'):
columns = line.strip().split(';')
first = columns[0]
last = columns[1]
age = int(columns[2])

28

Reading text files

This pattern goes through a file line by line (the for line). It then chops off the newline
character from each line ( strip ) and finally breaks the line into a list of items ( split ).

The str.strip() function


With the string function strip() , you chop off whitespace characters (spaces, newline
characters and tabs) from both ends of a string. The code:
text = " this is text "
s = text.split()

produces:
print(s)
"this is text"

The str.split() function


With the string function split(x) , you can divide a string into a list of strings. x is the
character at which you want to separate the columns (by default it splits on whitespace).
s = "this is text"
t = s.split(" ")
print(t)
["this", "is", "text"]

Both strip() and split() perfectly complement each other.


More sophisticated ways to read tabular information can be found in the Python modules
csv and pandas .

Writing file and directory names


When opening files, you often need to specify a directory name as well. You can use both
full or relative directory names. A full file name contains the entire path from the root
directory, e.g.:
C:/Python3/python.exe

29

Reading text files

A relative directory name starts from the current working directory, often the directory in
which your program is started:
data/my_file.txt

or go one directory level up, then move into the folder below:
../data/my_file.txt

On Windows, getting directory names right is a bit cumbersome, because the directory
names easily become long easily. Note that you can use forward slashed to separate
between directories. If you use the backslash \ , you need to write a double backslash \\
(because \ is also used for escape sequences like \n and \t ).
f = open('..\\my_file.txt')
f = open('C:\\Python\\my_file.txt')

Closing files
Closing files in Python is not mandatory but good practice. If you open too many files at the
same time this can be a problem.
f.close()

30

Writing text files

Writing text files


Opening a file for writing
Writing text to files is very similar to reading. One main difference is that you need to add the
'w' parameter for writing.

f = open('my_file.txt','w')
f.write(text)
f.close()

Writing a list of strings


If your data is a list of strings, it can be written to a file in one line of code. You only need to
take care of adding line breaks at the end of each line:
lines = ['first line\n', 'second line\n']
open('my_file.txt','w').writelines(lines)

Writing a table to a text file


A straightforward pattern to write multiple columns to a file uses a for loop to create lines
with separators and newline characters:
names = ['Emily', 'Bob', 'Charlie']
ages = [23, 45, 67]
f = open('my_file.txt', 'w')
for name, age in zip(names, ages):
line = "{};{}\n".format(name, age)
f.write(line)
f.close()

Like with reading, the csv and pandas offer more sophisticated ways to write tables.

Appending to a file
31

Writing text files

It is possible to append text to an existing file, too.


f = open('my_file.txt','a')
f.write('line appended at the end')
f.close()

Closing a file after writing


When writing data, Python buffers the data and writes it to the disk with a delay. The writing
occurs after the file has been closed, when Python ends or when the buffer runs full. By
using close() you make sure the data gets written.
f.close()

32

Working with directories

Working with directories


Importing the os module
Here, we will for the first time use a function that is not readily available in Python - it needs
to be imported:
import os

os is the name of a module that is automatically installed with Python. It is simply not kept

in memory all the time. This is why we need to import it.

Listing files in a directory:


The function
y = os.listdir("my_folder")

gives you a list of all files in the directory my_folder and stores it in the variable y .

Changing directories
With the os module, you can change the current directory:
import os
os.chdir(''..\\python'')

Check whether a file exists


print(os.path.exists('my_file.txt'))

Check file modification time

33

Working with directories

import time
t = os.path.getmtime('/home/krother/.bashrc')
gmt = time.gmtime(t)
time.strftime("%Y-%m-%d, %H:%M:%S", gmt)

Overview
The table lists some frequently used functions in os :
function

description

os.listdir(path)

returns list of file names in path

os.remove(path)

removes a file

os.getcwd()

returns current working directory

os.path.exists(path)

checks whether the given file or directory exists

os.path.isdir(path)

checks whether the given path is a directory

os.path.isfile(path)

checks whether the given path is a file

os.path.getsize(path)

returns file size

os.path.getmtime(path)

returns modification time

os.path.split(path)

cuts off the last dir/file name

os.path.join(d1, d2, d3, ..)

connects names by path separator

os.environ[key]

dictionary of environment variables

os.system(cmd)

executes shell command

34

Conditional statements with if

Conditional statements with if


The if statement is used to implement decisions and branching in a program. One or
more instructions are only executed if a condition matches:
if name == 'Emily':
studies = 'Physics'

There must be an if block, zero or more elif 's and an optional else block:
if name == 'Emily':
studies = 'Physics'
elif name == 'Maria':
studies = 'Computer Science'
elif name == 'Sarah':
studies = 'Archaeology'
else:
studies = '-- not registered yet --'

Code blocks
After an if statement, all indented commands are treated as a code block, and are
executed in the context of the condition.
The next unindented command is executed in any case.

Comparison operators
An if expression may contain comparison operators like:
a == b
a != b
a < b
a > b
a <= b
a >= b

On lists and strings you can also use:

35

Conditional statements with if

a in b

Multiple expressions can be combined with boolean logic ( and , or , not ):


a or b
a and b
not a
(a or b) and not (c or d)

Boolean value of variables


Each variable can be interpreted as a boolean ( True / False ) value. All values are treated
as True , except for:
False
0
[]
''
{}
set()
None

36

Loops with for

Loops with for


The for loop allows you to repeat one or more instructions. To write a for loop you need
two things: First a sequence-like object (e.g. a list, a string, a dictionary or the output of a
function producing sequences). Second, a variable that takes different values as the loop
iterates over the sequence. The variable is easier, because it doesn't matter what name you
give it.
Loops with for are useful whenever you want to repeat instructions a known number of
times, or when you wnat to do something for all elements of a sequence. Below you find
some examples.

Loops executing a given number of times


With the range() function, you can set the number of iterations easily:
for i in range(7):
print(i)

or using an interval:
for i in range(10, 17):
print(i)

or backwards:
for i in range(17, 10, -1):
print(i)

Loops over a string


With a string as the sequence, you obtain single characters in each iteration.
for char in 'ABCD':
print(char)

37

Loops with for

Loops over a list


A list iterates simply once through each element:
for elem in [1, 22, 333, 4444, 55555]:
print(elem)

Loops over a dictionary


With a dictionary, the for loop iterates over the keys. Note that the dictionary is inherently
unordered. Theoretically, you could get the keys in a different order each time.
pairs = {'Alice': 'Bob', 'Ada': 'Charlie', 'Visual': 'Basic'}
for key in pairs:
print(key)
print(pairs[key])

Indented block
All indented commands after the colon are executed within a for loop. The first unindented
command is executed after the loop finishes.
for i in range(5):
print('inside')
print('also inside')
print('outside')

38

Conditional Loops with while

Conditional loops with while


Another control flow statement is the conditional loop with while . While loops combine
the properties for and if . The loop is executed as long as the conditional expression at
the beginning holds. The conditional expressions work in exactly the same way as in if
statements.

Counting until a certain value


A simple usage of while is to count until an exit condition is met. The following loop
calculates the sum of all numbers from 1 through 10:
i = 0
total = 0
while i < 10:
print(i)
i = i + 1
total = total + i

Searching through data


With a while loop you can perform search operations - although many times the methods
on lists and dictionaries will give you a shortcut.
The following loop finds the first name starting with an 'E' :
data = ['Alice', 'Bob', 'Charlie', 'Emily', 'Fred']
i = 0
while i < len(data) and not data[i].startswith('E'):
i += 1
print(i)

Waiting for user input


A while loop is also useful to let a user stop the program:

39

Conditional Loops with while

number = 0
while input('press [Enter] to continue or [x] to exit') != 'x':
number = number +1
print(number)

Endless loops
With while it is possible to build loops that never stop. Most of the time this happens by
accident. In the following loop, the instruction to decrease a is missing. It runs endlessly:
a = 10
b = 1
while a > 0:
b = 1 - b
print(b)
# a = a - 1

40

Sorting

Sorting
Sorting a list
Let us consider sorting the following data:
cities = [
['Berlin', 1977],
['Helsinki', 1983],
['Warsaw', 2006],
['Poznan', 2008],
['Berlin', 2011]
]

There are two ways to sort a Python list. First, you can sort the values in-place, that means
the list gets changed in the process:
cities.sort()

Second, the sorted() function returns an iterator without modifying the initial list:
for city, year in sorted(cities):
print(city, year)

Sorting by a specific column


We can sort by the second column (index 1) with the itemgetter module:
from operator import itemgetter
column = 1
cities.sort(key=itemgetter(column))
for row in interfaces:
print(row)

Sorting by two columns:

41

Sorting

When we give a tuple of indices, we can sort first by one, then the other column:
cities.sort(key=itemgetter((1, 0)))

42

Functions

Functions
What is a function?
A function is an autonomous sub-program with local variables, input and output. Functions
help you divide a program into smaller logical portions. It is much easier to write, reuse and
debug a program consisting of many small functions than a single huge blob of code.
In Python, a function definition must be followed by a code block:
def calc_price(fruit, n):
'''Returns the price of fruits.'''
if fruit == 'banana':
return 0.75 * n
print(calc_price('banana', 10))

Obligatory and optional parameters


Each function can have obligatory parameters (that must be given when calling the function
and optional parameters that have default values. The following function can be called in
two ways:
def calc_price(fruit, n=1):
'''Returns the price of fruits.'''
if fruit == 'banana':
return 0.75 * n
print(calc_prices('banana'))
print(calc_prices('banana', 100))

Warning
Do not use mutable default values (e.g. lists and dictionaries) in the function header. They
make a mess!

List and keyword parameters

43

Functions

For even greater flexibility with function parameters you can also add a list *args for an
unspecified number of extra parameters, or a dictionary **kwargs for keyword parameters.
def func(*args, **kwargs):
print(args[1])
print(kwargs['a'])
func(1, 2, a=3, b=4)

This example may be a bit hard to digest, but these kinds of parameters are used widely.

Return values
A function may return values to the program part that called it.
return 0.75

Multiple values are returned as a tuple.


return 'banana', 0.75

In any case, the return statement ends the execution of a function. Nothing speaks against
having multiple return statements in a function (although for clarity it should not be too
many).

44

Decorators

Decorators
In general, decorators are functions that manipulate functions. More specifically, a
decorator wraps a function to manage a function call.
Imagine you have the following Python function:
def addition(a, b):
do_something_before()
result = a + b
do_something_after()
return result

The same can be written as a Python decorator:


def deco(func):
def wrapper(*args):
do_something_before()
result = func(*args)
do_something_after()
return result
return wrapper
@deco
def addition(a, b):
return a + b

You can argue that this does not simplify the code in the first place. Using decorators pays
off in bigger programs, when they are used often, or imported from different modules.

The functools Module


The functools module contains a couple of ways to manipulate functions.
The wraps decorator copies documentation strings, so that the decorated function looks
like the original one. It is useful when writing your own decorators
import functools
def deco(func):
@functools.wraps(func)
def wrapper(*args):
...

45

Decorators

The lru_cache decorator caches results, so that the second call with the same parameters
is faster. It is useful when your function is fully deterministic.
@functools.lru_cache(maxsize=128, typed=False)

The partial fills in a part of the parameters, resulting in a new function:


add5 = functools.partial(addition, 5)
print(add5(8)) # results in 13

46

Generators

Generators
Generators are "lazy functions". They produce results like normal Python functions, but only
when they are needed. The main purpose of using generators is to save memory and
calculation time when processing big datasets.
A Python generator is indicated by the yield keyword. You cannot have yield and
return in the same function. An example:

def count():
i = 0
print('checkpoint A')
while i < 20:
yield i
i += 1
gen = count()
print('checkpoint B')
print(next(gen))
print(next(gen))

The first call of the generator does nothing yet. Only when next() requests the next value,
the generator function is executed until the yield statement. Then it pauses until the next
yield and so on.

You can use generators in a for loop:


for x in gen:
print(x,)

Iterators
The thing returned by a generator is called an iterator. Many functions in Python 3 return
iterators (e.g. range() , enumerate() , zip() ).
Among the things you can do to iterators are:
request values with next .
use them in a for loop.
convert them to lists with list() .

47

Generators

48

Builtin functions

Builtin functions
In Python, there is a basic set of about 30 functions called builtin functions. Many of them
are shortcuts that make your everyday programming a lot easier. Here, the most common
ones are summarized, so that you can check your vocabulary.

Type Conversions
The type conversion functions convert one data type into another. Examples for them are
in an earlier section:
int(x) str(x) dict(x) bool(x)
float(x) list(x) tuple(x) set(x)

Input and output


Basic reading and writing data requires just three functions:
print(s) input(s) open(filename, mode)

Mathematical functions
With abs() you can calculate the absolute value of a number:
>>> abs(-42)
42

With round() you can round numbers to a given number of digits:


>>> round(3.14159, 2)
3.14

The divmod() function calculates a division and a modulo at the same time:
>>> divmod(23, 5)
(4, 3)

49

Builtin functions

The pow function does the same as the ** operator:


>>> pow(3, 3)
27

Working with sequences


There are tons of functions that help with Python sequences (lists, dictionaries, tuples and
iterators). The most common ones are:
len(x) min(x) sorted(x) enumerate(x)
sum(x) max(x) reversed(x) zip(x)
range(x)

I will explain them one by one

Determining the length of sequences


The len() function returns an integer with the length of an argument. It works with strings,
lists, tuples, and dictionaries.
>>> data = [0, 1, 2, 3]
>>> len(data)
4

Summing up numbers
The sum of a list of integer or float numbers can be calculated by the sum() function.
>>> data = [1, 2, 3, 4]
>>> sum(data)
10

Smallest and largest value


The functions min() and max() determine the smallest and largest value of a list:

50

Builtin functions

>>> data = [3, 5, 1, 7]


>>> min(data)
1
>>> max(data)
7

Creating lists of integer numbers


The range() function allows to create lists of numbers on-the-fly. There are two optional
parameters for the start value and the step size.
>>> list(range(4))
[0, 1, 2, 3]
>>> list(range(1, 5))
[1, 2, 3, 4]
>>> list(range(2, 9, 2))
[2, 4, 6, 8]
>>> list(range(5, 0, -1))
[5, 4, 3, 2, 1]

Note that because range() returns an iterator (a kind of lazy on-demand list), you need to
convert it to a list to see the data.

Enumerating list elements


The enumerate() function helps with counting elements. It creates tuples consisting of an
integer number starting from zero and the elements of the list.
>>> fruits = ['apple', 'banana', 'orange']
>>> list(enumerate(fruits))
[(0, 'apple'), (1, 'banana'), (2, 'orange')]

Note that enumerate() produces an iterator. To obtain a list, you need to convert it.
enumerate() is a great shortcut to loops with counter variables:

i = 0
for elem in data:
print(i, elem)
i += 1

becomes simply:

51

Builtin functions

for i, elem in enumerate(data):


print(i, elem)

Sorting data
The sorted() function sorts a list or the keys of a dictionary, but does not change the
original data.
>>> sorted(data)
[1, 3, 5, 7]

Reversing data
The reversed() function reverses the order of list elements, but does not change the
original data. It returns an iterator.
>>> data = [3, 5, 1, 7]
>>> list(reversed(data))
[7, 1, 5, 3]

Merging two lists


The zip() function associates the elements of two lists to a single list or tuple. Excess
elements are ignored.
fruits = ['apple','banana','orange']
prices = [0.55, 0.75, 0.80, 1.23]
for fruit, price in zip(fruits, prices):
print(fruit, price)

52

Modules and packages

Modules and Packages


Modules
Any Python file (ending with .py ) can be imported by Python script. A single Python file is
also called a module. This helps you to divide a bigger program into several smaller pieces.
For instance if you have a file names.py containing the following:
FIRST_NAMES = ['Alice', 'Bob', 'Charlie']

Then you can write (e.g. in a second Python file in the same directory):
import names
print(names.FIRST_NAMES)

Packages
For big programs, it is useful to divide up the code among several directories. A directory
from which you can import Python modules is called a package. To create a package that
Python will recognize you need to create a file __init__.py (it may be empty).
For instance, you could have the following files in a package namedata :
namedata/
__init__.py
names.py

Importing modules and packages


To import from a module, a package or their contents, place its name (without .py) needs to
be given in the import statement. Import statements can look like this:
import names
import names as n
from names import FIRST_NAMES
from namedata.names import FIRST_NAMES

53

Modules and packages

It is strongly recommended to list the imported variables and functions explicitly and not
write
from names import *

The latter makes debugging difficult.


When importing, Python generates intermediate files (bytecode) in the __pycache__
directory that help to execute programs more efficiently. It is managed automatically, and you
can safely ignore it.

How does Python find modules and packages?


When importing modules or packages, Python needs to know where to find them. There is a
certain sequence of directories in which Python looks for things to import:
The current directory.
The site-packages folder (where Python is installed).
In directories in the PYTHONPATH environment variable.
You can see all directories from within Python by checking the sys.path variable:
import sys
print sys.path

54

Handling Exceptions

Handling Exceptions
The try.. except clause catches errors and allows you to react on them.
# print all cards with even numbers.
cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
for card in cards:
try:
number = int(card)
if number % 2 == 0: # modulo operator
print(card, "is an even card.")
except ValueError:
print (card, "can not be divided")

Normally, int("J") would terminate the program. The except clause allows the program
to finish neatly.
In Python, Exceptions should always be handled explicitly. Using a generic except is
considered a very bad habit, because it makes debugging difficult.

Creating your own Exceptions


You can define your own types of Exceptions:
class MyError(Exception): pass

and create an error using your own type:


raise MyError("message")

and catch it:


try:
..
..
except MyError:
..

55

Handling Exceptions

56

Introspection

Introspection
Introspection is a feature of Python by which you can examine objects (including variables,
functions, classes, modules) inside a running Python environment (a program or shell
session).

Exploring the namespace


In Python all objects (variables, modules, classes, functions and your main program) are
boxes called namespaces. You can imagine the namespace of an object as the data and
functions inside than object. You can explore a namespace with the dir() function.

Exploring the namespace of a variable


With a string object, you see all the string methods:
s = "Emily"
print(dir(s))

Exploring the namespace of a module:


The same works for a module you import:
import time
print(dir(time))

Listing the builtin functions:


You also can view all builtin functions:
print(dir(__builtins__))

The help function


You can get context-sensitive help to functions, methods and classes with help() function.

57

Introspection

import time
print help(time.asctime)

help() utilizes the triple-quoted comments called docstrings, so that documentation you

write for your own functions is also availabel through help() :

Everything is an object
One consequence of the dynamic typing is that Python can treat everything it manages
technically in the same way. Everything is an object is a common phrase describing how
Python works. There is no fundamental difference between a function and an integer. Many
advanced features of Python are built on this concept.

58

Leftovers

Leftovers
Lists versus Dictionaries
Which is better, lists or dictionaries?
Generally, lists are good for sorting while dictionaries are good for searching.

The None value


The None data type is used when a function does not have a return statement. You can
also use it to indicate that some position in a list or dictionary is empty:
traffic_light = [None, None, 'green']

Installing additional Python modules


There are more than 75,000 additional modules/packages for Python available. The most
common ones are already installed with the Anaconda distribution. The next best solution is
to install Python modules using pip . pip is a program designed to make installing
modules easy. For instance, to install the pandas library, you can type on a terminal (not
the Python shell):
pip install pandas

Depending on your system configuration, this may require administrator privileges.

59

Anda mungkin juga menyukai