Sei sulla pagina 1di 128

Python

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

Python 3 Basics Tutorial


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

with contributions by Allegra Via, Kaja Milanowska and Anna Philips

Distributed under the conditions of the Creative Commons Attribution Share-alike License
4.0

Sources of this document can be found on


https://github.com/krother/Python3_Basics_Tutorial

Introduction

Who is this tutorial for?


This is a tutorial for novice programmers. You are the learner I had in mind when writing this
tutorial if:

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.

If you are an experienced programmer


If you are fluent in any programming language, this tutorial might be very easy for you. Of
course, you can work through the exercises to get the Python syntax into your fingers.
However, this tutorial contains very little material on the higher abstraction levels in Python,
like classes, namespaces or even functions.

For a tutorial for non-beginners, I recommend the following free online books:

Learn Python the Hard Way - a bootcamp-style tutorial by Zed Shaw


How to think like a Computer Scientist - a very systematic, scientific tutorial by Allen B.
Downey
Dive into Python 3 - explains one sophisticated program per chapter - by Mark Pilgrim

Introduction 5
Python 3 Basics Tutorial

The dataset of U.S. baby names

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.

Throughout this tutorial, we will work with this data.

The dataset of U.S. baby names 6


Python 3 Basics Tutorial

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

Which to install, depends on your operating system.

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:

sudo apt-get install python3


sudo apt-get install ipython3

To check whether everything worked, type:

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.

Other Python distributions


Python 3 - the standard Python installation
Anaconda - a Python distribution with many pre-installed packages for scientific

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

First steps on the IPython Shell


There are two ways to use Python: The interactive mode or IPython shell and writing
programs. We will use the interactive mode to store data on U.S. baby names.

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:

How to store a number?


How to add two numbers together?
How to store text?
How to convert a number to text and back?

First steps on the IPython Shell 9


Python 3 Basics Tutorial

Using Python as a calculator

The Challenge: Boys' names total


In the table, you find the five most popular boys' names from the year 2000. What is the total
number of boys in the top5?

name number

Jacob 34465
Michael 32025
Matthew 28569
Joshua 27531
Christopher 24928

Using Python as a calculator 10


Python 3 Basics Tutorial

Warming up
Enter Python in the interactive mode. You should see a message

In [1]:

Complete the following calculations by filling in the blanks:

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]:

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.

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.

The Challenge: Calculate an average


What is the average number of the top five boy names?

Calculate the number and store it in a variable.

Make a very rough estimate before you do the calculation.

Storing numbers 15
Python 3 Basics Tutorial

Warming up
Let's define some variables:

In [1]: emily = 25952


In [2]: hannah = 23073
In [3]: khaleesi = 5
In [4]: emily
Out[4]: ______
In [5]: hannah + 1
Out[5]: ______
In [6]: 3 * khaleesi
Out[6]: ______

Let's change the content:

In [7]: emily = emily + 1


In [8]: emily
Out[8]: _____

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

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

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

In [1]: emily = 25952


In [2]: emily = 25953
In [3]: emily
Out[3]: ______

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

Definitions 17
Python 3 Basics Tutorial

Print the number from a variable on the screen

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

last name O'Malley


gender M
year of birth 2000

Write the information from each row of the table into a separate string variable, then
combine them to a single string, e.g.:

O'Malley, Andrew, M, 2000

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 = first + " " + last


name

What do the following statements do:

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.

The following three statements are equivalent:

name = first + last


name = first + "Smith"
name = "Emily" + "Smith"

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?

. 7 @ ? / & * \ Ä ß " ' á

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

Converting numbers to text and back


Now you know how to store numbers and how to store text. Being able to convert the two
into each other will come in handy. For that, we will use type conversions.

The Challenge: A data record with types


field value type

first name Andrew string


last name O'Malley string
gender M string
year of birth 2000 integer
age 15 integer

Write the values from each row of the table into string or integer variables, then combine
them to a single one-line string.

Converting numbers to text and back 25


Python 3 Basics Tutorial

Warming up
Now we are going to combine strings with integer numbers.

name = 'Emily Smith'


born = _____
____ = '15'

text = ____ + ' was born in the year ' + _____


year = born + _____
text
year

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

Writing Python programs


Using the interactive IPython alone is exciting only for a while. To write more complex
programs, you need to store your instructions in programs, so that you can execute them
later.

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:

