Python Quest

Track 1 · Lesson 2 of 11

How Python reads your code

Statements, top-to-bottom execution, comments, blank lines, capital letters and why spaces at the start of a line matter.

About 20 minFirst steps3 exercisesFree

You will be able to

  • Describe a program as a sequence of statements that run one at a time, top to bottom
  • Write comments that explain code, and use a comment to switch a line off
  • Explain why Python treats print and Print as different names
  • Recognise that spaces at the start of a line change the meaning of the code

Imagine handing a checklist to a new colleague who follows every instruction exactly as written, in order, and never guesses what you meant. That is how Python treats your program. It is very fast and completely literal.

Knowing exactly how Python reads a file saves you a lot of confusion later. In this lesson you'll see what a single instruction looks like, the order instructions run in, how to leave notes for human readers, and the few small details (capital letters, spaces at the start of a line) that Python is strict about.

One statement at a time

Each complete instruction in a program is called a statement. In the programs you'll write in this track, a statement is usually one line. print("Hello") is a statement.

When you press Run, Python starts at the first line, carries out that statement, then moves to the next one. It never skips ahead and never goes back on its own. If a statement fails, Python stops right there and reports an error, and the lines after it do not run.

Python stops at the first statement it cannot carry out
print("Line 1")
print("Line 2")
prnt("Line 3")
print("Line 4")

Try it: Run it and notice that Line 1 and Line 2 appear before the error. Then fix the spelling on line 3 and run it again.

The first two lines work, so their output appears. On line 3, Python finds the name prnt, which it does not know, and stops with a NameError. Line 4 never runs. You'll learn to read error messages properly in Reading error messages; for now, notice that the error tells you which line caused the problem.

Comments: notes for humans

A comment starts with #. Python ignores everything from the # to the end of that line. Comments are for people: they explain what the code is for, so that you (or a colleague) can understand it next week.

A comment can sit on its own line or at the end of a line of code.

Comments are ignored when the program runs
# Daily opening message for Palm Grove Library
print("Welcome!")  # shown at the entrance screen
# print("Closed for maintenance")
print("Open until 8 pm")

Try it: Delete the # at the start of line 3 and run it again. The line comes back to life.

Line 3 is a print statement with a # in front, so Python treats the whole line as a comment. Putting # in front of a line to switch it off without deleting it is called commenting out. It is handy when you want to try the program without a line but may want it back later.

A # inside quotes is ordinary text, not a comment: print("Room #4") prints Room #4.

Blank lines in code versus blank lines in output

Python ignores blank lines in your code. Use them to group related lines, the way paragraphs group sentences. They do not produce empty lines in the output.

To print an empty line, call print() with nothing between the parentheses.

Blank lines in code do nothing; print() prints an empty line
print("SHIFT ROTA")

print("Morning: Omar")
print()
print("Evening: Lina")

Try it: Remove the empty line after the first print. The output doesn't change. Then remove print() and see what does change.

Predict the output

# Delivery note
print("Order 318")

# print("Status: lost")
print("Status: packed")  # updated at 9:00
print()
print("Driver: Kareem")

Capital letters matter

Python is case sensitive: it treats uppercase and lowercase letters as different. print, Print and PRINT are three different names, and only print is built into Python. Writing Print("Hi") stops the program with a NameError, exactly like the spelling mistake above.

Inside quotes, capitals are kept exactly as you type them: print("ABC") and print("abc") print different text.

Spaces at the start of a line

In many languages, spaces at the start of a line are only decoration. In Python they are part of the meaning. The spaces at the start of a line are called indentation, and Python uses them to group lines that belong together. You'll use that properly when you learn about conditions and loops.

For now the rule is short: every line in your programs should start at the left edge, with no spaces before it. An unexpected indent stops the program before it even starts.

An unexpected indent: nothing runs at all
print("Opening the shop")
    print("Counting the till")

Try it: Remove the spaces before the second print and run it again.

Notice that this time not even the first line printed. Before running anything, Python reads the whole file to check that it follows the rules of the language (its syntax). An indentation problem breaks those rules, so Python refuses to start. A misspelled name like prnt is different: the file is valid Python, and the problem only shows up when that line runs.

Guided practice · ●difficulty 1 of 5 · about 3 min

Switch off a line

This program prints a pharmacy's opening times, but the line about Friday is out of date and should not appear for now. Comment out that line (put # at its start) instead of deleting it, so it can be switched back on later.

The output should be:

CITY PHARMACY
Saturday to Thursday: 9 to 23

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Challenge · ●difficulty 1 of 5 · about 5 min

A documented timetable

Write a program for Palm Grove Library that prints its timetable exactly like this, including the empty second line:

PALM GROVE LIBRARY

Sunday to Thursday: 8 to 20
Friday: closed
Saturday: 10 to 18

Start the program with a comment line that says what the program does (for example # Prints the library timetable).

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Debugging · ●●difficulty 2 of 5 · about 5 min

Fix the delivery note

A courier company's delivery note program stops with an error before printing anything. It should print:

Delivery note
Parcel: 2 boxes
Deliver to: Gate 3

Find and fix the problems. There is more than one, and once you fix the first, Python will point you to the next.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Summary

  • A program is a sequence of statements. Python runs them one at a time, from top to bottom.
  • If a statement fails, Python stops there; lines after it don't run.
  • A # starts a comment that Python ignores. Use comments to explain code or to switch lines off.
  • Blank lines in code are ignored; print() prints an empty line.
  • Python is case sensitive, and lines must start at the left edge for now.