Exercises
These exercises check the first running-code habits. Keep the programs small. The goal is to predict, run, inspect, and fix.
In what order does this program print the words?
Select one choice, then check.
Hint
There are two print() calls. Each one prints one line.
Solution
The first print() runs before the second:
run
again
Which commands check for Python 3 in this chapter?
Select every choice that applies, then check.
Hint
A version check asks Python to print its version. It does not run a file or list files.
Solution
The chapter uses two main Python 3 version checks.
On macOS or Linux:
python3 --version
On Windows PowerShell:
py -3 --version
Some systems also support python --version, but you should check that it
prints Python 3 before using it for this course.
Run the code and fix it so the output contains loss: 0.5.
Define before use
Ready to run.
Hint
Python needs loss to have a value before this line runs:
print("loss:", loss)
Add an assignment above it.
Solution
One fix is:
The assignment gives loss a value before print() uses it.
Should this line be copied into a .py script exactly as shown?
>>> print("hello")
Select one choice, then check.
Hint
The >>> text is a prompt shown by Python. It is not part of the script.
Solution
No. In an interactive session, Python shows:
>>>
That marker is not part of a script. A script should contain:
print("hello")
Edit only one number so the output contains triple: 21.
Controlled edit
Ready to run.
Hint
The program computes value * 3. What value gives 21?
Solution
Use value = 7:
The output is:
triple: 21