Syntax and indentation
RequiredRequired
Python uses indentation, not braces, to group statements. A block starts after a line ending in : and lasts while the lines stay indented.
temperature = 31
if temperature > 30:
print("Hot") # inside the if
print("Drink water")
print("Done") # outside: runs always
The rules that matter
- Use 4 spaces per level. Never mix tabs and spaces: Python refuses the file with
TabError. - A line ends the statement. Long expressions can continue inside parentheses, brackets or braces without any extra character.
#starts a comment, up to the end of the line.- Names are case-sensitive:
totalandTotalare different variables. Usesnake_casefor variables and functions, as the style guide (PEP 8) recommends.
total = (price * quantity
+ shipping
- discount)
Indentation errors are the most common first mistake. When Python reports IndentationError, look at the line above the one it names.