4.3 Statements
Simple statements vs compound statements in Python, placing multiple statements on one line, and splitting a long statement across multiple lines.
1.1 What Is a Statement?
A statement is an instruction that tells Python to do something.
name = "Alex"
print(name)
The first statement stores a name. The second statement displays it.
Python normally runs statements from top to bottom.
1.2 Simple Statements
A simple statement usually fits on one line and performs one action.
server_count = 3 # assignment
print(server_count) # function call
import platform # import
Other common simple statements include return, break, and continue inside functions or loops.
1.3 Compound Statements
A compound statement controls a block of code. Its first line ends with a colon, and the lines below it are indented.
server_status = "running"
if server_status == "running":
print("The server is healthy")
The if line is the header. The indented print() line is the body.
Common compound statements include:
iffor decisionsforandwhilefor loopsdeffor functionsclassfor classestryfor error handling
1.4 Statements with Conditions
An if statement runs its block only when the condition is true:
disk_usage = 70
if disk_usage > 80:
print("Disk usage is high")
else:
print("Disk usage is normal")
The else block runs when the if condition is false.
1.5 Statements with Loops
A for statement repeats a block for each item:
servers = ["web-01", "web-02", "web-03"]
for server in servers:
print(server)
The output is:
web-01
web-02
web-03
1.6 Multiple Statements on One Line
Python allows multiple simple statements on one line with semicolons:
first_name = "Alex"; print(first_name)
However, this style is harder to read. Write one statement per line instead:
first_name = "Alex"
print(first_name)
One statement per line is the usual Python style and makes errors easier to find.
1.7 Long Statements on Multiple Lines
Long statements can be split across lines inside brackets:
server_names = [
"web-01",
"web-02",
"web-03",
]
Python understands that the list continues until the closing bracket.
You can also use parentheses for a long calculation:
total_cost = (
monthly_compute_cost
+ monthly_storage_cost
+ monthly_network_cost
)
This is easier to read than one very long line.
Avoid using a backslash for line continuation when brackets can be used:
total = (
first_value
+ second_value
)
1.8 The pass Statement
The pass statement does nothing. It is useful when you want to create a block and add the real code later:
def check_server():
pass
Without pass, Python would report an error because the function has no body.
1.9 A Complete Example
This example combines assignments, a condition, a function, and a loop:
servers = ["web-01", "web-02"]
healthy_servers = 0
def show_server(server):
print(f"Checking {server}")
for server in servers:
show_server(server)
healthy_servers += 1
print(f"Healthy servers: {healthy_servers}")
1.10 Practice Exercise
Create a program that checks a list of server statuses:
server_statuses = ["running", "stopped", "running"]
for status in server_statuses:
if status == "running":
print("Server is healthy")
else:
print("Server needs attention")
Change the list and observe how the output changes.
Interview Questions
- What is a statement in Python?
- What is the difference between a simple and compound statement?
- Why does a compound statement need a colon?
- Why is indentation important after a compound statement?
- Can you place multiple statements on one line?
- How can you split a long statement across multiple lines?
Quick Interview Answer
A statement is an instruction for Python to execute. A simple statement usually performs one action on one line, such as an assignment or function call. A compound statement, such as
if,for, ordef, ends with a colon and contains an indented block. Python allows semicolons, but one statement per line is easier to read.
Key Takeaways
- A statement is an instruction for Python.
- Simple statements usually fit on one line.
- Compound statements contain an indented block.
- Compound statement headers end with a colon.
- Prefer one statement per line.
- Use brackets or parentheses to split long statements. 1 2
## Line Continuation
### What Is It?
Splitting one logical statement across multiple physical lines, either explicitly with a trailing backslash or implicitly inside brackets.
### Why Is It Used?
It keeps long expressions readable instead of one very long line.
### How Is It Used?
See [4.15 Multiple Statements & Line Continuation](../15-multiple-statements-and-line-continuation/) for the full comparison of both styles.
```python
total = 1 + \
2 + \
3
>>> total
6
Quick Interview Answer
“A simple statement fits on one line and does one thing — an assignment, a function call, an import. A compound statement has a colon-terminated header and an indented block beneath it —
if,for,def,class. You can technically cram multiple simple statements onto one line with semicolons, but PEP 8 discourages it.”
Common Mistakes
- Forgetting the colon at the end of a compound statement’s header line — the single most common beginner
SyntaxError. - Overusing semicolons to combine simple statements, which hurts readability without any real performance benefit.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form