Introduction
In Python, the if statement is used to execute a block of code only when a specified condition is true. It allows programs to make decisions and perform different actions based on different situations.
The if statement is the foundation of control flow in Python. Without it, programs would execute instructions sequentially without being able to respond to changing conditions.
What is Python If Statement?
The Python if statement is a conditional statement used to execute a block of code when a specified condition evaluates to True.
If the condition evaluates to False, the code inside the if block is skipped.
Example:
age = 20
if age >= 18:
print("You are eligible to vote.")
Output:
Since the condition age >= 18 is true, the code inside the if block executes.
Syntax:
The basic syntax of an if statement is:
if condition:
# code block
Syntax Breakdown
- if is a Python keyword.
- condition is an expression that returns True or False.
- A colon (:) follows the condition.
- Indented code executes when the condition is true.
Simple If Statement Example
temperature = 35
if temperature > 30:
print("It's a hot day.")
Output:
The condition evaluates to True, so Python executes the print statement.
If Statement with False Condition
temperature = 20
if temperature > 30:
print("It's a hot day.")
Output:
Since the condition is false, Python skips the code block.
Using Comparison Operators in If Statements
Comparison operators are commonly used with if statements.
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example:
marks = 75
if marks >= 50:
print("Pass")
Output:
Using Logical Operators in If Statements
Logical operators help combine multiple conditions.
| Operator | Description |
|---|---|
| and | Both conditions must be True |
| or | At least one condition must be True |
| not | Reverses the condition |
Example Using AND
age = 25
if age > 18 and age < 60:
print("Working Age Group")
Output:
Example Using OR
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("Weekend")
Output:
Example Using NOT
is_logged_in = False
if not is_logged_in:
print("Please log in.")
Output:
Using Boolean Values
If statements can directly evaluate Boolean variables.
Example:
is_member = True
if is_member:
print("Member Access Granted")
Output:
Multiple Statements Inside If Block
You can include multiple lines of code inside an if block.
Example:
salary = 50000
if salary > 30000:
print("Eligible for Loan")
print("Submit Documents")
print("Application Processing Started")
Output:
All statements execute because the condition is true.
Nested If Statement
An if statement can be placed inside another if statement.
Example:
age = 25
if age >= 18:
if age >= 21:
print("Eligible for Adult Membership")
Output:
If Statement with User Input
Example:
age = int(input("Enter your age: "))
if age >= 18:
print("You can vote.")
Output:
This demonstrates how if statements work with user-provided data.
Example: Check Positive Number
number = 10
if number > 0:
print("Positive Number")
Output:
Example: Check Even Number
number = 8
if number % 2 == 0:
print("Even Number")
Output:
The modulus operator checks whether the remainder is zero.
Example: Password Validation
password = "python123"
if len(password) >= 8:
print("Valid Password")
Output:
Real-life Examples:
1. Online Shopping
Suppose an e-commerce website offers free shipping for orders above ₹1000.
order_amount = 1500
if order_amount > 1000:
print("Free Shipping Applied")
Output:
This type of condition is commonly used in online stores.
2. ATM Withdrawal
balance = 10000
withdraw_amount = 5000
if withdraw_amount <= balance:
print("Transaction Successful")
Output:
Banks use similar conditions to validate transactions.
3. Student Result System
marks = 75
if marks >= 40:
print("Pass")
Output:
Educational software frequently uses if statements.
Advantages of If Statements
1. Decision Making
Allows programs to make intelligent decisions.
2. Flexible Execution
Code executes only when required.
3. Better User Experience
Applications respond dynamically to user actions.
4. Improved Program Logic
Helps organize business rules clearly.
5. Essential for Automation
Used extensively in automated systems.
Common Mistakes
1. Missing Colon
Incorrect:
age = 20
if age >= 18
print("Adult")
Output:
Correct:
if age >= 18:
print("Adult")
2. Incorrect Indentation
Incorrect:
age = 20
if age >= 18:
print("Adult")
Output:
Correct:
if age >= 18:
print("Adult")
3. Using Assignment Instead of Comparison
Incorrect:
if age = 18:
print("Adult")
Output:
Correct:
if age == 18:
print("Adult")
4. Forgetting Type Conversion
Incorrect:
age = input("Enter age: ")
if age >= 18:
print("Adult")
Output:
Correct:
age = int(input("Enter age: "))
5. Using Wrong Logical Conditions
Incorrect:
age = 25
if age > 18 and age > 60:
print("Eligible")
The condition may not produce the intended result.
Always verify logical expressions carefully.
Conclusion
The Python if statement is one of the most important control flow statements in programming. It allows programs to make decisions and execute code based on specific conditions. From simple validations to complex business logic, if statements play a crucial role in creating dynamic and intelligent applications.