How to write to the screen.


How to read from the keyboard.
How to repeat instructions.
How to store many items in a list.
How to make decisions.

Writing Python programs 29


Python 3 Basics Tutorial

Writing to the screen


In this section, you will write your first Python program. It will be the most simple Python
program possible. We simply will have the program write names of babies to the screen.

The Challenge: Write a table with names


Many names of fictious characters that have been used as baby names. Write a program
that produces an output similar to:

Harry Hermione Severus


Tyrion Daenerys Snow
Luke Leia Darth

Extra challenges:
Use a single print statement to produce the output.
Store the names in separate variables first, then combine them.
Use string formatting.

Writing to the screen 30


Python 3 Basics Tutorial

Warming up
Open a text editor window (not a Python console). Type:

print("Hannah")
print(23073)

Now save the text to a file with the name first_program.py .

Then run the program.

In Canopy, you do this by pressing the 'play' button.


In IDLE, you can execute a program by pressing F5.
On Unix, you go open a terminal (a regular one, not IPython) and write:

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.

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

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.

s = 'Result: %i' % (77)


print('Hello %s!' % ('Roger')
a = 5.099999
b = 2.333333
print('(%6.3f/%6.3f)' % (a, b)

The formatting characters include:

%i – an integer.

%4i – an integer formatted to length 4.

%6.2f – a float number with 6 digits (2 after the dot).

%10s – a right-oriented string with length 10.

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

Reading from the keyboard


Next, we will connect the keyboard to our program.

The Challenge: Enter a baby name


Write a program that asks for a name and an age, then writes a sentence with the entered
data:

Bobby is 3 years old.

Extra challenge:
Add 1 to the age entered.

Reading from the keyboard 35


Python 3 Basics Tutorial

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.

The Challenge: count characters


Write a program that calculates the number of characters in Emily Smith .

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

Loops with for


The for loop allows you to repeat one or more instructions. It requires a sequence of items
that the loop iterates over. This can be e.g. a string.

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 char in 'ABCD':


print(char)

for i in range(3):
print(i)

for elem in [1,2,3,4]:


print(elem)

When to use for?


When you want repeat an operation

When you want to do something to all


characters of a string.
When you want to do something to all elements of a list.
When you already know the number of iterations.

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.

You can see the complete list with the commands:

Definitions 41
Python 3 Basics Tutorial

import keyword
keyword.kwlist

Code blocks and indentation


The line with the for statement, a code block starts. A code block contains one or more
line that belong to the for and are executed within the loop. Python recognizes this code
block by indentation, meaning that each line starts with four extra spaces.

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.

Code blocks in the IPython shell


Define code blocks by indenting extra lines:

In [1] for i in range(3):


... print(i)
...
0
1
2
In [2]

Definitions 42
Python 3 Basics Tutorial

Exercises

Exercise 1
Which of these for commands are correct?

[ ] for char in "ABCD":


[ ] for i in range(10):
[ ] for num in (4, 6, 8):
[ ] for k in 3+7:
[ ] for (i=0; i<10; i++):
[ ] for var in seq:

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

Storing lists of items


To handle larger amounts of data, we cannot invent a new variable for every new data item.
Somehow we need to put more data into one variable. This is where Python lists come in.

The Challenge: Percentage of top 10 names


For 2000, a total of 1962406 boys were registered born in the U.S. What percentage of that
is made by the top 10 names?

Write a program to calculate that number.

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

gives a different result than

3.0 / 4

Storing lists of items 45


Python 3 Basics Tutorial

However, Python 3 automatically creates the decimal places if needed.

Storing lists of items 46


Python 3 Basics Tutorial

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:

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:]

You can use slicing to create a copy:

Definitions 48
Python 3 Basics Tutorial

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

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.

The Challenge: Filter a list


You have a list of the top 20 girls names from 2000:

['Emily', 'Hannah', 'Madison', 'Ashley', 'Sarah',


'Alexis', 'Samantha', 'Jessica', 'Elizabeth', 'Taylor',
'Lauren', 'Alyssa', 'Kayla', 'Abigail', 'Brianna',
'Olivia', 'Emma', 'Megan', 'Grace', 'Victoria']

Write a program that prints all names that start with an A .

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:

movie = input('Please enter your favourite movie: ')


if movie == 'Star Trek':
print('Live long and prosper')
elif movie == 'Star Wars':
print('These are not the droids you are looking for')
elif movie == 'Dirty Dancing':
print('I fetched the melons')
else:
print("Sorry, I don't know the movie %s." % (movie) )

Warming up 53
Python 3 Basics Tutorial

Definitions

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 for or 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

Definitions 54
Python 3 Basics Tutorial

On lists and strings you can also use:

a in b

Multiple expressions can be combined with boolean logic:

a or b
a and b
not a
(a or b) and (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

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.

data = [1, 2, 5, 10, 20, 100, 200, 500, 1000]


query = int(input())

Exercises 56
Python 3 Basics Tutorial

Reading and writing files


In this section, you will learn to read and write files and to extract information from them.

Goals
read text files
write text files
extract information from a file
list all files in a directory

The Dataset of Baby Names


For the next exercises, you will need the complete archive of baby names. You can
download the files from http://www.ssa.gov/oact/babynames/limits.html.

Reading and writing files 57


Python 3 Basics Tutorial

Reading text files

The Challenge
Write a program that counts the number of names in the file yob2014.txt from the dataset
of baby names.

Reading a text file 58


Python 3 Basics Tutorial

Warming up
Match the descriptions with the Python commands.

Warming up 59
Python 3 Basics Tutorial

Definitions

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:

f = open('my_file.txt')
for line in f:
...

Alternatively, you can write both commands in one line:

for line in open('my_file.txt')

Reading a file to a string


You can read the entire content of a file to a single string:

f = open('my_file.txt')
text = f.read()

Writing directory names in Python


When opening files, you often need to specify a directory name as well. You can use both
full or relative directory names.

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

Closing files in Python is not mandatory but good practice.

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

Writing text files

The Challenge: Write a table


Given is the following data:

names = ["Emma", "Olivia", "Sophia", "Isabella",


"Ava", "Mia", "Emily"]
numbers = [20799, 19674, 18490, 16950,
15586, 13442, 12562]

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.

Writing a text file 63


Python 3 Basics Tutorial

Warming up
Execute the following program. Explain what happens.

names = ['Adam', 'Bob', 'Charlie']

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

Opening a file for writing


Writing text to files is very similar to reading. The only difference is the 'w' parameter.

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:

lines = ['first line\n', 'second line\n']


open('my_file.txt','w').writelines(lines)

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?

[ ] for line in open(filename):


[ ] f = open(filename, 'w')
[ ] open(filename).writelines(out)
[ ] f.close()

Exercises 66
Python 3 Basics Tutorial

Extracting information from a file


Rarely you can use the information from a file directly. Most of the time you will need to
parse it, that is extracting relevant information.

Parsing means for instance:

dividing tables into columns


converting numbers to integers or floats.
discarding unnecessary lines.

The Challenge: Find Your name


Write a program that finds out how often your name occurs in the list of 2014 baby names
and print that number.

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

Save the file with the name alice.txt .

Then run the following program:

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

The strip/split pattern


The two most frequently written lines you need in Python are probably:

for line in open(filename): columns = line.strip('\n').split(sep) ...

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

text = " this is text "


s = text.split()

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)

["this", "is", "text"]

Definitions 69
Python 3 Basics Tutorial

Both functions perfectly complement each other.

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

Working with directories


To process bigger amounts of data, you will need to work on more than one file. Sometimes
you don't know all the files in advance.

The Challenge: count the files


Write a program that counts the number of files in the unzipped set of baby names. Have the
program print that number.

Verify that the number is correct.

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.

Working with directories 72


Python 3 Basics Tutorial

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 memory all the time. This is why we need to import it.

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

Check whether a file exists

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

Using external modules 76


Python 3 Basics Tutorial

Introspection

The Challenge: a Baby name generator


Write a program that will generate a random baby name from a list of possible names.

Use the Python module random

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)

