Expressions Worksheet

Back to Week 2 Notes

Problem 1

The program below should result in a printed output of hello, world! – be careful, the correct output should have a trailing space!

print("hello,")
print("world! ")
hello, world! 

Problem 2

The program below should print:

12
21

Note that it includes several lines at the end that can’t be changed: modify the beginning of the program so that it results in the desired output.

x = (4 * 2) + 1
y = 4 * (2 + 2
print(y)
print(x + y)
12
21

Problem 3

The program below should print:

1
6

Note that it includes several lines at the end that can’t be changed: modify the beginning of the program so that it results in the desired output.

x = 2
y = x - 2
# hint: add a print statement
y = 2 + x
print(x + y)
1
6

Problem 4

The program below should print:

9
4
x = 6
y = 0
print(x * (2 + y))
y = x // 2
print(y)
9
4

Problem 5

x = "a space at the end"
y = "makes a difference"
print(x + y + z)
a space at the end
makes a difference 

Problem 6

The print statements in this program use the end = keyword argument to change what happens after the printed output:

  • Normally, printing goes onto the next line
  • Calling print with the end = changes this
  • Experiment and figure it out!
x = "spacing"
y = "matters!"
print(x * 5, end = " ")
print(y)
spacing      matters!

Problem 7

Remember how the + operator behaves differently depending on what it operates on! This should print 5.0.

x = 3
print(x + 2)
5.0

Problem 8

Remember how the + operator behaves differently depending on what it operates on!

The desired printed output is:

is three the same as 3?
three = 3
print("is three the same as " + three + "?")
is three the same as 3?

Back to Week 2 Notes