Loading...

Python essentialsGetting Started

Syntax, indentation & comments

5 min lesson

Next
  • Syntax is the set of grammar rules for writing code; indentation is the spaces at the start of lines; comments are notes for humans that the computer ignores.
  • These matter because Python will not run code with broken syntax, and comments help you and others understand your code later.
  • Think of syntax as punctuation in a sentence: a missing period can confuse the reader, just like a missing colon confuses Python.
  • A statement is one instruction; a block is a group of statements that belong together; a comment starts with # and runs to the end of the line.
  • Python uses indentation (usually 4 spaces) to show which lines are inside a block, such as the body of an if statement.
  • The basic form is: if condition: followed by an indented block; comments look like # this is a note.
  • A real use: explaining why a calculation exists, or temporarily disabling a line while testing.
  • Easy-to-miss detail: mixing tabs and spaces can cause TabError or IndentationError even if it looks aligned.
  • This connects to your first program (you already used print) and prepares you for variables and control flow.
Keep in mind
  • Keep syntax simple and consistent: colons for blocks, semicolons not needed in Python, quotes around text.
  • Indent with 4 spaces per level and never mix tabs and spaces.
  • Use # comments to explain why, not what.
  • Syntax errors are normal; read the line number and message, fix one thing, and run again.
main.py
# A comment explains the purpose of the example.
apples = 3
oranges = 5

# A colon starts the indented body of the if statement.
if apples + oranges > 0:
    total = apples + oranges
    print(f"Total fruit: {total}")

What happens: Total fruit: 8

Breaking it down

Statement

total = apples + oranges

An assignment is one statement. Python executes it in order and binds the calculated value to the name total.

Block

if condition:\n do_work()

A block contains statements controlled by a function, loop, or condition. Python uses consistent indentation instead of curly braces to mark the block.

Comment

# explain why this step is needed

A hash mark starts a single-line comment. Python ignores the comment, so it does not change the program's result.

Formatted string

f"Total: {total}"

An f-string evaluates expressions inside braces and includes their values in the resulting text.

Python syntax essentials

SymbolMeaningExample
:Starts an indented blockif ready:
IndentationGroups statements into a block print("ready")
#Starts a single-line comment# explain here
f-stringInserts a value into a stringf"Count: {count}"

A helpful analogy

Syntax is the grammar of a programming language: it determines how instructions fit together. In Python, a colon and indentation mark a block, while a # comment is a note that the interpreter ignores.

Nesting rules

  • 1.Put a colon after a compound statement such as if, for, while, or def.
  • 2.Indent the block consistently (four spaces is the standard convention); do not mix tabs and spaces.
  • 3.Python does not require semicolons between ordinary statements, and braces do not delimit blocks.

Common mistakes

Forgetting the colon before a block

Avoid

if ready\n print("Go")

Use instead

if ready:\n print("Go")

The colon marks the start of the suite controlled by the if statement. Without it, Python reports a SyntaxError.

Not indenting the block

Avoid

if ready:\nprint("Go")

Use instead

if ready:\n print("Go")

Python uses indentation to determine which statements belong to the if body. The unindented version raises IndentationError.

Quick tips

  • •Use four spaces per indentation level and let your editor insert them consistently.
  • •Use # comments to explain intent or a non-obvious decision.
  • •When a SyntaxError points to a line, also inspect the line just above it.
  • •Try changing the indentation of the print() line and read Python's error before correcting it.
Previous
Next