What does the code do?

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.

Exploring the namespace


In Python classes, functions, modules etc. have separate namespaces. A namespace is the
set of attributes of an object. You can explore a namespace with dir() :

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.

Each object has a name:


print (x.__name__)

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.

Standard modules and libraries 80


Python 3 Basics Tutorial

re

What is re?
re is a Python module for Regular Expressions. Regular expressions help you to search

and replace text using sophisticated patterns.

Searching
import re

text = 'all ways lead to Rome'

# find one occurence


print(re.search('R...\s', text))

# find every occurence


print(re.findall('\s(.o)', text))

Replacing
re.sub('R[meo]+', 'London', text)

How to find the right pattern for your problem:


Finding the right RegEx requires lots of trial-and-error search. You can test regular
expressions online before including them into your program: www.regexplanet.com/simple/

Characters used in RegEx patterns:


Some of the most commonly used characters in Regular Expression patterns are:

re 81
Python 3 Basics Tutorial

\d - decimal character [0..9]


\w - alphanumeric [a..z] or [0..9]
\A - start of the text
\Z - end of the text
[ABC] - one of characters A,B,C
. - any character
^A - not A
a+ - one or more of pattern a
a* - zero or more of pattern a
a|b - either pattern a or b matches
\s - empty space

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:

"In the class were three girls: Thelma, Emma and


