Exercises
These exercises check the core idea of the chapter: names refer to values, and types explain behavior.
What does the final line print?
Compute it first, then check your number.
Hint
Track the value of count after each assignment. The last assignment uses the
previous value.
Solution
Track the name:
The right side of the last assignment uses the current value, 3, so the new
value is 5.
Output:
5
What does this print?
print("2" + "3")
Answer it first, then check.
Hint
The quotes matter. These are text values, not numeric values.
Solution
Both values are strings:
So + joins them:
23
Edit the code so the output contains total: 9.
Use numbers, not strings
Ready to run.
Hint
Remove the quotes around the numbers.
Solution
One fix is:
Output:
total: 9
Removing the quotes changes the values from strings to integers.
What value does this expression produce?
3 == 4
Select one choice, then check.
Hint
== asks whether the two values are equal.
Solution
3 == 4 asks whether 3 and 4 have the same value. They do not, so the
expression produces:
False
What type name does this print?
print(type(0.5))
Answer it first, then check.
Hint
Python uses float for many decimal-number values.
Solution
0.5 is a floating-point number, so:
print(type(0.5))
prints:
<class 'float'>
The short type name is float.