These exercises practice what the Module 1 reading explained. They aren’t graded. Work each one on paper or in your head first, then check your answer, and only then run the code to confirm it, in the interactive interpreter, a notebook cell, or a script. The answers are in the collapsed blocks; open one only after you have an answer of your own.
Worked example: predicting a script’s output¶
What does this script print?
print("Start")
10 + 5
print("Sum:", 10 + 5)
print("10 + 5")Line 1 prints the string
Start.Line 2 works out
15and throws it away: a script shows only whatprintshows.Line 3 gives
printa string and a calculation. It prints the string, a space, and the result:Sum: 15.Line 4 gives
printa string. The quotes make10 + 5text, so Python prints it as written.
So it prints:
Start
Sum: 15
10 + 5Exercise 1.1 (LO-1.1, LO-1.2, ★, about 10 minutes)¶
The first part is done for you. Predict what each script prints.
print("3 * 4")thenprint(3 * 4)prints3 * 4and then12: the first is a string, and the second is a calculation.print("a", "b")thenprint()thenprint("c")5 - 2thenprint(5 - 2 - 1)print("Area:", 3 * 4, "square meters")
Hint
Ask of each line: is it a print, and is each thing inside the print in quotes or not?
Answer
a b, then an empty line, thenc.Only
2. The first line’s value is thrown away, and5 - 2 - 1runs from left to right:3 - 1is2.Area: 12 square meters.printputs one space between each of its three things.
Exercise 1.2 (LO-1.1, ★, about 10 minutes)¶
For each job, choose the way to run Python that suits it best (the interactive interpreter, a notebook, or a script), and give one reason.
You want to check whether
10 / 4gives2or2.5.You are writing a program that prints a timetable, which a friend will run on their computer every week.
You are working through a lab, trying each step and reading the result before the next.
Answer
The interactive interpreter: it’s one quick question, and the interpreter shows the value without
print. (It gives2.5.)A script: it is kept in a file, can be run again every week, and can be given to someone else.
A notebook: each step’s code, its output, and your notes stay together, in order.
Worked example: the order of operations¶
What does print(2 + 10 / 5 * 2) print?
/and*come before+, and run from left to right: first10 / 5, which is2.0.Then
2.0 * 2, which is4.0.Then
2 + 4.0, which is6.0. The result has a decimal point because/made one.
It prints 6.0.
Exercise 1.3 (LO-1.2, ★, about 10 minutes)¶
Predict what each line prints. The first is partly done: finish it.
print(20 - 4 * 3):4 * 3runs first and gives12, so the result is ...print((20 - 4) * 3)print(20 / 4 / 5)print(1 + 2 * 3 - 4 / 2)
Answer
8, because20 - 12is8.48: the parentheses make the subtraction run first, and16 * 3is48.1.0: from left to right,20 / 4is5.0, and5.0 / 5is1.0.5.0:2 * 3is6and4 / 2is2.0; then from left to right,1 + 6is7, and7 - 2.0is5.0.
Exercise 1.4 (LO-1.2, ★★, about 15 minutes)¶
A cinema ticket costs 9.50 for an adult and 6.00 for a child. Write a script, tickets.py, that
prints a receipt for 2 adults and 3 children, in this shape:
Cinema tickets
Adults: 19.0
Children: 18.0
Total: 37.0Start it with a comment that says what it does, and let Python work out every amount. Then change it to 1 adult and 4 children, and check the new total by hand before you run it.
Hint
Each amount is a number of tickets times a price. The total is the two amounts added together; since multiplication comes before addition, it needs no parentheses.
Answer
# Prints a receipt for cinema tickets: 2 adults and 3 children.
print("Cinema tickets")
print("Adults:", 2 * 9.50)
print("Children:", 3 * 6.00)
print("Total:", 2 * 9.50 + 3 * 6.00)For 1 adult and 4 children, the total is 9.50 + 24.00, which prints as 33.5.
Exercise 1.5 (LO-1.2, ★, about 10 minutes)¶
Here is a script with comments. Which comment is useful, and which one should be deleted? Why?
# Prints how many hours a 6-week course takes at 7 hours a week.
print(6 * 7) # multiply 6 by 7Answer
The first comment is useful: it says what the program is for, which the code alone can’t say. The second only repeats what the code already shows, so it adds nothing and should go. A better comment for that line would say what the numbers mean, if the first comment didn’t already.
Worked example: reading an error message¶
A script prints this:
Distance: 42.195
Traceback (most recent call last):
File "/home/you/py101/race.py", line 2, in <module>
print("Distance in meters: " + 42.195 * 1000)
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
TypeError: can only concatenate str (not "float") to strThe last line says it’s a
TypeError:+was asked to join a string to afloat, Python’s name for a number with a decimal point.The line above names line 2 of
race.py, and the^points at the+.Line 1 ran: its output,
Distance: 42.195, came before the error.The fix gives
printtwo things:print("Distance in meters:", 42.195 * 1000).
Exercise 1.6 (LO-1.3, ★, about 10 minutes)¶
For each error message’s last line, say what kind of error it is, the most likely cause, and how to fix it.
NameError: name 'Print' is not defined. Did you mean: 'print'?SyntaxError: unterminated string literal (detected at line 4)NameError: name 'Goodbye' is not defined, for the lineprint(Goodbye)
Answer
A name error:
printwas written with a capital P, and Python treatsPrintas a different name. Writeprint.A syntax error: a string on line 4 is missing its closing quotation mark. Add it.
A name error: the text has no quotes, so Python looks for something named
Goodbye. Writeprint("Goodbye").
Exercise 1.7 (LO-1.3, ★★, about 15 minutes)¶
This script is meant to print three lines. Without running it, list every mistake, in the order Python will report them, and say for each whether any line prints before Python stops.
print("Line one")
print("Line two"
prnt("Line three")Hint
Python reads the whole file before it runs anything, so it reports one kind of error before any other.
Answer
First, a syntax error: the
(on line 2 is never closed. Python reportsSyntaxError: '(' was never closed, naming line 2, and no line prints, because Python couldn’t read the file.After you add the
), a name error on line 3:prntisn’t a name Python knows. This timeLine oneandLine twoprint first, because a name error stops the script only when Python reaches that line.After you fix
prnt, the script prints all three lines.
Exercise 1.8 (LO-1.1, LO-1.3, ★★, about 15 minutes)¶
In the terminal in VS Code, start the interactive interpreter and try each line below. For each,
write down whether Python shows a value, prints text, or shows an error, and what kind of error.
Then leave with exit().
100 / 8print(100 / 8)"100 / 8"100 + " pages"
Answer
It shows the value
12.5.It prints
12.5. On screen this looks the same as line 1; in a script, only this one would show anything.It shows the string’s value,
'100 / 8', with quotes: the interpreter shows a string as you would type it in code, whileprintshows only its text.A type error:
TypeError: unsupported operand type(s) for +: 'int' and 'str'. With the number first, the message is worded differently from the reading’s example, but it is the same problem:+can’t add a number and a string.