Exercises

These exercises check the first running-code habits. Keep the programs small. The goal is to predict, run, inspect, and fix.

Exercise: Code and output

In what order does this program print the words?

Order

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
Exercise: Python version

Which commands check for Python 3 in this chapter?

Choose every version check

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.

Exercise: Fix the missing name

Run the code and fix it so the output contains loss: 0.5.

Define before use

Runs locally with Python in your browser.

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.

Exercise: Interactive prompt

Should this line be copied into a .py script exactly as shown?

>>> print("hello")
Answer

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")
Exercise: One controlled edit

Edit only one number so the output contains triple: 21.

Controlled edit

Runs locally with Python in your browser.

Ready to run.

Hint

The program computes value * 3. What value gives 21?

Solution

Use value = 7:

The output is:

triple: 21