In Python, a statement is like a sentence in English—it tells the computer to do something. Let’s break it down into simple types of statements, with examples you can try.
1. Expression Statements
An expression statement evaluates something and gives you a result. Think of it like solving a math problem.
Example:
# Expression statement
3 + 4
Output:
7
You don’t need to store the result; Python simply evaluates the expression.
2. Assignment Statements
These are used to store values in variables. Think of variables as boxes where you can keep information.
Example:
# Assignment statement
x = 5
y = 10
sum_of_numbers = x + y
print(sum_of_numbers)
Output:
15
x = 5
assigns the value 5 tox
. You can then usex
in calculations.
3. Conditional Statements
These allow your program to make decisions based on conditions. It’s like asking, “Is it raining? If yes, take an umbrella.”
Example:
# Conditional statement
temperature = 25
if temperature > 30:
print("It's hot!")
else:
print("It's a nice day!")
Output:
It's a nice day!
Python checks if
temperature > 30
. If it’s true, it runs the firstprint()
; otherwise, it runs the second.
4. Looping Statements
Loops are like repeating the same task until you’re done. For example, counting numbers.
Example: For Loop
# For loop
for number in range(1, 6):
print(f"Number: {number}")
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
The loop starts at 1 and ends at 5, printing each number.
Example: While Loop
# While loop
count = 1
while count <= 5:
print(f"Count is: {count}")
count += 1
Output:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
The loop runs as long as
count <= 5
. After each iteration,count
increases by 1.
5. Function Definition Statements
Functions group code that performs a task. Imagine it as a machine where you input something, and it gives an output.
Example:
# Function definition
def greet(name):
return f"Hello, {name}!"
print(greet("John"))
Output:
Hello, John!
The
greet()
function takes aname
and returns a greeting message.
6. Import Statements
Python has libraries (pre-built code) you can use. The import
statement brings these into your program.
Example:
# Import statement
import math
result = math.sqrt(16)
print(f"The square root of 16 is {result}")
Output:
The square root of 16 is 4.0
The
math
library helps with advanced math operations.
7. Try-Except Statements
These handle errors gracefully. Think of it as a safety net.
Example:
# Try-Except statement
try:
number = int(input("Enter a number: "))
print(f"You entered: {number}")
except ValueError:
print("That's not a number!")
Output (if input is abc
):
That's not a number!
If the input isn’t a number, the
except
block runs instead of crashing the program.
8. Pass Statements
The pass
statement does nothing—it’s a placeholder. It’s useful when you’re planning to write code later.
Example:
# Pass statement
if True:
pass
Output:
No output—Python does nothing.
9. Break and Continue Statements
These control how loops behave. break
stops a loop, and continue
skips to the next iteration.
Example: Break
for number in range(1, 10):
if number == 5:
break
print(number)
Output:
1
2
3
4
The loop stops completely when
number
is 5.
Example: Continue
for number in range(1, 6):
if number == 3:
continue
print(number)
Output:
1
2
4
5
The number 3 is skipped, but the loop continues.
10. Return Statements
Used inside functions to send a result back.
Example:
# Return statement
def square(num):
return num * num
result = square(4)
print(f"Square of 4 is {result}")
Output:
Square of 4 is 16
The
return
sendsnum * num
back to where the function was called.
Quick Summary Table
Type of Statement | Purpose | Example |
---|---|---|
Expression | Evaluate something | 3 + 5 |
Assignment | Store values in variables | x = 10 |
Conditional | Make decisions | if x > 5: |
Looping | Repeat tasks | for i in range(5): |
Function Definition | Group code into a function | def add(a, b): |
Import | Use external libraries | import math |
Try-Except | Handle errors | try: ... except: |
Pass | Do nothing temporarily | pass |
Break/Continue | Control loop behavior | break , continue |
Return | Return results from functions | return result |