Python Statements
A statement in Python is an instruction that the Python interpreter can execute.
Every line of code you write in Python is made up of statements that tell Python what to do.
Python is designed to keep statements simple, readable, and flexible.
What Are Python Statements?
Python statements are used to:
- Assign values
- Make decisions
- Repeat actions
- Call functions
- Control program flow
Python executes statements line by line, from top to bottom.
Simple Definition
A Python statement is a single logical instruction written in a program.
Many Statements in Python
Python allows you to write multiple statements in a program.
Each statement is usually written on a new line.
This makes Python code:
- Easy to read
- Easy to debug
- Easy to maintain
Code Example 1: Multiple Statements on New Lines
python
x = 10
y = 20
sum = x + y
print(sum)
Code Example 2: Statements with Conditions
python
age = 18
if age >= 18:
print("You are eligible to vote")
Code Example 3: Statements Inside a Loop
python
for i in range(3):
print("Number:", i)
Semicolons in Python (Optional, Rarely Used)
Unlike languages such as C, C++, or Java, Python does not require semicolons at the end of statements.
However, Python allows semicolons, but they are rarely used.
Important
Semicolons are optional in Python and generally not recommended.
Code Example 1: Without Semicolon (Recommended)
python
print("Hello Python")
Code Example 2: With Semicolon (Valid but Rare)
python
print("Hello Python");
Code Example 3: Multiple Statements on One Line
python
x = 5; y = 10; print(x + y)
Best Practice
Writing multiple statements on one line reduces readability and should be avoided.
Expression Statements
An expression statement evaluates an expression and produces a result.
Code Example 1: Simple Expression
python
10 + 20
Code Example 2: Expression with Variables
python
x = 5
x + 10
Code Example 3: Expression in Print
python
print(5 * 4)
Assignment Statements
Assignment statements are used to assign values to variables.
Code Example 1: Simple Assignment
python
x = 10
Code Example 2: Multiple Assignment
python
a = b = c = 100
print(a, b, c)
Code Example 3: Augmented Assignment
python
x = 5
x += 3
print(x)
Control Flow Statements
Control flow statements decide which code runs and when.
Common control flow statements:
- if
- else
- elif
- for
- while
Code Example 1: if Statement
python
num = 10
if num > 0:
print("Positive number")
Code Example 2: if-else Statement
python
num = -5
if num >= 0:
print("Positive")
else:
print("Negative")
Code Example 3: for Loop Statement
python
for i in range(1, 4):
print(i)
Empty Statements (pass)
Sometimes a statement is required syntactically but you don’t want to write any code yet.
Python provides the pass statement for this purpose.
Code Example 1: pass in Condition
python
if True:
pass
Code Example 2: pass in Loop
python
for i in range(3):
pass
Code Example 3: pass in Function
python
def my_function():
pass
Use Case
The pass statement acts as a placeholder.
Why Python Statements Are Beginner-Friendly
Python statements:
- Are close to natural language
- Avoid unnecessary symbols
- Encourage clean coding style
- Improve readability
Key Takeaway
Python statements focus on clarity over complexity.
Summary
- Python statements are executable instructions
- Each statement is usually written on a new line
- Semicolons are optional and rarely used
- Python supports different types of statements
- Clean statements make Python easy to learn and use
Exercise
- Write a program with 5 different statements
- Try using a semicolon and see the output
- Identify assignment, expression, and control statements in your code