3. Python Syntax and Comments

What you will learn

  • How Python uses indentation instead of braces
  • Statements and line breaks
  • Case sensitivity
  • How to write comments and docstrings

Statements

A Python program is a list of statements, run from top to bottom. Each normally sits on its own line. Unlike PHP or JavaScript, there is no semicolon at the end of a line.

Python code

print("First")
print("Second")
print("Third")

Output

First
Second
Third

Indentation matters

Many languages use curly braces { } to group code. Python uses indentation, the spaces at the start of a line. A line ending in a colon (:) starts a block, and the lines inside it must be indented by the same amount.

Python code

age = 20

if age >= 18:
    print("You are an adult.")
    print("This line is also inside the if.")

print("This line always runs.")

Output

You are an adult.
This line is also inside the if.
This line always runs.
  • The standard is four spaces per level.
  • Do not mix tabs and spaces in one file. Set your editor to insert spaces when you press Tab.
  • Code in the same block must line up exactly.

Indentation errors

Python code: this fails

if 5 > 2:
print("Five is bigger")

What Python reports (the wording varies by version)

IndentationError: expected an indented block after 'if' statement on line 1

Python refuses to run the file until the indentation is fixed. Read the message and the line number it gives.

Case sensitivity

Python treats capital and small letters as different. name, Name, and NAME are three separate variables. Keywords are written in lowercase, except True, False, and None, which start with a capital.

Python code

colour = "blue"
Colour = "red"

print(colour, Colour)

Output

blue red

Long lines

Inside brackets, a statement can continue over several lines. A backslash \ at the end of a line also continues it, though brackets are usually tidier.

Python code

total = (10 +
         20 +
         30)

print(total)

Output

60

You can put two statements on one line with a semicolon, but this is discouraged. Use one statement per line.

Comments

A comment is a note for people. Python ignores it. A comment starts with # and runs to the end of the line.

Python code

# This is a comment on its own line
price = 20          # a comment after code

# print("This line is switched off")
print("Price:", price)

Output

Price: 20

Python has no special multi-line comment. Either put # in front of each line, or select the lines in your editor and press Ctrl+/.

Docstrings

A string in triple quotes placed first inside a function, class, or file is a docstring. Tools read it to explain your code.

Python code

def greet(name):
    """Return a greeting for the given name."""
    return "Hello, " + name

print(greet("Sam"))
print(greet.__doc__)

Output

Hello, Sam
Return a greeting for the given name.

You will learn functions in a later lesson. For now, notice that the docstring documents what the function does.

Good habits

  • Explain why, not the obvious what.
  • Keep comments short, and update them when the code changes.
  • Follow PEP 8, the official style guide: four-space indents, lines under about 79 characters, and clear names.
  • Never put passwords or private data in comments.

Common mistakes

  • Forgetting the colon at the end of an if line.
  • Inconsistent indentation, or mixing tabs and spaces.
  • Adding semicolons out of habit from other languages.
  • Different capital letters in two places for the same variable.
  • Using curly “smart” quotes copied from a document instead of straight quotes.

Exercise

Fix this program so it prints Welcome and then Done. Also add a comment above the if explaining what it does.

logged_in = True
if logged_in
print("Welcome")
print("Done")
Show solution
logged_in = True

# Greet the visitor only if they are logged in
if logged_in:
    print("Welcome")

print("Done")

The if line needed a colon, and the print inside it needed to be indented.

Knowledge check

1. How does Python group statements into a block?

By indentation, after a line that ends with a colon.

2. How do you write a single-line comment?

Start it with #.

3. Are name and Name the same variable?

No. Python is case sensitive.

Summary

  • No semicolons or braces: lines end the statement, and indentation (four spaces) forms blocks.
  • Python is case sensitive, and blocks start after a colon.
  • Comments use #, and docstrings in triple quotes document functions.
Scroll to Top