Expressions Worksheet
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.
= (4 * 2) + 1
x = 4 * (2 + 2 y
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.
= 2
x = x - 2
y # hint: add a print statement
= 2 + x
y print(x + y)
1
6
Problem 4
The program below should print:
9
4
= 6
x = 0 y
print(x * (2 + y))
= x // 2
y print(y)
9
4
Problem 5
= "a space at the end"
x = "makes a difference" y
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!
= "spacing"
x = "matters!" y
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
.
= 3 x
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?
= 3 three
print("is three the same as " + three + "?")
is three the same as 3?