Getting Started Lesson 3 of 23
Python Syntax — Indentation and Comments
How Python code is written — one statement per line, indentation instead of curly braces, case sensitivity, and writing comments with #.
Python's rules of grammar (its syntax) are simpler than most languages, but one rule is very strict and very important: indentation. Get it right from the start and you'll avoid the most common Python errors.
One statement per line
In Python, each line is usually one instruction. There's no semicolon at the end:
print("Line one")
print("Line two")
total = 25 + 15
print(total)
Output
Line one Line two 40
Indentation defines blocks
Other languages group code with curly braces { }. Python uses indentation, the spaces at the start of a line. Lines that are indented the same amount belong to the same block:
grade = 85
if grade >= 75:
print("PASSED")
print("Congratulations!")
print("Done checking.")
Output
PASSED Congratulations! Done checking.
The two indented lines belong to the if. They run only when the condition is true. The last line isn't indented, so it always runs. Notice the colon : at the end of the if line; a colon always starts a new block.
Indentation errors
If the indentation is wrong, Python refuses to run the program:
if grade >= 75:
print("PASSED")
Error
File "main.py", line 2
print("PASSED")
^
IndentationError: expected an indented block after 'if' statement on line 1Python is case-sensitive
name, Name and NAME are three different variables. print works but Print gives an error:
Print("Hi")
Error
NameError: name 'Print' is not defined. Did you mean: 'print'?
Python's error messages are friendly: it even suggests what you probably meant.
Comments
A comment starts with #. Python ignores everything after it on that line:
# This program computes the area of a classroom
length = 9 # meters
width = 7 # meters
print(length * width) # 63 square meters
Output
63
Multi-line comments
Python has no special multi-line comment. Either put # on every line, or use a string with three quotes, which Python ignores if it isn't used for anything:
"""
Grade Calculator
Author: Juan Dela Cruz
Grade 10 - Rizal
"""
print("Grade Calculator v1")
A triple-quoted string at the top of a file or function is called a docstring. It documents what the code does.
Long lines
If a line gets too long, you can continue it on the next line inside parentheses, brackets or braces:
total = (math_grade + science_grade + english_grade
+ filipino_grade + ap_grade)
Blank lines
Blank lines are ignored. Use them to separate parts of your program, like paragraphs in an essay. It makes code much easier to read.
The style guide: PEP 8
Python has an official style guide called PEP 8. The main rules for beginners:
- 4 spaces per indentation level.
- Variable and function names in lowercase with underscores:
final_grade,compute_total. This is called snake_case (JavaScript uses camelCase instead). - Spaces around operators:
x = 5 + 3, notx=5+3. - Lines no longer than about 79 characters.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04