Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Objectives: LO-1.1, LO-1.2, LO-1.3
Prerequisites: the Module 1 reading
Time: about 2 hours

In this lab you run Python in notebook cells, write script files and run them, print a receipt that Python works out for you, and fix programs that stop with an error. Each exercise ends with a check cell. Run it after your answer: it prints passed, or it stops with a message that says what is wrong. Until you answer an exercise, its check fails on purpose.

Run the cells in order, from the top, by clicking in each one and pressing Shift+Enter. If you change an earlier cell, run it again before the cells after it.

Setup

Run the next cell first. It prints the version of Python that runs this lab, which should be 3.12 or newer, and makes a folder named scratch beside the lab for the files you write.

It also defines two helpers, small pieces of code that the rest of the lab uses: run_script runs a script file the way the terminal would and shows what it prints, and output_of gives a check cell the script’s output to compare. You don’t need to understand their code yet; Module 5 teaches you to write your own. Nothing in this lab is random, so it needs no random seed.

1. Running code in cells

Run the next cell. It holds two print lines, so it shows two lines of output.

The next cell has no print. Before you run it, predict what it shows. Then run it.

A notebook cell shows the value of its last line only, so you saw 3.0 and not 3.5. A script would show neither, because a script shows only what it prints.

Exercise L1.1 (LO-1.1, ★, about 10 minutes)

For each of the three snippets below, decide what it shows when it is run as a script, and what it shows when it is the whole of one notebook cell. Don’t run them first; predict.

  • Snippet A: print(4 * 5)

  • Snippet B: 4 * 5

  • Snippet C: two lines, 4 * 5 and then print(4 + 5)

Write each answer as text between the quotes, as the lines in the next cell show. Write 20, 9, or nothing. The lines that start with a name and = give each answer a name, so the check cell can read it; Module 2 explains them properly.

Hint

A script shows only what print shows. A notebook cell shows what print shows, and also the value of its last line if that line is a calculation.

2. Writing and running a script

A cell that starts with %%writefile doesn’t run its code: it saves the rest of the cell as a file. The next cell saves a script named hello.py in the scratch folder, and the cell after it runs that script, as the terminal would. Run both.

The middle line worked out 5, but the script threw it away, so the script printed two lines.

Exercise L1.2 (LO-1.1, LO-1.2, ★, about 10 minutes)

Change the %%writefile cell below so that the script scratch/intro.py prints exactly these three lines:

Hello, world!
This is my first script.
2 + 2 = 4

The 4 on the third line must come from Python adding 2 + 2, not from typing 4 inside a string. Run the cell to save the file, then run the run_script cell after it to see the output, then run the check.

Hint

print puts a space between the things you give it, separated by commas. The third line is a string, "2 + 2 =", followed by a calculation.

Now run the same script two other ways, outside the notebook:

  1. In VS Code, open scratch/intro.py (it’s in your py101 folder, inside scratch), and click Run Python File, the triangle in the top-right corner.

  2. In VS Code’s terminal, in your py101 folder, run it by typing the path to Python and then the path to the file: .venv\Scripts\python scratch\intro.py on Windows, or .venv/bin/python scratch/intro.py on macOS and Linux.

Both should print the same three lines as the cell above.

3. Printing a receipt

A bakery sells tea at 1.80 a cup and muffins at 2.40 each. The script below should print a receipt for 2 cups of tea and 3 muffins, with Python doing every calculation.

Exercise L1.3 (LO-1.2, ★, about 15 minutes)

Finish the script so that it prints exactly these lines:

Bakery receipt
2 teas at 1.80: 3.6
3 muffins at 2.40: 7.199999999999999

Total: 10.799999999999999

Yes, Python really prints 7.199999999999999 for 3 * 2.40: computers store most decimal numbers very slightly wrong, and Module 2 explains why and how to print 7.20 instead. For now, let Python print what it works out.

Start the script with a comment that says what it does. Write each amount as a calculation, such as 2 * 1.80, never as a number you worked out yourself. The fourth line is empty.

Hint 1

print() with nothing between the parentheses prints an empty line.

Hint 2

The total is the cost of the teas plus the cost of the muffins: 2 * 1.80 + 3 * 2.40. Python multiplies before it adds, so this needs no parentheses.

4. The order of operations

Three readings of a river’s depth were 120, 135, and 150 centimeters. The next cell tries to print their average. Run it, and decide whether the answer is reasonable.

Exercise L1.4 (LO-1.2, ★, about 5 minutes)

An average must lie between the smallest and the largest reading, and 305.0 doesn’t. Write the correct calculation after average = in the next cell, so that it gives 135.0.

5. Reading error messages

Each of the next three scripts stops with an error. For each one, run the %%writefile cell and the run_script cell, and read the error message from the bottom up: what kind of error is it, and which line does it name? Then fix the script, save it again, and run it again.

Exercise L1.5 (LO-1.3, ★, about 10 minutes)

Run the next two cells, read the error, and fix the script so that it prints Welcome to the course! and then Let's begin. Then fill in the kind of error the original script had and the line number its message named.

Notice that the original script printed nothing at all, not even its first line. Python reads the whole file before it runs any of it, so a syntax error stops every line.

Exercise L1.6 (LO-1.3, ★, about 10 minutes)

This script is meant to print the number of minutes in a week. Run it, read the error, fix the script, and fill in the kind of error and the line number.

This time the first line printed before the error. Unlike a syntax error, a name error stops the script only when Python reaches the line that has it.

Exercise L1.7 (LO-1.3, ★★, about 10 minutes)

This script is meant to print Seconds in an hour: 3600. Run it, read the error, and fix the script without typing 3600 yourself: keep the calculation. Then fill in the kind of error and the line number.

Hint

+ can’t join a string and a number. print can show both if you give it them separately.

Exercise L1.8 (LO-1.3, ★★, about 15 minutes)

This script has two mistakes. Fix them one at a time: run it, fix the error the message names, and run it again until it prints these two lines:

Days in a leap year: 366
Hours in a leap year: 8784

Then answer: which error did Python report first? Write its kind.

6. The interactive interpreter

The last way to run Python is the interactive interpreter. It can’t run inside a notebook, so this part has no check cell. In VS Code’s terminal, in your py101 folder:

  1. Start the interpreter: .venv\Scripts\python on Windows, or .venv/bin/python on macOS and Linux. Python prints its version, then the >>> prompt.

  2. Type 7 / 2 and press Enter, then 2 + 3 * 4, then (2 + 3) * 4. Python shows each value without print.

  3. Type prnt("hi") and read the error message. The interpreter shows the same kinds of error as a script, then gives you the prompt back.

  4. Type exit() to leave.

Summary

In this lab you:

  • saw that a notebook cell shows the value of its last line, and a script only what it prints;

  • wrote scripts and ran them from the notebook, from VS Code, and from the terminal;

  • let Python do a receipt’s arithmetic, and used parentheses to fix an average;

  • read error messages from the bottom up, and fixed a syntax error, a name error, and a type error; and

  • saw that a syntax error stops a whole script, while other errors stop it at their own line.

Next, the Module 1 practice gives you more exercises with answers, and Module 2 explains the values, the variables, and the strange decimals you met in this lab.