Course topics

By WebNest Studio Engineering Team

Python Tutorial

Python Syntax Basics

Python's syntax was deliberately designed to read almost like plain English while still being precise enough for a computer to execute. Unlike languages such as Java or C, Python does not use curly braces {} to mark blocks of code, and it does not require a semicolon at the end of every statement. Instead, Python uses indentation itself as part of the language's grammar.

This design choice — sometimes called the "off-side rule" — forces every Python program to be visually organized, which is one reason Python is popular for teaching, scripting, and data work. Learning to read and write correct indentation is the very first skill every Python programmer needs, because a misplaced space can change what your program does or stop it from running at all.

Why it matters

In most languages indentation is a matter of style. In Python it is part of the grammar: it decides which statements belong to a loop, a function or an if. A line indented one level too far, or not far enough, changes what the program does or stops it from running, so this is the first rule to get right.

Indentation Defines Blocks

In Python, a colon (:) at the end of a line — after an if, for, while, def, class, or similar statement — signals that an indented block follows. All statements in that block must be indented by the same amount, conventionally four spaces. There are no braces to open or close a block; the indentation level itself marks where the block starts and ends.

  • A block begins right after a line ending in a colon (:).
  • Every line in the block must use consistent indentation (spaces are recommended; mixing tabs and spaces raises a TabError).
  • Reducing the indentation level ends the block and returns control to the enclosing level.

No Semicolons or Braces Required

Python statements normally end at the end of the physical line — no semicolon is needed. A semicolon can technically be used to place multiple statements on one line, but this is discouraged by Python's style guide (PEP 8) because it hurts readability. Similarly, there are no braces {} anywhere in Python's control-flow syntax; parentheses () are used only for function calls, tuples, and grouping expressions.

The Python Interpreter

Python is an interpreted language: source code in a .py file is read and executed line by line by the Python interpreter (CPython is the standard, most widely used implementation), rather than being compiled ahead of time into a standalone native executable. You can run a script with "python filename.py" from a terminal, or run Python interactively using the REPL (Read-Eval-Print Loop) by simply typing "python" or "python3", which lets you test expressions one at a time and see results immediately.

Comments

A single-line comment starts with a hash symbol (#) and continues to the end of the line; the interpreter ignores everything after it. Python has no dedicated multi-line comment syntax, but a triple-quoted string (''' ... ''' or """ ... """) that is not assigned to anything is commonly used as a block comment, and the same triple-quoted syntax is also used for docstrings that document functions, classes, and modules.

Examples

Indentation-based blocks compared to a flat statement

Python
age = 20

# Comment: check whether age qualifies as an adult
if age >= 18:
    print("You are an adult")
    print("You can vote")
else:
    print("You are a minor")

print("This line always runs, regardless of the if/else")
Output
You are an adult
You can vote
This line always runs, regardless of the if/else

Inconsistent indentation raises an IndentationError

Python
if True:
    print("First line indented with 4 spaces")
   print("Second line indented with only 3 spaces")
Output
  File "<stdin>", line 3
    print("Second line indented with only 3 spaces")
                                                     ^
IndentationError: unindent does not match any outer indentation level

Common Mistakes

  • Mixing tabs and spaces in the same block, which raises a TabError in Python 3 even if the code looks aligned in some editors.
  • Forgetting the colon (:) at the end of an if, for, while, or def line — this raises a SyntaxError.
  • Assuming indentation amount does not matter as long as it is "some" indentation — every line in the same block must match exactly.
  • Writing multiple unrelated statements on one line with semicolons out of habit from other languages, which goes against PEP 8 style.

Exercise

The program should add up the numbers and print the total once, but it stops with an IndentationError. Fix the indentation.

Expected output: 6

Python · starter code
total = 0
for number in [1, 2, 3]:
total += number
print(total)
Show hints
  • The line that should repeat must be indented under the for line.
  • Leave print(total) with no indentation, so it runs once after the loop has finished.
Show solution
Python · solution
total = 0
for number in [1, 2, 3]:
    total += number
print(total)

Quick quiz

1. How does Python know which statements are inside a loop?

  • A. Curly braces
  • B. Their indentation
  • C. A semicolon after each one
  • D. The keyword end
Show answer

B. The statements indented under the line ending with a colon form its block.

2. How many spaces per indentation level does the PEP 8 style guide recommend?

  • A. 2
  • B. 8
  • C. 4
  • D. One tab
Show answer

C. Four spaces per level is the standard used by almost all Python code.

3. Is a semicolon needed at the end of a Python statement?

  • A. Yes, always
  • B. Only after print
  • C. Only inside functions
  • D. No; a statement ends at the end of the line
Show answer

D. A semicolon may separate two statements on one line, but this is rarely done.

Interview questions

How does Python use indentation?

Show answer

A line ending with a colon, such as an if, for, def or class line, starts a block, and the lines indented beneath it belong to that block. The block ends at the first line indented less. All lines of one block must use the same indentation; inconsistent indentation raises IndentationError.

What is PEP 8?

Show answer

PEP 8 is the official style guide for Python code. It covers layout and naming: four spaces per indentation level, a limit on line length, snake_case for functions and variables, PascalCase for classes and upper case for constants. Following it makes code look familiar to any Python developer, and formatters such as Black and Ruff apply it automatically.

Key Points to Remember

  • Python uses indentation, not braces, to define code blocks — indentation is part of the syntax, not just style.
  • Statements end at the end of the line; semicolons are optional and rarely used.
  • Python code runs through an interpreter (commonly CPython), either as a script or interactively via the REPL.
  • Comments start with #; triple-quoted strings are used for docstrings and block-style comments.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering