Python Syntax and Comments
Write correctly structured statements and add comments that explain your code.
Statements and syntax
A statement is one instruction for Python to carry out. Syntax means the rules for writing that instruction correctly.
Most Python statements end at the end of a line, so clear line breaks matter.
name = "Mina"
print("Hello", name)
The first statement saves text. The second statement prints a greeting using that saved value.
Python indentation
Indentation is the blank space at the beginning of a line. Python uses it to show which statements belong inside a block, such as an if statement.
if True:
print("This line is indented")
print("This line is outside the if")
The four spaces before the first print() place it inside the if block. The final line has no indentation, so it is outside the block.
if True:
print("Missing spaces")
This code raises an IndentationError because the line after if True: needs indentation. Use four spaces for each indentation level.
Comments and continuation
You can continue a long expression inside parentheses. Use # for a single-line comment. Triple-quoted strings are often used as multi-line comments, while a triple-quoted string placed first in a module, function, or class is a docstring that describes it.
# Calculate a total across two lines
total = (10 + 20 +
30)
"""This text can span
multiple lines."""
print(total)
The parentheses let the addition continue onto the next line. The comment and triple-quoted text do not change the value that gets printed.
Key points
- A statement is one Python instruction.
- Python uses indentation to define blocks.
- Incorrect indentation causes IndentationError.
- Use # for single-line comments.
- Parentheses can continue a long expression.
Worked example
You leave a note for the shopkeeper inside the code itself — comments explain, Python ignores them.
# Fare from home to coaching fare = 40 # one-way in taka print(fare * 2) # return trip
How it works, step by step
- Lines starting with # are comments; Python skips them completely.
- Comments after code (same line) are ignored too.
- Only the real code runs, so the output is just 80.
References & Further Reading
Related entries from PyLabs's own reference library.
Practice exercise
Not passed yetWrite an if True: statement with an indented print() that displays Indented correctly.
# Write your code below
Check yourself
Python Syntax and Comments quiz
10 questions. You get instant feedback per question, and the timer starts when you press the button.
Lesson incomplete
Not completedCreate an ID or sign in to save lesson completion across devices.