Python syntax uses whitespace indentation instead of curly braces {} to define code blocks. Every loop, function, class, and if statement ends its header line with a colon : and indents the lines below it.
This strict layout keeps every Python file readable across teams. If two lines in the same block use different spacing, Python refuses to run and raises an IndentationError.
A block starts after a colon and continues until a line returns to the previous indentation level. Standard Python style (PEP 8) uses exactly 4 spaces per level.
order_total = 120
if order_total >= 100:
discount = 15
final_price = order_total - discount
print(f"Discount applied: ${final_price}")
else:
print(f"Standard total: ${order_total}")
Both discount and final_price belong inside the if block because they share the same 4-space indent. The else: line returns to column zero to close the first block.
Python treats each physical line as one statement. When an expression grows too wide, wrap it inside parentheses (), brackets [], or braces {} instead of using backslashes.
weekly_sales = [
1450, 1890, 2100,
1760, 2340, 2980,
]
message = (
"Weekly sales exceeded the target "
"across five of six days."
)
print(sum(weekly_sales), message)
IndentationError.if score > 50 without : causes a SyntaxError immediately.; between statements on one line, but putting multiple statements on one line hurts readability.PEP 8 recommends 4 spaces per indentation level. Python allows any consistent count above zero, such as 2 spaces, as long as every line in that block matches.
Python 3 forbids mixing tabs and spaces in the same file. Nearly all Python editors convert the Tab key into 4 spaces automatically.
Yes. total, Total, and TOTAL are three separate names, and keywords like if, for, and True must match exact casing.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.