agyanadda@gmail.com

Python Syntax and Comments

Python Basics 1 min read Updated Dec 4, 2026 Topic 3 of 32

Indentation

Python uses indentation (spaces at the start of a line) to show which lines belong to a block — after if, for, def, class and others. A colon : starts the block.

  • Use 4 spaces per level (the standard)
  • All lines in a block must have the same indentation
  • Wrong indentation gives an IndentationError

Statements

Each line is usually one statement. No semicolon is needed at the end. Long lines can be split inside ( ), [ ] or { }.

Case sensitive

name, Name and NAME are three different variables.

Comments

  • # This is a comment — Python ignores everything after #
  • Use comments to explain why, not what

Docstrings

A string in triple quotes """ ... """ right after a function or class definition documents it. Tools and help() show it.

Style guide

PEP 8 is Python's official style guide: 4-space indents, snake_case names, lines under about 79–99 characters.

Example

Example — Indentation and comments Python
# Check if a student passed
marks = 72

if marks >= 40:
    print("Passed")          # inside the if block
    if marks >= 75:
        print("Distinction")
else:
    print("Failed")

print("Result checked")      # back outside the block

def area(length, width):
    """Return the area of a rectangle."""
    return length * width

print(area(4, 5))
help(area)

Output: Passed, Result checked, 20, then help() shows "area(length, width) Return the area of a rectangle." Distinction is not printed because 72 < 75.

Test Yourself With Exercises

Exercise 1 Multiple Choice

How does Python mark a code block?

Exercise 2 True / False

Python is case sensitive: age and Age are different variables.

Exercise 3 Fill in the Blank

A single-line comment starts with the ___ symbol.

Frequently Asked Questions

Use 4 spaces, as PEP 8 recommends. Mixing tabs and spaces causes errors. Editors like VS Code insert 4 spaces when you press Tab in a Python file.