Sophia. Also three boys: Emmett and his best friends
Sophocles and Theophilius"

Exercise 2
Reduce the number of patterns you need to three.

Exercise 3
Find all six names using a single pattern.

Where to learn more about Regular


Expressions?
see [www.regexone.com](http://www.regexone.com)

re 82
Python 3 Basics Tutorial

xml

What is xml?
xml is a Python module for reading XML documents.

The XML documents are parsed to a tree-like structure of Python objects.

Exercises
Exercise 1
Store the following in an XML document:

xml 83
Python 3 Basics Tutorial

<?xml version="1.0" encoding="UTF-8" ?>


<movieml name="XML example for teaching" author="Kristian Rother">

<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

from xml.dom.minidom import parse

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

time and datetime


The time module offers functions for getting the current time and date.

import time
s = time.asctime()
i = time.time()

The datetime module helps you to represent dates and format them nicely:

date = datetime.date(2015, 12, 24)


date.strftime("%d.%m.%Y")

Dates can be converted to integer numbers:

date = datetime.date(2015, 12, 24)


number = date.toordinal()

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)

Options of CSV file readers/writers


Both CSV readers and writers can handle a lot of different formats. Options you can change
include:

delimiter : the symbol separating columns.


quotechar : the symbol used to quote strings.
lineterminator : the symbol at the end of lines.

reader = csv.reader(open('my_file.csv'), delimiter='\t', quotechar='"')

Do I really need this?


Parsing a file with the common pattern

for line in open('my_file.csv'):


row = line.strip().split(',')

csv 88
Python 3 Basics Tutorial

is almost as easy. It's your decision which you like more.

csv 89
Python 3 Basics Tutorial

Installable Python modules


There are more than 50,000 extra modules/packages for Python available. Here, you will get
to see some of the usual suspects.

pip
pip is a program designed to make installing modules easy.

Modules not covered here yet


numpy
RPy2
biopython

Installable modules and libraries 90


Python 3 Basics Tutorial

pandas

What is pandas?
pandas is a Python library for efficient handling of tables.

Installation
Write in the command line:

`pip install pandas`

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.

tgirls = tgirls[tgirls['sum'] >= 1000]


tgirls.to_csv('girls_1000.csv')

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:

`pip install xlrd`

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:

`pip install scipy`

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

def func(x, a, b, c):


return a * np.exp(-(x-b)**2/(2*c**2))

x = np.linspace(0, 10, 100)


y = func(x, 1, 5, 2)
yn = y + .2 * np.random.laplace(size=len(x))

params, pcov = curve_fit (func, x , yn)


ys = func(x, params[0], params[1], params[2])

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:

from pylab import figure, plot, savefig

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:

from pylab import figure, title, pie, savefig

names = ['Emily', 'Hannah', 'Madison', 'Ashley', 'Sarah']


count = [25952,23073, 19967, 17995, 17687]
explode = [0.0, 0.0, 0.0, 0.05, 0.05]

colors = ["#f0f0f0", "#dddddd", "#bbbbbb", "#999999", "#444444"]

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:

from pylab import figure, title, xlabel, ylabel, xticks, bar, \


legend, axis, savefig

nucleotides = ["A", "G", "C", "U"]

counts = [
[606, 1024, 759, 398],
[762, 912, 639, 591],
]

figure()
title('RNA nucleotides in the ribosome')
xlabel('RNA')
ylabel('base count')

