Python 3 Basics Tutorial
Python 3 Basics Tutorial
3 Basics Tutorial
Table of Contents
Introduction 0
The dataset of U.S. baby names 1
Installing Python 2
First steps on the IPython Shell 3
Using Python as a calculator 3.1
Warming up 3.1.1
Definitions 3.1.2
Exercises 3.1.3
Storing numbers 3.2
Warming up 3.2.1
Definitions 3.2.2
Exercises 3.2.3
Storing text 3.3
Warming up 3.3.1
Definitions 3.3.2
Exercises 3.3.3
Converting numbers to text and back 3.4
Warming up 3.4.1
Definitions 3.4.2
Exercises 3.4.3
Writing Python programs 4
Writing to the screen 4.1
Warming up 4.1.1
Definitions 4.1.2
Exercises 4.1.3
Reading from the keyboard 4.2
Warming up 4.2.1
Definitions 4.2.2
Exercises 4.2.3
Repeating instructions 4.3
2
Python 3 Basics Tutorial
Warming up 4.3.1
Definitions 4.3.2
Exercises 4.3.3
Storing lists of items 4.4
Warming up 4.4.1
Definitions 4.4.2
Exercises 4.4.3
Making decisions 4.5
Warming up 4.5.1
Definitions 4.5.2
Exercises 4.5.3
Reading and writing files 5
Reading a text file 5.1
Warming up 5.1.1
Definitions 5.1.2
Exercises 5.1.3
Writing a text file 5.2
Warming up 5.2.1
Definitions 5.2.2
Exercises 5.2.3
File parsing 5.3
Warming up 5.3.1
Definitions 5.3.2
Exercises 5.3.3
Working with directories 5.4
Warming up 5.4.1
Definitions 5.4.2
Exercises 5.4.3
Using external modules 6
Introspection 6.1
Warming up 6.1.1
Definitions 6.1.2
Standard modules and libraries 6.2
re 6.2.1
3
Python 3 Basics Tutorial
xml 6.2.2
urllib 6.2.3
time 6.2.4
csv 6.2.5
Installable modules and libraries 6.3
pandas 6.3.1
xlrd 6.3.2
scipy 6.3.3
matplotlib 6.3.4
pillow 6.3.5
Leftovers 7
Overview of Data types in Python 7.1
Dictionaries 7.2
Tuples 7.3
while 7.4
List functions 7.5
Structuring bigger programs 8
Functions 8.1
Modules 8.2
Packages 8.3
Background information on Python 3 9
Recommended books and websites 10
Acknowledgements 11
4
Python 3 Basics Tutorial
Distributed under the conditions of the Creative Commons Attribution Share-alike License
4.0
Introduction
you have worked a little with a different programming language like R, MATLAB or C.
you have no programming experience at all
you know Python well and would like to teach others
This tutorial works best if you follow the chapters and exercises step by step.
For a tutorial for non-beginners, I recommend the following free online books:
Introduction 5
Python 3 Basics Tutorial
The authorities of the United States have recorded the first names of all people born as U.S.
citizens since 1880. The dataset is publicly available on
http://www.ssa.gov/oact/babynames/limits.html . However for the protection of privacy only
names used at least 5 times appear in the data.
Installing Python
The first step into programming is to get Python installed on your computer. You will need
two things
Python itself
a text editor
On Ubuntu Linux
By default, Python is already installed. In this tutorial however, we will use Python3
whenever possible. You can install it from a terminal window with:
ipython3
As a text editor, it is fine to start with gedit. Please make sure to change tabs to spaces via
Edit -> Preferences -> Editor -> tick 'Insert spaces instead of tabs'.
On Windows
A convenient way to install Python, an editor and many additional packages in one go is
Canopy. Download and install it from the website.
After installing, you will need to set up Canopy from the Start menu. Select the first item from
the Enthought Canopy group and click through the dialog.
Installing Python 7
Python 3 Basics Tutorial
applications
Other editors
Idle - the standard editor
Sublime Text - a very powerful text editor for all operating systems
Notepad++ - a powerful text editor for Windows. Please do not use the standard
Notepad. It won't get you anywhere.
PyCharm - a professional Python development environment capable of handling large
projects. You won't need most of the functionality for a long time, but it is a well-written
editor.
vim - a console-based text editor for Unix systems. The tool of choice for many system
administrators.
Questions
Question 1
Which text editors are installed on your system?
Question 2
Which Python version are you running?
Installing Python 8
Python 3 Basics Tutorial
In the first part of the tutorial, you will use the IPython shell to write simple commands.
Goals
The commands on the IPython shell cover:
name number
Jacob 34465
Michael 32025
Matthew 28569
Joshua 27531
Christopher 24928
Warming up
Enter Python in the interactive mode. You should see a message
In [1]:
In [1]: 1 + ___
Out[1]: 3
In [2]: 12 ___ 8
Out[2]: 4
In [3]: ___ * 5
Out[3]: 20
In [4]: 21 / 7
Out[4]: ___
In [5]: ___ ** 2
Out[5]: 81
Enter the commands to see what happens (do not type the first part In [1] etc., these will
appear automatically).
Warming up 11
Python 3 Basics Tutorial
Definitions
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]:
Integer numbers
The numerical values in the calculations are called integers. An integer is a number that has
no decimal places. In Python, integers are a predefined data type.
Later, we will learn to know other kinds of numbers and data types.
Operators
The arithmetical symbols like + - * / connecting two numbers are called operators.
Generally, operators connect two values.
Arithmetical operators
Definitions 12
Python 3 Basics Tutorial
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
Definitions 13
Python 3 Basics Tutorial
Exercises
Complete the following exercises:
Exercise 1:
There are more operators in Python. Find out what the following operators do?
3 ** 3
24 % 5
23 // 10
Exercise 2:
Which of the following Python statements are valid? Try them in the IPython shell.
0 + 1
2 3
4-5
6 * * 7
8 /
9
Exercise 3:
Which operations result in 8?
[ ] 65 // 8
[ ] 17 % 9
[ ] 2 ** 4
[ ] 64 ** 0.5
Exercises 14
Python 3 Basics Tutorial
Storing numbers
The U.S. authorities record the names of all babies born since 1880. How many babies with
more or less popular names were born in 2000? Let's store the numbers in variables.
Storing numbers 15
Python 3 Basics Tutorial
Warming up
Let's define some variables:
In [9]: all_babies = 0
In [10]: all_babies = _____ + _____ + _____
In [11]: all_babies
Out[11]: 49031
Insert the correct values and variable names into the blanks.
Warming up 16
Python 3 Basics Tutorial
Definitions
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
Changing variables
You may assign to the same variable name twice:
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.
Calculate a number
Put a number into a variable
Definitions 17
Python 3 Basics Tutorial
In the next lessons, you will learn to know lots of other statements.
Definitions 18
Python 3 Basics Tutorial
Exercises
Complete the following exercises:
Exercise 1:
Which of the following variable names are correct? Try assigning some numbers to them.
Emily
EMILY
emily brown
emily25
25emily
emily_brown
emily.brown
Exercise 2:
Which are correct variable assignments?
[ ] a = 1 * 2
[ ] 2 = 1 + 1
[ ] 5 + 6 = y
[ ] seven = 3 * 4
Exercises 19
Python 3 Basics Tutorial
Storing text
The Challenge
first name Andrew
Write the information from each row of the table into a separate string variable, then
combine them to a single string, e.g.:
Storing text 20
Python 3 Basics Tutorial
Warming up
So far, we have only worked with numbers. Now we will work with text as well.
first = 'Emily'
last = "Smith"
first
last
name[0]
name[3]
name[-1]
Warming up 21
Python 3 Basics Tutorial
Definitions
String
Text values are called strings. In Python, strings are defined by single quotes, double
quotes or three quotes of either. The following are equivalent:
first = 'Emily'
first = "Emily"
first = '''Emily'''
first = """Emily"""
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.
Indexing
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]
Definitions 22
Python 3 Basics Tutorial
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.
s = 'Emily Smith'
s[1:6]
s[6:11]
s[0:2]
s[:3]
s[-4:]
Definitions 23
Python 3 Basics Tutorial
Exercises
Exercise 1:
Is it possible to include the following special characters in a string?
Exercise 2:
What do the following statements do?
first = `Hannah`
first = first[0]
name = first
name = ""
Exercise 3:
Explain the code
text = ""
characters = "ABC"
text = characters[0] + text
text = characters[1] + text
text = characters[2] + text
text
Exercises 24
Python 3 Basics Tutorial
Write the values from each row of the table into string or integer variables, then combine
them to a single one-line string.
Warming up
Now we are going to combine strings with integer numbers.
Insert into the following items into the code, so that all statements are working: age ,
int(age) , name, str(born) , 2000
Questions
Can you leave str(born) and int(age) away?
What do str() and int() do?
Warming up 26
Python 3 Basics Tutorial
Definitions
Type conversions
If you have an integer number i , you can make a string out of it with str(i) :
text = str(2000)
If you have a string s , you can make an integer out of it with int(s) :
number = int("2000")
Both int() and str() change the type of the given data. They are therefore called type
conversions.
Functions
With int() and str() , you have just learned to know two functions. In Python, functions
consist of a name followed by parentheses. They can have parameters, so that a function is
used like:
y = f(x)
Using a function is a regular Python statement (or part thereof). It is executed only once
Definitions 27
Python 3 Basics Tutorial
Exercises
Exercise 1:
What is the result of the following statements?
9 + 9
9 + '9'
'9' + '9'
Exercise 2:
Change the statements above by adding int() or str() to each of them, so that the result is 18
or '99', respectively.
Exercise 3:
Explain the result of the following operations?
9 * 9
9 * '9'
'9' * 9
Exercise 4:
Write Python statements that create the following string:
12345678901234567890123456789012345678901234567890
Exercises 28
Python 3 Basics Tutorial
In the second part of the tutorial, you will learn a basic set of Python commands.
Theoretically, they are sufficient to write any program on the planet (this is called Turing
completeness).
Practically, you will need shortcuts that make programs prettier, faster, and less painful to
write. We will save these shortcuts for the later parts.
Goals
The new Python commands cover:
Extra challenges:
Use a single print statement to produce the output.
Store the names in separate variables first, then combine them.
Use string formatting.
Warming up
Open a text editor window (not a Python console). Type:
print("Hannah")
print(23073)
python3 first_program.py
Warming up 31
Python 3 Basics Tutorial
Definitions
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.
print
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:
Definitions 32
Python 3 Basics Tutorial
print('Hello World')
print(3 + 4)
print(3.4)
print("""Text that
stretches over
multiple lines.
""")
print('number', 77)
print()
print(int(a) * 7)
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.
%i – an integer.
Escape characters
Strings may contain also the symbols: \t (tabulator), \n (newline), \r (carriage return),
and \\ (backslash).
Definitions 33
Python 3 Basics Tutorial
Exercises
Exercise 1
Explain the following program:
name = "Emily"
year = 2000
print(name, year)
Exercise 2
Write into a program:
name = "Emily"
name
What happens?
Exercise 3
Which print statements are correct?
[ ] print("9" + "9")
[ ] print "nine"
[ ] print(str(9) + "nine")
[ ] print(9 + 9)
[ ] print(nine)
Exercises 34
Python 3 Basics Tutorial
Extra challenge:
Add 1 to the age entered.
Warming up
What happens when you write the follwing lines in the IPython shell:
In [1]: a = input()
In [2]: a
Warming up 36
Python 3 Basics Tutorial
Definitions
input
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 for learning Python.
Definitions 37
Python 3 Basics Tutorial
Exercises
Exercise 1
Which input statements are correct?
[ ] a = input()
[ ] a = input("enter a number")
[ ] a = input(enter your name)
[ ] a = input(3)
Exercises 38
Python 3 Basics Tutorial
Repeating instructions
So far, each Python instruction was executed only once. That makes programming a bit
useless, because our programs are limited by our typing speed.
In this section you will learn the for statements that repeats one or more instructions
several times.
Extra challenge
Duplicate each character, so that Emily becomes EEmmiillyy .
Repeating instructions 39
Python 3 Basics Tutorial
Warming up
What does the following program do?
text = ""
characters = "Hannah"
for char in characters:
text = char + text
print(text)
Warming up 40
Python 3 Basics Tutorial
Definitions
Later we will see how you can run for loops over other things e.g. a list, a tuple, a
dictionary, a file or a function. Some examples:
for i in range(3):
print(i)
Reserved words
The two words for and in are also 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.
Definitions 41
Python 3 Basics Tutorial
import keyword
keyword.kwlist
Indentation is a central element of Python syntax. Indentation must not be used for
decorative purposes.
There are other Python commands that are also followed by code blocks. All of them end
with a colon ( : ) at the end of the line.
Definitions 42
Python 3 Basics Tutorial
Exercises
Exercise 1
Which of these for commands are correct?
Exercise 2
Write a for loop that creates the following output
000
111
222
333
444
555
666
777
888
999
Exercise 3
Write a for loop that creates the following string variable:
000111222333444555666777888999
Exercise 4
Write a for loop that creates the following output
Exercises 43
Python 3 Basics Tutorial
1
3
6
10
15
21
28
Exercise 5
Write a for loop that creates the following string
"1 4 9 16 25 36 49 64 81 "
Exercise 6
Add a single line at the end of the program, so that it creates the following string:
"1 4 9 16 25 36 49 64 81"
Exercises 44
Python 3 Basics Tutorial
name number
Jacob 34465
Michael 32025
Matthew 28569
Joshua 27531
Christopher 24928
Nicholas 24650
Andrew 23632
Joseph 22818
Daniel 22307
Tyler 21500
Hint
If you are using Python2, you need to specify whether you want numbers with decimal
places when dividing. That means
3 / 4
3.0 / 4
Warming up
Find out what each of the expressions does to the list in the center.
Warming up 47
Python 3 Basics Tutorial
Definitions
Lists
A list is a Python data type representing a sequence of elements. You can have lists of
strings:
print(names[0])
print(numbers[3])
print(names[-1])
Definitions 48
Python 3 Basics Tutorial
girls = names[:]
names.append('Marilyn')
names.remove(3)
names.pop()
names[4] = 'Jessica'
Sorting a list
names.sort()
Counting elements
names = ['Hannah', 'Emily', 'Sarah', 'Emily', 'Maria']
names.count('Emily')
Definitions 49
Python 3 Basics Tutorial
Exercises
Exercise 1
What does the list b contain?
a = [8, 7, 6, 5, 4]
b = a[2:4]
[ ] [7, 6, 5]
[ ] [7, 6]
[ ] [6, 5]
[ ] [6, 5, 4]
Exercise 2
Use the expressions to modify the list as indicated. Use each expression once.
Exercises 50
Python 3 Basics Tutorial
Exercise 3
Use the expressions to modify the list as indicated. Use each expression once.
Exercises 51
Python 3 Basics Tutorial
Making decisions
The last missing piece in our basic set of commands is the ability to make decisions in a
program. This is done in Python using the if command.
Extra challenge
count the number of names starting with A and print that number as well.
Making decisions 52
Python 3 Basics Tutorial
Warming up
Add your favourite movie to the following program and execute it:
Warming up 53
Python 3 Basics Tutorial
Definitions
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 for or if statement, all indented commands are treated as a code block, and
are executed in the context of the condition.
Comparison operators
An if expression may contain comparison operators like:
a == b
a != b
a < b
a > b
a <= b
a >= b
Definitions 54
Python 3 Basics Tutorial
a in b
a or b
a and b
not a
(a or b) and (c or d)
False
0
[]
''
{}
set()
None
Definitions 55
Python 3 Basics Tutorial
Exercises
Exercise 1
Which of these if statements are syntactically correct?
[ ] if a and b:
[ ] if len(s) == 23:
[ ] if a but not b < 3:
[ ] if a ** 2 >= 49:
[ ] if a != 3
[ ] if (a and b) or (c and d):
Exercise 2
Write a program that lets the user enter a number on the keyboard. Find the number in the
list that is closest to the number entered and write it to the screen.
Exercises 56
Python 3 Basics Tutorial
Goals
read text files
write text files
extract information from a file
list all files in a directory
The Challenge
Write a program that counts the number of names in the file yob2014.txt from the dataset
of baby names.
Warming up
Match the descriptions with the Python commands.
Warming up 59
Python 3 Basics Tutorial
Definitions
f = open('my_file.txt')
for line in f:
...
f = open('my_file.txt')
text = f.read()
On Windows, this is a bit more cumbersome, because you must replace the backslash \
by 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
Definitions 60
Python 3 Basics Tutorial
f.close()
Definitions 61
Python 3 Basics Tutorial
Exercises
Exercise 1:
Make the program work by inserting close , line , yob2014.txt , print into the gaps.
f = open(___)
for ____ in f:
____(line)
f.____()
Exercises 62
Python 3 Basics Tutorial
Write a program that writes all names into a single text file.
Extra Challenges
Write each name into a separate line.
Write the numbers into the same file.
Write each number into the same line as the corresponding name.
Warming up
Execute the following program. Explain what happens.
f = open('boy_names.txt', 'w')
for name in names:
f.write(name + '\n')
f.close()
Remove the + '\n' from the code and run the program again. What happens?
Warming up 64
Python 3 Basics Tutorial
Definitions
f = open('my_file.txt','w')
f.write(text)
If your data is a list of strings, it can be written to a file as a one-liner. You only need to add
line breaks:
Appending to a file
It is possible to add text to an existing file, too.
f = open('my_file.txt','a')
f.write(text)
Definitions 65
Python 3 Basics Tutorial
Exercises
Exercise 1
Which are correct commands to work with files?
Exercises 66
Python 3 Basics Tutorial
Extra Challenge:
Calculate the total number of babies registered in 2014.
File parsing 67
Python 3 Basics Tutorial
Warming up
Create a text file in a text editor. Write the following line there:
Alice Smith;23;Macroeconomics
f = open('alice.txt')
print(f)
text = f.read()
print(text)
columns = text.strip().split(';')
print(columns)
name = columns[0]
age = int(columns[1])
studies = columns[2]
print(name)
print(age)
print(studies)
What happens?
Warming up 68
Python 3 Basics Tutorial
Definitions
This pattern goes through a file line by line (the for line). It then chops off a linebreak
character from each line ( strip ) and finally breaks the line into a list of items.
str.strip()
With the string function strip() , you chop off whitespace characters (spaces, newline
characters and tabs) from both ends of a string.
The line
produces:
print(s)
"this is text"
str.split()
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 whitespace).
s = "this is text"
t = s.split(" ")
print(t)
Definitions 69
Python 3 Basics Tutorial
Definitions 70
Python 3 Basics Tutorial
Exercises
Exercise 1
What does the following line produce?
"Take That".split('a')
Exercise 2
Create a text file with the contents:
Alice Smith;23;Macroeconomics
Bob Smith;22;Chemistry
Charlie Parker;77;Jazz
Write a program that reads all names and puts them into a list. Print the list.
Exercise 3
Collect the ages into a separate list.
Exercise 4
Collect the occupations into a separate list.
Exercise 5
Leave the strip() command away from the above program. What happens?
Exercises 71
Python 3 Basics Tutorial
Extra Challenges
Read each of the files and count the total number of lines.
Generate a message that tells you which file the program is reading.
Find your name in the files with a program.
Warming up
Fill in the gaps
Warming up 73
Python 3 Basics Tutorial
Definitions
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 this section of the tutorial, you will need just one function from the module. The function
y = os.listdir("x")
gives you a list of all files in the directory x and stores it in the variable y .
Changing directories
With the os module, you can change the current directory:
import os
os.chdir(''..\\python'')
print(os.access('my_file.txt'))
Definitions 74
Python 3 Basics Tutorial
Exercises
Exercise 1
Explain the following code:
import os
for dirname in os.listdir('.'):
print(dirname)
Exercises 75
Python 3 Basics Tutorial
Introspection
Extra challenges:
let the user choose the gender of the babies.
let the user enter how many babies they want to have.
load baby names from a file.
Introspection 77
Python 3 Basics Tutorial
Warming up
Try the following on the interactive shell:
import math
dir(math)
help(math.sin)
Warming up 78
Python 3 Basics Tutorial
Definitions
Introspection is a feature of Python by which you can examine objects (including variables,
functions, classes, modules) at runtime.
print dir()
import time
print dir(time)
Built-in help
You can get context-sensitive help to functions, methods and classes that utilize the triple-
quoted comments: import time print help(time.asctime)
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, meaning it has attributes. These
attributes are objects themselves. Methods are simply attributes that can be called.
and a type:
print (type(x))
Definitions 79
Python 3 Basics Tutorial
Standard modules
There are more than modules already installed with Python. In earlier parts of this tutorial,
you have already seen some of them, e.g.:
math
random
os
This section introduces a few more that are useful while learning Python.
re
What is re?
re is a Python module for Regular Expressions. Regular expressions help you to search
Searching
import re
Replacing
re.sub('R[meo]+', 'London', text)
re 81
Python 3 Basics Tutorial
Ignoring case
If the case of the text should be ignored during the pattern matching, add re.IGNORECASE to
the parameters of any re function.
Exercises
Exercise 1
Write six patterns that extract all names from the following string:
Exercise 2
Reduce the number of patterns you need to three.
Exercise 3
Find all six names using a single pattern.
re 82
Python 3 Basics Tutorial
xml
What is xml?
xml is a Python module for reading XML documents.
Exercises
Exercise 1
Store the following in an XML document:
xml 83
Python 3 Basics Tutorial
<movielist name="movies">
<movie id="1" name="Slumdog Millionaire (2008)">
<rating value="8.0" votes="513804"></rating>
</movie>
<movie id="2" name="Planet of the Apes (1968)">
<rating value="8.0" votes="119493"></rating>
</movie>
<movie id="3" name="12 Angry Men (1957)">
<rating value="8.9" votes="323565"></rating>
</movie>
<movie id="4" name="Pulp Fiction (1994)">
<rating value="8.9" votes="993081"></rating>
</movie>
<movie id="5" name="Schindler's List (1993)">
<rating value="8.9" votes="652030"></rating>
</movie>
</movielist>
<movielist name="serials">
<movie id="6" name="Breaking Bad (2008)
{To'hajiilee (#5.13)}">
<rating value="9.7" votes="14799"></rating>
</movie>
<movie id="7" name="Game of Thrones (2011)
{The Laws of Gods and Men (#4.6)}">
<rating value="9.7" votes="13343"></rating>
</movie>
<movie id="8" name="Game of Thrones (2011)
{The Lion and the Rose (#4.2)}">
<rating value="9.7" votes="17564"></rating>
</movie>
<movie id="9" name="Game of Thrones (2011)
{The Rains of Castamere (#3.9)}">
<rating value="9.8" votes="27384"></rating>
</movie>
<movie id="10" name="Breaking Bad (2008)
{Ozymandias (#5.14)}">
<rating value="10.0" votes="55515"></rating>
</movie>
</movielist>
</movieml>
Exercise 2
Run the following program:
xml 84
Python 3 Basics Tutorial
document = parse('movies.xml')
taglist = document.getElementsByTagName("movie")
for tag in taglist:
mid = tag.getAttribute('id')
name = tag.getAttribute('name')
print(mid, name)
subtags = tag.getElementsByTagName('rating')
print(subtags)
Exercise 3
Calculate the total number of votes.
xml 85
Python 3 Basics Tutorial
urllib
The HTML code of web pages and downloadable files from the web can be accessed in a
similar way as reading files:
import urllib2
url = 'http://www.academis.eu'
page = urllib.urlopen(url)
print(page.read())
Exercises
Exercise 1
Download the homepage of your academic institution or company and print it to the screen.
urllib 86
Python 3 Basics Tutorial
import time
s = time.asctime()
i = time.time()
The datetime module helps you to represent dates and format them nicely:
and back
datetime.date.fromordinal(7)
Exercises
Exercise 1
Print today's date and time of day in a custom format.
time 87
Python 3 Basics Tutorial
csv
The csv module reads and writes tables from text files.
Reading
import csv
reader = csv.reader(open('my_file.csv'))
for row in reader:
print row
Writing
Similarly, tables can be written to files:
write = csv.writer(open('my_file.csv','w'))
write.writerow(table)
csv 88
Python 3 Basics Tutorial
csv 89
Python 3 Basics Tutorial
pip
pip is a program designed to make installing modules easy.
pandas
What is pandas?
pandas is a Python library for efficient handling of tables.
Installation
Write in the command line:
Exercises
Exercise 1
Execute the following program:
import pandas as pd
girls = []
for year in range(1880, 2015):
fn = "names/yob%i.txt" % year
df = pd.read_csv(PATH + fn, names=['gender', year], index_col=0)
girl = df[df.gender=='F'][year]
girls.append(girl)
girls = pd.DataFrame(girls).fillna(0)
Exercise 2
Write the content of the variables to the screen by adding print statements. Explain what it
does.
Exercise 3
Add the following lines to the program. Explain them.
pandas 91
Python 3 Basics Tutorial
tgirls = girls.transpose()
tgirls['sum'] = girls.apply(sum)
Exercise 4
Add the following lines to the program. Explain them.
Exercise 5
Add lines to conduct a similar analysis of boys' names.
pandas 92
Python 3 Basics Tutorial
xlrd
What is xlrd?
xlrd is a Python library for reading Excel documents.
Installation
Write in the command line:
Exercises
Exercise 1
Create an Excel table similar to the data in the .txt files.
name number
Jacob 34465
Michael 32025
Matthew 28569
Exercise 2
Execute the following program:
xlrd 93
Python 3 Basics Tutorial
import xlrd
workbook = xlrd.open_workbook("data.xlsx")
sheet_names = workbook.sheet_names()
sheet = workbook.sheet_by_name(sheet_names[0])
row = sheet.row(0)
for row_idx in range(0, sheet.nrows):
print ('-'*40)
print ('Row: %s' % row_idx)
for col_idx in range(0, sheet.ncols):
cell_obj = sheet.cell(row_idx, col_idx)
print ('Column: [%s] cell_obj: [%s]' % (col_idx, cell_obj))
Exercise 3
Make the program work and print the data from the document to the screen.
Exercise 4
Calculate the sum of numeric values in the Python program.
xlrd 94
Python 3 Basics Tutorial
scipy
What is scipy?
scipy is a Python library for fitting functions and other kinds of numerical analyses.
Installation
Write in the command line:
Note: on some machines, this may not work. Especially on Windows, you will need to install
e.g. the Anaconda distribution to avoid a long and painful struggle.
Exercises
Exercise 1
Execute the following program:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
fig = plt.figure
plt.plot(x, y, "--")
plt.show()
scipy 95
Python 3 Basics Tutorial
Exercise 2
Add print statements to see what the program does.
Exercise 3
Plot the y values with noise added.
Exercise 4
Plot the y values of the best-fit function.
Hint:
The plot function allows you to use various symbols for plotting, e.g. ^ * . - .
Exercise 5
Change the program to generate and fit a linear function instead.
References
Code adopted from the O'Reilly book: Scipy and Numpy. See more awesome examples
there.
scipy 96
Python 3 Basics Tutorial
matplotlib
What is matplotlib?
matplotlib is a Python module for creating diagrams.
Exercises
Exercise 1
Run the code to create a line plot:
xdata = [1, 2, 3, 4]
ydata = [1.25, 2.5, 5.0, 10.0]
figure()
plot(xdata, ydata, "r-")
savefig('simple_plot.png')
Exercise 2
Run the code to create a pie chart:
def get_percent(value):
'''Formats float values in pie slices to percent.'''
return "%4.1f%%" % (value)
figure(1)
title('frequency of top 5 girls names in 2000')
pie(count, explode=explode, labels=names, shadow=True,
colors=colors, autopct=get_percent)
savefig('piechart.png', dpi=150)
matplotlib 97
Python 3 Basics Tutorial
Exercise 3
Run the code to create a bar plot:
counts = [
[606, 1024, 759, 398],
[762, 912, 639, 591],
]
figure()
title('RNA nucleotides in the ribosome')
xlabel('RNA')
ylabel('base count')
xticks(x1, nucleotides)
legend()
axis([1.0, 9.0, 0, 1200])
savefig('barplot.png')
Exercise 4
Write a program creating a line plot for the frequency of one name over several years.
matplotlib 98
Python 3 Basics Tutorial
pillow
What is pillow?
pillow is a Python module for image manipulation. It derives from the Python Imaging
Library (PIL).
PIL has been around since 2002, but Pillow is the currently maintained distribution.
Installation
pip install pillow
Exercises
Exercise 1
Execute a program for converting an image format.
carrot = Image.open('carrot.png')
img.save('carrot.jpg')
pillow 99
Python 3 Basics Tutorial
Exercise 2
Execute a program to merge two images.
carrot = Image.open('carrot.png')
brain = Image.open('brain.png')
img.save('step01.png')
pillow 100
Python 3 Basics Tutorial
Exercise 3
Execute the program to resize an image.
carrot = Image.open('carrot.png')
brain = Image.open('brain.png')
img.save('step02.png')
pillow 101
Python 3 Basics Tutorial
Exercise 4
Execute the program to draw an arrow
draw = ImageDraw.Draw(arrow)
draw.rectangle((0, 50, 100, 100), fill="black")
draw.polygon((100, 25, 100, 125, 150, 75), fill="black")
arrow.save('arrow.png')
pillow 102
Python 3 Basics Tutorial
Exercise 4
Execute the program to draw a gradient.
for x in range(250):
box = (x, 0, x+1, 250)
color = (255, (255-x), 0)
draw.rectangle(box, fill=color)
gradient.save('gradient.png')
Exercise 5
Execute the program to superimpose two images
arrow = Image.open('arrow.png')
gradient = Image.open('gradient.png')
img.save('grad_arrow.png')
pillow 103
Python 3 Basics Tutorial
f1 = ImageChops.multiply(brain, carrot)
f2 = ImageChops.blend(brain, carrot, 0.2)
f3 = ImageChops.screen(carrot, gradient)
Exercise 6
Combine the carrot, brain and arrow into one image.
Exercise 7
Add text to the image
draw = ImageDraw.Draw(img)
arial = ImageFont.truetype('arial.ttf', 40)
draw.text((220, 360), "Carrots rock your brain!",
fill=(0,0,0), font=arial)
img.save('step06.png')
pillow 104
Python 3 Basics Tutorial
Exercise 7
Bonus:
Write a 70-line script that
Calling MEncoder:
pillow 105
Python 3 Basics Tutorial
Leftovers
Here you find some things which you may find useful and which didn't fit anywhere else.
Leftovers 106
Python 3 Basics Tutorial
Data types
Match the data samples with their types.
Definitions
List [1, 2, 2, 3]
Dictionary {'name': 'John Smith', 'year': 1990}
Set ()
Type conversions
Values can be converted into each other using conversion functions. Try the following:
int('5.5')
float(5)
str(5.5)
list("ABC")
tuple([1,2,3])
dict([('A',1),('B',2)])
set([1,2,2,3])
Exercises
Exercise 1
On which data types does the len() function work?
[ ] lists
[ ] dictionaries
[ ] strings
[ ] floats
Dictionaries
Using a dictionary
Find out what each of the expressions does to the dictionary in the center.
Definitions
Dictionaries
Dictionaries are an unordered, associative array. They have a set of key/value pairs. They
are very versatile data structures, but slower than lists. Dictionaries can be used easily as a
hashtable.
Creating dictionaries
Dictionaries 109
Python 3 Basics Tutorial
prices = {
'banana':0.75,
'apple':0.55,
'orange':0.80
}
Methods of dictionaries
There is a number of functions that can be used on every dictionary:
prices.has_key('apple')
prices.get('banana')
prices.get('kiwi')
Dictionaries 110
Python 3 Basics Tutorial
prices.setdefault('kiwi', 0.99)
prices.setdefault('banana', 0.99)
# for 'banana', nothing happens
print prices.keys()
print prices.values()
print prices.items()
Exercises
Exercise 1.
What do the following commands produce?
[ ] False
[ ] "B"
[ ] True
[ ] 1
Exercise 2.
What do these commands produce?
[ ] 1
[ ] True
[ ] "B"
[ ] False
Exercise 3.
What do these commands produce?
Dictionaries 111
Python 3 Basics Tutorial
[ ] True
[ ] ['A', 1, True]
[ ] 3
[ ] [1, 'B', 'A']
Exercise 4.
What do these commands produce?
Exercise 5.
What do these commands produce?
[ ] None
[ ] 'C'
[ ] an Error
[ ] False
Exercise 6.
What do these commands produce?
[ ] 3
Dictionaries 112
Python 3 Basics Tutorial
[ ] 'C'
[ ] None
[ ] an Error
Dictionaries 113
Python 3 Basics Tutorial
Tuples
A tuple is a sequence of elements that cannot be modified. They are useful to group
elements of different type.
t = ('bananas','200g',0.55)
Exercises
Exercise 1
Which are correct tuples?
[ ] (1, 2, 3)
[ ] ("Jack", "Knife")
[ ] ('blue', [0, 0, 255])
[ ] [1, "word"]
Exercise 2
What can you do with tuples?
Tuples 114
Python 3 Basics Tutorial
Exercise
Match the expressions so that the while loops run the designated number of times.
While loops combine for and if . They require a conditional expression at the beginning.
The conditional expressions work in exactly the same way as in if.. elif statements.
i = 0
while i < 5:
print (i)
i = i + 1)
while 115
Python 3 Basics Tutorial
Exercises
Exercise 1
Which of these while commands are correct?
[ ] while a = 1:
[ ] while b == 1
[ ] while a + 7:
[ ] while len(c) > 10:
[ ] while a and (b-2 == c):
[ ] while s.find('c') >= 0:
Exercise 2
Which statements are correct?
while 116
Python 3 Basics Tutorial
Built-in functions
Examples
data = [0,1,2,3]
len(data)
Summing up numbers
The sum() of a list of integer or float numbers or strings can be calculated by the sum()
function.
data = [1,2,3,4]
sum(data)
fruits = ['apple','banana','orange']
prices = [0.55, 0.75, 0.80, 1.23]
for fruit,price in zip(fruits, prices):
print(fruit, price)
Other functions
sorted(data) returns a sorted list
map(f, data) applies a function to all elements.
filter(f, data) returns elements for which f is True.
reduce(f, data) collapses the data into one value.
Structuring programs
In Python, you can structure programs on four different levels: with functions, classes,
modules and packages. Of these, classes are the most complicated to use. Therefore they
are skipped in this tutorial.
Goals
Learn to write functions
Learn to write modules
Learn to write packages
Know some standard library modules
Know some installable modules
Functions
What is a function?
A function is an autonomous sub-program with local variables, input and output, and its own
namespace. 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))
Optional parameters
Function parameters may have default values. In that case, they become optional. Do not
use mutable types as default arguments!
print(calc_prices('banana'))
print(calc_prices('banana',100))
Return values
A function may return values to the program part that called it.
return 0.75
Functions 120
Python 3 Basics Tutorial
Functions 121
Python 3 Basics Tutorial
Modules
What is a module?
Any Python file (ending .py) can be imported from another Python script. A single Python file
is also called a module.
Importing modules
To import from a module, its name (without .py) needs to be given in the import statement.
Import statements can look like this:
import fruit
import fruit as f
from fruit import fruit_prices
from my_package.fruit import fruit_prices
It is strongly recommended to list the imported variables and functions explicitly instead of
using the import * syntax. This makes debugging a lot easier.
When importing, Python generates intermediate code files (in the pycache directory) that
help to execute programs faster. They are managed automatically, and dont need to be
updated.
Modules 122
Python 3 Basics Tutorial
Packages
What is a package?
For big programs, it is useful to divide up the code among several directories. These are
called packages. Technically, a package is a directory containing Python files. To import a
package from outside, there needs to be a file init.py (it may be empty).
You can see all directories from within Python by checking the sys.path variable:
import sys
print sys.path
Exercises
Exercise 1
Join the right halves of sentences.
Packages 123
Python 3 Basics Tutorial
Exercise 2
Which import statements are correct?
[ ] import re
[ ] import re.sub
[ ] from re import sub
[ ] from re import *
[ ] from re.sub import *
Exercise 3
Where does Python look for modules to import?
Exercise 4
Packages 124
Python 3 Basics Tutorial
Exercise 5
Which packages are installed by default?
Packages 125
Python 3 Basics Tutorial
What is Python?
Python is an interpreted language.
Python uses dynamic typing.
Python 3 is not compatible to Python 2.x
The Python interpreter generates intermediate code (in the pycache directory).
Strengths
Quick to write, no compilation
Fully object-oriented
Many reliable libraries
All-round language
100% free software
Weaknesses
Writing very fast programs is not straightforward
No strict encapsulation
Paper books
Managing your Biological Data with Python - Allegra Via, Kristian Rother and Anna
Tramontano
Data Science from Scratch - Joel Grus
Websites
Main documentation and tutorial: http://www.python.org/doc
Tutorial for experienced programmers: http://www.diveintopython.org
Tutorial for beginners: http://greenteapress.com/thinkpython/thinkCSpy/html/
Comprehensive list of Python tutorials, websites and books:
http://www.whoishostingthis.com/resources/python/
Python Library Reference covering the language basics:
https://docs.python.org/3/library/index.html
Global Module Index – description of standard modules: https://docs.python.org/3/py-
modindex.html
Authors
© 2013 Kristian Rother (krother@academis.eu)
This document contains contributions by Allegra Via, Kaja Milanowska and Anna Philips.
License
Distributed under the conditions of a Creative Commons Attribution Share-alike License 3.0.
Acknowledgements
I would like to thank the following people for inspiring exchange on training and Python that
this tutorial has benefited from: Pedro Fernandes, Tomasz Puton, Edward Jenkins, Bernard
Szlachta, Robert Lehmann and Magdalena Rother
Acknowledgements 128