The Python Challenge Solutions
The Python Challenge Solutions
of Contents
Introduction
1.1
1.2
Installing Python
1.3
1.4
1.4.1
Storing numbers
1.4.2
Storing text
1.4.3
1.4.4
1.5
1.5.1
1.5.2
1.6
1.6.1
Repeating instructions
1.6.2
1.6.3
Making decisions
1.6.4
Extracting data
1.7
1.7.1
1.7.2
Writing files
1.8
1.8.1
1.8.2
1.9
Creating tables
1.9.1
Shortcuts
1.9.2
1.10
Dictionaries
1.10.1
Tuples
1.10.2
1.10.3
Structuring programs
1.11
while
1.11.1
1.11.2
Modules
1.11.3
Introspection
1.11.4
1.12
1.13
Acknowledgements
1.14
Introduction
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.
Installing Python
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
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 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
Installing Python
Question 2
Which Python version are you running?
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?
Enter the commands to see what happens (do not type the first part In [1] etc., these will appear automatically).
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
number
Jacob
34465
Michael
32025
Matthew
28569
Joshua
27531
Christopher
24928
10
Storing numbers
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.
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]: ______
Insert the correct values and variable names into the blanks.
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
11
Storing numbers
[ ] seven = 3 * 4
12
Storing text
Storing text
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
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
13
Storing text
The Challenge
first name
Andrew
last name
O'Malley
gender
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
14
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?
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:
15
12345678901234567890123456789012345678901234567890
value
type
first name
Andrew
string
last name
O'Malley
string
gender
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.
16
Goals
The new Python commands cover:
How to write to the screen.
How to read from the keyboard.
17
Warming up
Open a text editor window (not a Python console). Type:
print("Hannah")
print(23073)
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)
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.
19
Warming up
What happens when you write the follwing lines in the IPython shell:
In [1]: a = input()
In [2]: a
Exercise 1
Which input statements are correct?
[ ] a = input()
[ ] a = input("enter a number")
[ ] a = input(enter your name)
[ ] a = input(3)
Extra challenge:
Add 1 to the age entered.
20
New concepts
reading files with open()
loops with for
lists
making decisions with if
21
Exercise 1:
Make the program work by inserting close , line , bigbang.txt , print into the gaps.
f = open(___)
for ____ in f:
____(line)
f.____()
Hint:
Depending on your editor, you may need to insert the complete path to the file. If the program does not work, a wrong file
name or location are by far the most probable reasons.
Exercise 2
Write a program that calculates the total number of names in the file bigbang.txt from the dataset of baby names.
You will need to use a counter variable. The follwing two lines will be useful:
names = 0
and
names = names + 1
Exercise 3
Write a program that reads a file bigbang_numbers.txt that contains numbers only and calculates their sum. For the file,
use the following data:
22
12562
2178
342
129
384
208
164
82
41
23
Repeating instructions
Repeating instructions
In our early programs, each Python instruction was executed only once. That makes programming a bit pointless, because
our programs are limited by our typing speed.
In this section we will take a closer look at the for statement that repeats one or more instructions several times.
Exercise 1
What does the following program do?
for number in range(42):
print(number)
Exercise 2
Write a for loop that creates the following output
1
4
9
16
25
36
49
Exercise 3
Explain the difference between the following two programs:
total = 0
for number in range(10):
total = total + number
print(total)
and
total = 0
for number in range(10):
total = total + number
print(total)
Exercise 4
24
Repeating instructions
Exercise 5
Write a for loop that creates the following string:
000111222333444555666777888999
Exercise 6
Which of these for commands are correct?
for char in "ABCD":
for i in range(10):
for number in [4, 6, 8]:
for k in 3+7:
for (i=0; i<10; i++):
for var in open('bigbang.txt'):
Exercise 7
Read the file bigbang_numbers.txt and calculate the average and standard deviation. Use the code sniplet:
import math
root = math.sqrt(value)
Exercise 8
What does the following program do?
text = ""
characters = "Hannah"
for char in characters:
text = char + text
print(text)
Exercise 9
Write a program that calculates the number of characters in Emily Smith .
Exercise 10
Write a program that duplicates each character of a string, so that Emily becomes EEmmiillyy .
25
Exercise 1
Find out what each of the expressions does to the list in the center.
Exercise 2
What does list b contain?
a = [8, 7, 6, 5, 4]
b = a[2:4]
[7, 6, 5]
[7, 6]
[6, 5]
[6, 5, 4]
Exercise 3
In the year 2000, a total of 1962406 boys were registered born in the U.S. What percentage of the total names do the top 5
names consist?
You have the following list of the top 10 names:
26
Write a program that extracts the top 5 names from the list and then calculates the percentage on the 1962406 total births.
Hint
If you are using Python2, you need to specify whether you want numbers with decimal places when dividing. That means
3 / 4
Exercise 4
Use the expressions to modify the list as indicated. Use each expression once.
Exercise 5
Use the expressions to modify the list as indicated. Use each expression once.
27
Exercise 6
You have the following two lists:
top_ten_names = ['Jacob', 'Michael', 'Matthew', 'Joshua', \
'Christopher', 'Nicholas', 'Andrew', 'Joseph', \
'Daniel', 'Tyler']
top_ten_boys = [34465, 32025, 28569, 27531, \
24928, 23632, 22818, 22307, 21500]
Print a table to the screen with all names in the left column and all numbers in the right column.
Exercise 7
Create a list that contains the sum of California and New York for each name.
# Emily, Amy, Penny, Bernadette (2014)
california = [2269, 542, 54, 21]
new_york = [881, 179, 12, 11]
Exercise 8
Read all lines from the file yob2014.txt into a list. Print the first 10 and the last 10 lines to the screen.
28
Making decisions
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.
Exercise 1
Execute the following program. What does it calculate?
boys = 0
girls = 0
for line in open('yob2014.txt'):
if ",M," in line:
boys = boys + 1
elif ",F," in line:
girls = girls + 1
else:
print("unrecognized line:", line)
print(boys, girls)
Exercise 2
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 3
Write a program that lets the user enter a name on the keyboard. Find the line(s) containing that name in the file
yob2014.txt and write them to the screen.
Exercise 4
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']
Exercise 5
How many baby names are there in 1900 beginning with M ?
29
Making decisions
Exercise 6
How many different girls names starting with an M were there in 2014?
30
Exercise 2
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?
Exercise 3
The following program writes names used for both boys and girls into a file.
Complete the part dissecting the line into the variables name and gender .
31
girls = []
duplicates = []
for line in open('names/yob2014.txt'):
# add your code here
if gender == 'F':
girls.append(name)
elif gender == 'M':
if name in girls:
# found a duplicate name!
duplicates.append(name)
Exercise 4
Which are correct commands to work with files?
for line in open(filename):
f = open(filename, 'w')
open(filename).writelines(out)
f.close()
Exercise 5
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 data 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.
32
Exercise 2
Write a program that does the following:
The program asks the user for a name.
The program reads the file yob2000.txt .
The program prints all lines that contain the entered name.
Exercise 3
Extend the program to ask for the gender as well. Print only lines that match the gender.
Exercise 4
Extend the program to read all files from 1880 to 2014 and print all lines matching the name.
Exercise 5
Replace the input command by a variable (re-entering the same name over and over should get annoying by now).
Exercise 6
Collect the numbers for each year in a list. Write the resulting list of numbers to an output file.
Exercise 7
If the name was not found for a given year, use a 0 by default.
Exercise 8
33
Instead of matching single names, compare to a list of names (Big Bang or GoT characters).
34
In this part, you find exercises to handle two-dimensional lists (also called nested lists) conveniently.
35
Creating tables
Tables
Exercise 1:
Create an empty table of 10 x 10 cells.
Exercise 2:
Fill the table with the numbers from 1 to 100.
Exercise 3:
Save the table to a file.
Exercise 4:
Calculate the average number from the count column for a file with baby names for the year 2000 and print it.
Exercise 5:
Calculate the standard deviation as well.
Exercise 6:
Calculate how many girls' names and boys' names are there in total in 1900 and in 2000.
36
Shortcuts
Shortcuts
Exercise 1
Simplify the following code using one of the functions enumerate(), sum(), range(), zip() :
counts = [356, 412, 127, 8, 32]
total = 0
for number in data:
total = total + number
print(total)
Exercise 2
Simplify the following code using one of the functions enumerate(), sum(), range(), zip() :
i = 0
while i < 10:
print(i * '*')
i += 1
Exercise 3
Simplify the following code using one of the functions enumerate(), sum(), range(), zip() :
names = ['Lilly', 'Lily', 'Leila', 'Lilja', 'Lillie']
counts = [356, 412, 127, 8, 32]
table = []
i = 0
while i < len(names):
row = (names[i], counts[i])
table.append(row)
i += 1
print(table)
Exercise 4
Simplify the following code using one of the functions enumerate(), sum(), range(), zip() :
names = ['Lilly', 'Lily', 'Leila', 'Lilja', 'Lillie']
i = 0
for name in names:
print(i, name)
i += 1
Exercise 5
Use range() to create the following lists:
[4, 7, 9, 12]
37
Shortcuts
Exercise 6
On which data types does the len() function work?
lists
dictionaries
strings
floats
sets
38
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 )
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]
39
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
40
Dictionaries
Dictionaries
Exercise 1
Find out what each of the expressions does to the dictionary in the center.
Exercise 2
What do the following commands produce?
d = {1:'A', 'B':1, 'A':True}
print(d['A'])
False
"B"
True
1
Exercise 3
What do these commands produce?
d = {1:'A', 'B':1, 'A':True}
print(d.has_key('B'))
41
Dictionaries
True
"B"
False
Exercise 4
What do these commands produce?
d = {1:'A', 'B':1, 'A':True}
print(d.values())
True
['A', 1, True]
3
[1, 'B', 'A']
Exercise 5
What do these commands produce?
d = {1:'A', 'B':1, 'A':True}
print(d.keys())
Exercise 6
What do these commands produce?
d = {1:'A', 'B':1, 'A':True}
print(d['C'])
None
'C'
an Error
False
Exercise 7
What do these commands produce?
d = {1:'A', 'B':1, 'A':True}
d.setdefault('C', 3)
print(d['C'])
3
'C'
None
42
Dictionaries
an Error
43
Tuples
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?
[ ] group data of different kind
[ ] change the values in them
[ ] run a for loop over them
[ ] sort them
44
Warming up
Fill in the gaps
Exercise 1
Explain the following code:
import os
for dirname in os.listdir('.'):
print(dirname)
Exercise 1
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.
45
Exercise 2
How many entries (lines) does the entire name dataset have?
Hint: Generate a message that tells you which file the program is reading.
Exercise 3
Write a program that finds the most frequently occuring name in each year and prints it.
The Challenge
Find and print your name and the according number in each of the files, so that you can see how the number changes over
time.
46
Structuring programs
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
47
Functions
Python 3.5 has 72 builtin functions. To start writing useful programs, knowing about 25 of them is sufficient. Many of these
functions are useful shortcuts that make your programs shorter.
48
Modules
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.
49
Introspection
Introspection
Warming up
Try the following on the interactive shell:
import random
dir(random)
help(random.choice)
name = random.choice(['Hannah', 'Emily', 'Sarah'])
type(name)
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.
50
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
51
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
52
Acknowledgements
Authors
2013 Kristian Rother ([email protected])
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
53