x1 = [2.0, 4.0, 6.0, 8.0]


x2 = [x - 0.5 for x in x1]

xticks(x1, nucleotides)

bar(x1, counts[1], width=0.5, color="#cccccc", label="E.coli 23S")


bar(x2, counts[0], width=0.5, color="#808080", label="T.thermophilus 23S")

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.

Where to learn more?


You can find example diagrams and code in the matplotlib gallery on
matplotlib.org/gallery.html.

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

Where to learn more?


pillow.readthedocs.org

Exercises
Exercise 1
Execute a program for converting an image format.

from PIL import Image

carrot = Image.open('carrot.png')
img.save('carrot.jpg')

pillow 99
Python 3 Basics Tutorial

Exercise 2
Execute a program to merge two images.

from PIL import Image

img = Image.new('RGBA', (800, 500), 'white')

carrot = Image.open('carrot.png')
brain = Image.open('brain.png')

img.paste(carrot, (100, 80))


img.paste(brain, (500, 100))

img.save('step01.png')

pillow 100
Python 3 Basics Tutorial

Exercise 3
Execute the program to resize an image.

from PIL import Image

img = Image.new('RGBA', (800, 500), "white")

carrot = Image.open('carrot.png')
brain = Image.open('brain.png')

carrot = carrot.resize((250, 250))

img.paste(carrot, (100, 80))


img.paste(brain, (500, 100))

img.save('step02.png')

pillow 101
Python 3 Basics Tutorial

Exercise 4
Execute the program to draw an arrow

from PIL import Image, ImageDraw

arrow = Image.new('RGBA', (250, 250), "white")

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.

from PIL import Image, ImageDraw

gradient = Image.new('RGBA', (250, 250), "white")


draw = ImageDraw.Draw(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

from PIL import Image, ImageChops

arrow = Image.open('arrow.png')
gradient = Image.open('gradient.png')

arrow = ImageChops.screen(arrow, gradient)

img.save('grad_arrow.png')

pillow 103
Python 3 Basics Tutorial

More Filter Examples:

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

from PIL import ImageFont

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

1. Cuts a picture into many small images


2. Moves the pieces randomly
3. Writes out frames after each move
4. Call MEncoder (on Windows)

Calling MEncoder:

mencoder "mf://*.png" -mf fps=25 -o output.avi


-ovc lavc -lavcopts vcodec=mpeg4

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

Overview of Data types in Python

Data types
Match the data samples with their types.

Definitions

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 )

Overview of Data types in Python 107


Python 3 Basics Tutorial

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

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

Overview of Data types in Python 108


Python 3 Basics Tutorial

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
}

Accessing elements in dictionaries


By applying square brackets with a key inside, the values of a dictionary can be requested.
Valid types for keys are strings, integers, floats, and tuples.

print prices['banana'] # 0.75


print prices['kiwi'] # KeyError!

Looping over a dictionary:


You can access the keys of a dictionary in a for loop. However, their order is not
guaranteed, then.

for fruit in prices:


print fruit

Methods of dictionaries
There is a number of functions that can be used on every dictionary:

Checking whether a key exists:

prices.has_key('apple')

Retrieving values in a fail-safe way:

prices.get('banana')
prices.get('kiwi')

Setting values only if they dont exist yet:

Dictionaries 110
Python 3 Basics Tutorial

prices.setdefault('kiwi', 0.99)
prices.setdefault('banana', 0.99)
# for 'banana', nothing happens

Getting all keys / values:

print prices.keys()
print prices.values()
print prices.items()

Exercises
Exercise 1.
What do the following commands produce?

d = {1:'A', 'B':1, 'A':True}


print(d['A'])

[ ] False
[ ] "B"
[ ] True
[ ] 1

Exercise 2.
What do these commands produce?

d = {1:'A', 'B':1, 'A':True}


print(d.has_key('B'))

[ ] 1
[ ] True
[ ] "B"
[ ] False

Exercise 3.
What do these commands produce?

Dictionaries 111
Python 3 Basics Tutorial

d = {1:'A', 'B':1, 'A':True}


print(d.values())

[ ] True
[ ] ['A', 1, True]
[ ] 3
[ ] [1, 'B', 'A']

Exercise 4.
What do these commands produce?

d = {1:'A', 'B':1, 'A':True}


