Skip to content

Python Syntax and Comments

Write correctly structured statements and add comments that explain your code.

10 min read 10 quiz questions

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.

Example 1: Two statements
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.

Example 2: Correct indentation
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.

Example 3: Missing indentation
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.

Example 4: Continuation and comments
# 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.

Example
# Fare from home to coaching
fare = 40   # one-way in taka
print(fare * 2)  # return trip

How it works, step by step

  1. Lines starting with # are comments; Python skips them completely.
  2. Comments after code (same line) are ignored too.
  3. Only the real code runs, so the output is just 80.

References & Further Reading

Related entries from PyLabs's own reference library.

  • help() (Open Python's built-in help system for a topic.)
  • dir() (List an object's available attribute names.)

Your notes

Sign in to keep private notes alongside this lesson.

Sign in to take notes

Practice exercise

Not passed yet

Write an if True: statement with an indented print() that displays Indented correctly.

# Write your code below
Want another challenge on this module? More coding practice

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 completed

Create an ID or sign in to save lesson completion across devices.

Create ID to save
Module lesson progress0/5 complete
Module cheat sheet