Exercises for Lesson 2

Back to Lesson 2

Exercise 1: Operations on numbers and strings

For this exercise, predict the output and then type each of the following commands in VS Code. If anything happens that you don’t expect, ask about it! :)

1a: Operators on numbers

print(4)

print(5 + 3)

print(5.0 - 3.0)

print(5 * 3)

print(5 ** 3)

print(5 / 3)

print(5 // 3)

print(5 % 3)

1b: Operators on strings

print("5" + "3")

print("5" - "3")

print("5" - 3)

print("5" * "3")

print("5" * 3)

print("5" / "3")

1c: Combining strings and expressions

print("4 + 3 =", 4 + 3)

print("Three 4s:", 3 * "4")

print("a=", 4-3)

print("a=" + str(4-3))

Exercise 2: Keyword parameters of print

When you call print() in Python, in addition to the expressions you want printed, you can also provide “keyword parameters” by name to change how print behaves.

2a: the end keyword prameter

The end parameter (specified by name when you call print) says how to end a print statement. By default, this is with a “newline character” (specified in Python as '\n'), meaning that the next thing printed will be on the next line. Running this code:

print("cat")
print("dog")

produces this output:

cat
dog

What do you think happens if we instead change this to use end? Predict the output of the following, then try running it:

print("cat", end="!") # try changing "!" to something else
print("dog")

2b: the sep keyword parameter

Another keyword parameter of print is called sep. What do you think the sep pararmeter does? Hint: sep is short for separator. Try it out using the following print statement:

print("3", "1", "4", "1", "5", sep="!") # try changing "!" to something else

Back to Lesson 2

Exercise 3: Assignment statements

The following program computes the volume of a sphere given a radius. Note: This is updated from class so that it’s computing the volume correctly. :)

 1. # Get the radius from the user
 2. radius = input("What is the radius? ")
 3. radius = float(radius)
 4.
 5. # Compute the volume (4/3 * pi * r^3)
 6. volume = 4 / 3
 7. volume = volume * 3.14
 8. volume = volume * radius
 9. volume = volume * radius
10. volume = volume * radius
11. print("The volume is", volume, "(radius=" + str(radius) + ")")

Fill in a table like the following to keep track of the values of variables over time. You can assume the user types in 10, and it’s sufficient to write fractions rather than real numbers, so you could write 4/3 instead of 1.3333333333333333 below, and 4/3 * pi instead of 4.1866666666666665.

Line Name Value
2 radius "10"
3 radius 10.0
6 volume 1.3333333333333333
7

Back to Lesson 2

Exercise 4: Predicting loop outputs

Predict the output from the following code fragments:

4a: Regular loop sequences

a)  for i in range(5):
        print(i * i)

b)  for i in range(5):
        print(i, i % 2)

c)  for i in range(4):
        print("Hello", end=" ")

4b: Bounds testing range

d)  for i in range(0):
        print("Hola")

e)  for i in range(3, 4):
        print("你好")

f)  for i in range(3, 3):
        print("안녕하세요")

g)  for i in range(3, 2):
        print("🐉")

Back to Lesson 2