Python Syntax Basics
Indentation, colons, and why Python looks the way it does. The rules that make Python the most readable programming language in the world.
"Clean code is not written for the computer. It is written for the next human who reads it — including your future self."
— ShurAIWhat is Syntax?
Syntax is the set of rules that define how you must write code in a language. Just like English has grammar rules — capital letters at the start of sentences, full stops at the end — Python has its own rules about how code must be structured.
Break an English grammar rule and people still understand you. Break a Python syntax rule and the program refuses to run. Python is strict — but its rules are few, logical, and designed to make code beautiful.
Rule 1 — Indentation is Everything
This is the most important Python rule and the one that surprises people coming from other languages. In Python, indentation is not optional formatting — it is part of the language itself.
Indentation tells Python which lines of code belong together — which lines are "inside" a block. Look at this example:
# A simple if statement
temperature = 38
if temperature > 37:
print("You have a fever.")
print("Please rest and drink water.")
print("Check complete.")
The two lines starting with print that are indented belong inside the if block. They only run when the condition is true. The last print is not indented — it runs always, regardless of the condition.
Use 4 spaces for each level of indentation. This is the official Python standard. Most code editors automatically insert 4 spaces when you press the Tab key. Never mix tabs and spaces — this causes errors.
See what happens when indentation is wrong:
if True:
print("This will cause an error!")
# ↑ Missing indentation — Python does not know this belongs inside the if
IndentationError: expected an indented block
Python will tell you exactly what went wrong and on which line. Once you get used to indentation, it becomes second nature — and your code will always be visually organized.
Rule 2 — Colons Start a Block
Whenever you are about to write a block of code — an if, a loop, a function, a class — Python requires a colon : at the end of that line. The colon is Python's way of saying "what follows belongs inside this".
# Colon after if condition
if 5 > 3:
print("Five is greater than three")
# Colon after for loop
for i in range(3):
print(i)
# Colon after function definition
def greet():
print("Hello!")
# Colon after while condition
while False:
print("This never runs")
The pattern is always the same: statement ending with colon → indented block on the next line. This appears everywhere in Python — master this and you understand the structure of all Python code.
Rule 3 — Python is Case Sensitive
Python treats uppercase and lowercase letters as completely different. name, Name, and NAME are three separate things in Python's eyes.
city = "Mumbai"
City = "Delhi"
CITY = "Pune"
print(city) # Output: Mumbai
print(City) # Output: Delhi
print(CITY) # Output: Pune
# Python keywords are also case sensitive
print(True) # Works ✓
# print(true) ← Error! 'true' is not a Python keyword
A very frequent beginner error is writing Print() or PRINT() instead of print(). Python will say NameError: name 'Print' is not defined. Always use lowercase for Python's built-in functions.
Rule 4 — One Statement Per Line
In Python, each line normally contains one complete instruction. Unlike some other languages, you do not need a semicolon to end a line. The line ending itself tells Python the statement is complete.
# Normal Python — one statement per line, no semicolons needed
name = "Ravi"
age = 25
print(name)
print(age)
# You CAN put multiple statements on one line using semicolons
# but this is considered bad style — avoid it
x = 1; y = 2; print(x + y) # works but messy
Rule 5 — Blank Lines for Readability
Python ignores blank lines inside your code. You can and should use them to separate logical sections, just like paragraphs in writing. Good use of blank lines makes code dramatically easier to read.
# Without blank lines — hard to read
name = "Priya"
age = 22
def greet(person):
print(f"Hello {person}")
greet(name)
print(f"Age: {age}")
# With blank lines — easy to read
name = "Priya"
age = 22
def greet(person):
print(f"Hello {person}")
greet(name)
print(f"Age: {age}")
Rule 6 — Breaking Long Lines
If a line becomes too long to read comfortably, you can break it across multiple lines using a backslash \, or by opening a bracket and continuing inside it:
# Long line broken with backslash
total = 100 + 200 + \
300 + 400
# Long line broken inside brackets (preferred)
fruits = [
"mango",
"apple",
"banana",
"guava",
]
# Long function call broken across lines
print(
"This is a very long message",
"split across multiple lines",
"for readability"
)
Rule 7 — Comments with #
A comment starts with the # symbol. Python completely ignores everything after # on that line. Comments are notes you write for yourself and other humans reading the code — the computer never sees them.
# This entire line is a comment
score = 95 # This is an inline comment — code runs, then comment is ignored
# Comments explain WHY, not just WHAT
# Bad comment: x = x + 1 # add 1 to x
# Good comment: x = x + 1 # increment attempt count for retry logic
Putting It All Together
Here is a small program that uses every syntax rule from this lesson. Read it carefully and notice each rule in action:
# Student grade checker
# Demonstrates all Python syntax rules
student_name = "Amit" # variable names use lowercase_with_underscores
score = 78
# Function definition ends with colon → body is indented
def check_grade(marks):
if marks >= 90: # if ends with colon → body indented further
return "A — Excellent!"
elif marks >= 75:
return "B — Good job!"
elif marks >= 60:
return "C — Keep going!"
else:
return "D — Let's study harder."
# Blank line separates sections — makes code easier to read
grade = check_grade(score)
print(f"{student_name} scored {score} marks.")
print(f"Grade: {grade}")
Amit scored 78 marks.
Grade: B — Good job!
Quick Reference — Python Syntax Rules
name, Name, NAME are three different things. Keywords like True, False, None must be capitalised exactly.#. Python ignores everything after it on that line. Use them generously."Python's rules exist for one reason: to make code that humans can read as easily as machines can run it. Embrace them — they are your friend."
— ShurAI🧠 Quick Check — Question 1
What does indentation do in Python?
🧠 Quick Check — Question 2
Which of these lines will cause a Python error?
🧠 Quick Check — Question 3
You write Print("hello") instead of print("hello"). What happens?