2b. Prof. Rosenthal’s Review#

A review of Chapter 2: Variables and Statements, for students who have already studied the chapter. Use the two lists under Concepts covered as a checklist (click a list’s title to hide it), then work through the Questions, which go from easy to hard. Each question has a suggested answer under Answer. Try the question first, then open the Answer.

Run this page on Colab

Concepts covered#

Questions#

All the questions use only variables, arithmetic, strings, print and the math module.

The setup cell below imports math.

import math

Question 1 (easy): what is displayed?#

Predict what this code displays, then open the Answer to check.

x = 8
y = 2
print(x + y, x * y)
print(x / y)
print(x ** y)

Question 2 (easy): what is displayed?#

name = 'Ada'
age = 36
print('Name:', name)
print(name, 'is', age, 'years old')
print('name')

Question 3 (easy): what is displayed?#

word = 'ha'
laugh = word * 3
print(laugh, len(laugh))
print(word + '!' + word)

Question 4 (easy): what is displayed?#

What does this code display when it is run as a notebook cell?

price = 4
price * 2
price * 3

Question 5 (medium): what is displayed?#

x = 5
y = x + 1
x = 10
print(x, y)

Question 6 (medium): what is displayed?#

# print('one')
print('two')   # print('three')
print('four # five')

Question 7 (medium): what is displayed?#

import math
print(math.sqrt(16))
print(math.pow(2, 3))
print(2 ** 3)
print(round(math.pi, 2))

Question 8 (medium): what is displayed?#

print(int('101', 2))
print(int('11', 2) + int('11'))
print(round(2.71828, 1), round(2.71828))

Question 10 (medium): wrong calls#

math.pow(X, Y) raises X to the power Y. Two correct ways to call it:

import math
print(math.pow(5, 2))
25.0
import math
print(math.pow(2, 0.5))
1.4142135623730951

Each of the next three calls is wrong in a different way. Predict the error for each, then open the Answer to check.

Part a

import math
math.pow(2)

Part b

import math
math.pow(2, 3, 4)

Part c

import math
math.pow('2', 3)

Question 11 (medium): write code#

The variables hours, minutes and seconds have already been assigned. Write code that computes the total number of seconds, assigns it to a variable called total, and displays it.

Examples (each example assigns the variables; your code comes after them)

Run this cell to assign hours, minutes and seconds.

hours = 1
minutes = 2
seconds = 3
3723
hours = 0
minutes = 42
seconds = 42
2562

Question 12 (medium): write code with comments#

The variable km holds a distance in kilometers. Write code that displays the distance in miles, rounded to 2 decimal places, in the form shown below. A mile is 1.61 kilometers. Add a comment that says where the number 1.61 comes from.

Examples

Run this cell to assign km.

km = 10
10 km is 6.21 miles
km = 42.195
42.195 km is 26.21 miles

Question 13 (hard): what is displayed?#

a = 3
b = 5
a = a + b
b = a - b
a = a - b
print(a, b)

Question 14 (hard): syntax errors and runtime errors#

Each part has an error on its second line. Predict what each part displays before the error, and which error it shows.

Part a

print('start')
import maath
print('end')

Part b

print('start')
my score = 10
print('end')

Question 15 (hard): find the error#

This code should display the average of three test scores, which is 80.0. What does it display instead? What kind of error is it (syntax, runtime or semantic), and how do you fix it?

score1 = 90
score2 = 80
score3 = 70
average = score1 + score2 + score3 / 3
print(average)

Question 16 (hard): write code#

The variables price and quantity hold strings, as if a user had typed them, for example price = '19.99'. Write code that displays the total cost, price times quantity, rounded to 2 decimal places, in the form shown below.

Examples

Run this cell to assign price and quantity.

price = '19.99'
quantity = '3'
Total: 59.97
price = '2.5'
quantity = '4'
Total: 10.0

Question 17 (hard): write code#

The variable total holds a number of seconds (a whole number). Write code that displays it as hours, minutes and seconds, in the form shown below. Use integer division, //, which divides and rounds down to an integer (for example, 7 // 2 is 3).

Examples

Run this cell to assign total.

total = 3725
1 h 2 min 5 s
total = 7384
2 h 3 min 4 s

Summary and questions by Prof. Y. Rosenthal, based on Think Python: 3rd Edition by Allen B. Downey. Text license: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International