Exercises

These exercises check the core idea of the chapter: names refer to values, and types explain behavior.

Exercise: Track a name

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
Exercise: Number or string

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
Exercise: Fix the type surprise

Edit the code so the output contains total: 9.

Use numbers, not strings

Runs locally with Python in your browser.

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.

Exercise: Boolean comparison

What value does this expression produce?

3 == 4
Boolean

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
Exercise: Inspect the type

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.