Comments and readable code
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, know why one space at the start of a line can break a program, and write code the way most Python programmers do. Everything here runs in your browser.
Notes that Python ignores
Here's a program with some lines starting with #, and one line with # halfway
along. Decide what it prints before you look.
What will this print?
Decide before you look. Guessing wrong is how this sticks.
print("Selam")
# print("secret")
print("Abel") # print("secret")Python prints
Selam Abel
Everything from # to the end of a line is ignored. The whole middle line is skipped, and on the last line only print("Abel") runs. The words print("secret") never reach Python as code.
Anything after a # on a line is a
.
Python skips it completely, even if it looks like code. Comments are for people: they
explain why the code is there or what a number means. They never change what the
program does.
# Price of one macchiato, in birr
print(45)
print(3) # cups ordered this morningIn 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. Putting # in front of a line of code to switch it off is called "commenting
it out". Try it: comment out the last line above and run it again.
A # inside quotes is different: it's part of the text. print("#1 in class")
prints #1 in class.
Quick check
A program has the line print(45) # price in birr. What does that line show?
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. 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. In the code below, the second line starts with one space. You can hardly see it, but Python can.
Use four spaces for each level of indentation. VS Code does this for you when you press Tab in a Python file.
Code that reads well
Programs are full of names. In Module 1 you'll give names to values, like naming a
price so you can use it again. These two programs do exactly the same thing and
both print 135:
p=45
q=3
print(p*q)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, so Python code written by different people looks familiar. Two of its
rules you can use from today: names are lowercase with underscores between words
(price_per_cup), and there's one space on each side of symbols like =, +, and
*, and after commas.
Try it: this code runs, but it's cramped. Add spaces around + and after each 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.
Exercise
Explain a program with comments
This program works, but a reader can't tell what it's for. Add a comment at the top saying what the program does, and a comment before each of its two parts explaining that part. Don't change the code itself: it should print exactly the same thing.
Bole to Piassa
280Your code
print("Bole to Piassa")
print(250 + 30)In the editor, press Escape then Tab to move on.
Show a solutionHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
# Taxi receipt for one trip, in birr
# Where the trip went
print("Bole to Piassa")
# The fare plus a 30 birr tip
print(250 + 30)Key takeaways
- A
#starts a comment; Python ignores everything after it on that line, even code. - Comments never change what a program does. Good ones explain why.
- Indentation (spaces at the start of a line) groups lines in Python, and one stray space is an
IndentationError. - Follow PEP 8: lowercase names with underscores, spaces around operators and after commas.