Python syntax: comments, indentation and errors
The rules Python checks before it runs anything: names are case-sensitive, # starts a comment, and indentation decides which lines belong together.
Syntax is the set of rules for writing code that Python can read. Semantics is what that code means when it runs. Python reads a whole cell first and refuses to run any of it if one line breaks a syntax rule. Run the cells below in order, in a notebook in your project (lesson 0.2).
This module needs only Python's standard library, plus NumPy for one import in lesson 1.15. Add this line to requirements.txt and run your install command again (lesson 0.1):
numpy
Python is case-sensitive
name = "Alice"
Name = "Smith"
print(name)
print(Name)
OutputReal output
Alice Smith
name and Name are two different variables. The same goes for everything else: print works, Print is an unknown name.
Comments
# A comment: Python ignores everything after the #
print("Hello World") # a comment can also follow code
OutputReal output
Hello World
Python has no block-comment syntax. Text in triple quotes is often used as one, but it is a string: Python creates it and throws it away. A notebook gives it away by showing the string as the cell's value:
"""
This is not a comment.
It is a string nobody uses.
"""
OutputReal output
'\nThis is not a comment.\nIt is a string nobody uses.\n'
At the start of a file, function or class, a triple-quoted string is a docstring: the description that help() shows (lesson 1.12).
Indentation marks the blocks
Python groups lines by indentation, where many languages use braces { }. A line ending in : opens a block, and every line of that block is indented by the same amount: 4 spaces by convention. VS Code inserts them when you press Tab.
if True:
print("Correct indentation")
if False:
print("This won't print")
print("This will print")
print("Outside the if block")
OutputReal output
Correct indentation This will print Outside the if block
The block under if False: is skipped, and the last line belongs to neither block, so it always runs. Leave out the indentation and nothing in the cell runs:
age = 32
if age > 30:
print(age)
OutputReal output · last line of the error
IndentationError: expected an indented block after 'if' statement on line 2
Long lines and several statements
A backslash at the end of a line continues the statement on the next one:
total = 1 + 2 + 3 + 4 + 5 + 6 + 7 + \
4 + 5 + 6
print(total)
OutputReal output
43
Inside brackets a line can break without the backslash, which most style guides prefer:
total = (1 + 2 + 3 + 4 + 5 + 6 + 7
+ 4 + 5 + 6)
print(total)
OutputReal output
43
A semicolon puts several statements on one line:
x = 5; y = 10; z = x + y
print(z)
OutputReal output
15
It works, but one statement per line is easier to read.
Syntax errors and runtime errors
When a line breaks a syntax rule, Python reports a SyntaxError and runs nothing, not even the lines before it:
print("start")
2age = 30
OutputReal output · last line of the error
SyntaxError: invalid decimal literal
When the syntax is fine, Python runs the lines in order and can still fail on one of them. Here b was never given a value:
print("start")
a = b
OutputReal output · last line of the error
start NameError: name 'b' is not defined
The first error is about how the code is written, the second about what it means when it runs. Lesson 1.19 shows how to catch errors like the second one.