Python Syntax
Python syntax is the set of rules for writing Python code. The rules are simple and visual.
One statement per line
Each line holds one statement. Python runs them from top to bottom.
print("Line one")
print("Line two")Indent shows the block
10 > 5 is true, so the indented line runs. The last line has no indent and always runs.
if 10 > 5:
print("Inside the block")
print("Outside the block")Case matters
A capital letter makes a different name. This wrong line would give an error.
Print("Hello")Example
if 8 > 3:
print("Eight is greater than three.")
print("This block ran.")
print("Program finished.")Key points
- Statement: one instruction that Python runs
- Use one statement per line in most cases
- Indentation: spaces at the start of a line
- Python syntax is the set of rules for writing Python code.
- Block: a group of lines that belong together
- Use indentation to show which lines form a block
- Python is case sensitive, so print and Print are different
In this example, Python first checks if 8 is greater than 3. That check is true, so the two indented lines run. Each indented line prints one message. The last line is not indented, so it runs after the block.
Note
Python uses indentation, not curly braces, to group lines.
Keep the spaces the same inside one block.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What does indentation do in Python?