print(d.keys())

[ ] [1, 'B', 'A']


[ ] ['A', 'B', 1]
[ ] [1, 'A', 'B']
[ ] The order may vary

Exercise 5.
What do these commands produce?

d = {1:'A', 'B':1, 'A':True}


print(d['C'])

[ ] None
[ ] 'C'
[ ] an Error
[ ] False

Exercise 6.
What do these commands produce?

d = {1:'A', 'B':1, 'A':True}


d.setdefault('C', 3)
print(d['C'])

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

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

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?

[ ] group data of different kind


[ ] change the values in them
[ ] run a for loop over them
[ ] sort them

Tuples 114
Python 3 Basics Tutorial

Control flow statements


Control flow means controlling, which instruction is handled next. In this tutorial, we cover
three of Pythons control flow statements: for , if and while .

Conditional loops with while

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)

When to use while?


When there is an exit condition.

while 115
Python 3 Basics Tutorial

When it may happen that nothing is done at all.


When the number of repeats depends on user input.
Searching for a particular element in a list.

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 is also called a conditional loop


[ ] The expression after while may contain function calls
[ ] It is possible to write endless while loops
[ ] The colon after while may be omitted
[ ] The code block after while is executed at least once

while 116
Python 3 Basics Tutorial

Built-in functions

Examples

Determining the length of sequences


The len() functions returns an integer with the length of an argument. It works with strings,
lists, tuples, and dictionaries.

data = [0,1,2,3]
len(data)

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.

data = range(4) # [0,1,2,3]


data = range(1,5) # [1,2,3,4]
data = range(2,9,2) # [2,4,6,8]
data = range(5,0,-1) # [5,4,3,2,1]

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)

Enumerating elements of lists


The enumerate() function associates an integer number starting from zero to each element
in a list. This is helpful in loops where an index variable is required.

List functions 117


Python 3 Basics Tutorial

fruits = ['apple', 'banana', 'orange']


for i, fruit in enumerate(fruits):
print(i, fruit)

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)

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.

List functions 118


Python 3 Basics Tutorial

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

Structuring bigger programs 119


Python 3 Basics Tutorial

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!

def calc_price(fruit, n=1):


..

print(calc_prices('banana'))
print(calc_prices('banana',100))

List and keyword parameters


You can add a list *args for an unspecified number of extra parameters, or a dictionary
**kwargs for keyword parameters.

def func(&#42;args, &#42;&#42;kwargs):


print(args, kwargs)

func(1, 2, a=3, b=4)

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

return 0.75

Functions 120
Python 3 Basics Tutorial

Multiple values are returned as a tuple.

return 'banana', 0.75

In any case, the return statement ends the execution of a function.

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

Where does Python look for modules and


packages?
When importing modules or packages, their directory needs to be in the Python search path.

Python looks for modules and packages in:

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

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?

[ ] in the variable sys.path


[ ] in the current working directory
[ ] in the directory where the current module is
[ ] in the site-packages folder
[ ] in directories in the PYTHONPATH variable

Exercise 4

Packages 124
Python 3 Basics Tutorial

Which statements about packages are true?

[ ] a package is a directory with modules


[ ] a package must contain a file __init__.py
[ ] a package may contain no code
[ ] a module may contain many packages

Exercise 5
Which packages are installed by default?

[ ] os - manipulating files and directories


[ ] time - accessing date and time
[ ] csv - read and write tables
[ ] numpy - number crunching
[ ] pandas - clever handling of tabular data

Packages 125
Python 3 Basics Tutorial

Background information on Python 3

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

What has changed from Python 2 to Python 3?


print is now a function
all Strings are stored in Unicode (better handling of umlauts and special characters in all
languages)
many functions like zip(), map() and filter() return iterators
the standard library has been cleaned up completely

Background information on Python 3 126


Python 3 Basics Tutorial

Recommended books and websites

Free Online Books


Books for beginners and people with programming experience:

Learn Python the Hard Way - a bootcamp-style tutorial by Zed Shaw


How to think like a Computer Scientist - a very systematic, scientific tutorial by Allen B.
Downey
Dive into Python 3 - explains one sophisticated program per chapter - by Mark Pilgrim

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

Recommended books and websites 127


Python 3 Basics Tutorial

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

Potrebbero piacerti anche