Indentation
Python uses indentation (spaces at the start of a line) to show which lines belong to a block — after if, for, def, class and others. A colon : starts the block.
- Use 4 spaces per level (the standard)
- All lines in a block must have the same indentation
- Wrong indentation gives an
IndentationError
Statements
Each line is usually one statement. No semicolon is needed at the end. Long lines can be split inside ( ), [ ] or { }.
Case sensitive
name, Name and NAME are three different variables.
Comments
# This is a comment— Python ignores everything after#- Use comments to explain why, not what
Docstrings
A string in triple quotes """ ... """ right after a function or class definition documents it. Tools and help() show it.
Style guide
PEP 8 is Python's official style guide: 4-space indents, snake_case names, lines under about 79–99 characters.
Example
# Check if a student passed
marks = 72
if marks >= 40:
print("Passed") # inside the if block
if marks >= 75:
print("Distinction")
else:
print("Failed")
print("Result checked") # back outside the block
def area(length, width):
"""Return the area of a rectangle."""
return length * width
print(area(4, 5))
help(area)
Output: Passed, Result checked, 20, then help() shows "area(length, width) Return the area of a rectangle." Distinction is not printed because 72 < 75.