Skip to content

Comments and readable code

4 min read

Code is read far more often than it's written, and the person reading it most is you, a week from now. By the end of this lesson you'll leave notes in your code that Python ignores, understand why spaces at the start of a line can break a program, and write names and spacing the way most Python programmers do. Small habits, but they make every later lesson easier.

Notes that Python ignores

Anything after a # on a line is a . Python skips it completely. Comments are for people: they explain why the code is there or what a number means.

# Price of one macchiato, in birr
print(45)
print(3)  # cups ordered this morning

In the editor, press Escape then Tab to move on.

A comment can take a whole line, or sit at the end of a line of code, after two spaces. Either way, only the code runs.

Comments are also handy for switching a line off without deleting it. Putting # in front of a line of code is called "commenting it out". Before you run this, decide what you'll see.

What will this print?

Decide before you look. Guessing wrong is how this sticks.

print("Selam")
# print("Hana")
print("Abel")

Python prints

Selam
Abel

The middle line starts with #, so it's a comment now. Python skips it, and only the other two lines run.

A # inside quotes is different: it's part of the text, not the start of a comment. print("#1 in class") prints #1 in class.

Spaces at the start of a line

In most languages, spaces at the start of a line are only decoration. In Python they mean something. The space at the start of a line is called , and Python uses it to group lines together.

You'll use it properly in Module 2, with if. For a first look, here the second line is indented, which tells Python it belongs to the if above it and only runs when the condition is true.

if 5 > 3:
  print("Five is bigger")
print("Done")

In the editor, press Escape then Tab to move on.

Because indentation means something, a stray space where Python doesn't expect one is an error, even though the line looks harmless.

Use four spaces for each level of indentation. VS Code does this for you when you press Tab in a Python file.

You may see code that indents with the Tab character instead. Python accepts either, but never both in the same block: mixing them gives TabError: inconsistent use of tabs and spaces in indentation, which is hard to spot because tabs and spaces look the same on screen. Sticking to four spaces avoids the problem completely.

Names and spacing that read well

Programs are full of names. In the next module you'll give names to values, like naming a price so you can use it again. Compare these two programs. They do exactly the same thing and both print 135:

Hard to read
p=45
q=3
print(p*q)
Easy to read
price_per_cup = 45
cups = 3
print(price_per_cup * cups)

The second one explains itself. You can tell what it calculates without running it.

Python has an official called PEP 8. Most Python code in the world follows it, so following it too makes your code easy for others to read, and theirs easy for you. A few of its rules you can use from today:

  • Names are lowercase, with underscores between words: price_per_cup, not PricePerCup or pricepercup.
  • Put one space on each side of =, +, *, and similar symbols: a = b + c.
  • After a comma, one space: print("Birr", 250).
  • Keep lines short, around 79 characters, so code fits on a screen.

Names also have a few hard rules that Python enforces, style guide or not. A name can contain letters, digits, and underscores, but it can't start with a digit and can't contain spaces. second_place is fine; 2nd_place is a SyntaxError. Names are also case-sensitive, so total and Total are two different names.

Try it: this code runs, but it doesn't follow PEP 8. Add spaces around the symbols and after the comma, then run it again. The output shouldn't change.

print("Total:",250+30,"birr")

In the editor, press Escape then Tab to move on.

Quick check

Which name follows PEP 8?

Exercise

Tidy up a taxi fare

This program works, but it's hard to read. Rewrite it so that it has a comment saying what it calculates, clear names instead of x and y, and PEP 8 spacing. It should still print the same thing:

Output
280

Your code

x=250
y=30
print(x+y)

In the editor, press Escape then Tab to move on.

Show a solution

This is one way to solve it, not the only one. If yours prints the same thing, it works.

# Taxi fare from Bole to Piassa, plus a tip, in birr
fare = 250
tip = 30
print(fare + tip)

Key takeaways

  • A # starts a comment; Python ignores everything after it on that line.
  • Good comments explain why; the code already shows what.
  • Indentation (spaces at the start of a line) groups lines in Python, and a stray indent is an error.
  • Follow PEP 8: lowercase names with underscores, spaces around operators and after commas.