Python Programming
python condition
Python Conditional Statements
Introduction
Programs often need to make decisions.
Examples:
- If age is 18 or more → Allow voting
- If marks are above 40 → Pass
- If balance is sufficient → Allow withdrawal
- If password is correct → Login
Conditional statements allow programs to make decisions.
What is a Conditional Statement?
A conditional statement executes code only when a condition is True.
Example:
age = 20
if age >= 18:
print("Eligible to vote")
Output:
Eligible to vote
Understanding Conditions
A condition always evaluates to:
True
or
False
Examples:
print(10 > 5)
print(10 < 5)
Output:
True
False
Types of Conditional Statements
Python provides:
- if
- if-else
- if-elif-else
- Nested if
- Multiple if Statements
- Conditional Expressions (Ternary Operator)
The if Statement
Used when a block should run only if a condition is True.
Syntax:
if condition:
statement
Example:
age = 18
if age >= 18:
print("You can vote")
Output:
You can vote
if Condition False
age = 15
if age >= 18:
print("You can vote")
Output:
No output
Because condition is False.
Flow of if Statement
Condition
↓
True ? ---- Yes ----> Execute Block
|
No
↓
Skip Block
Multiple Statements Inside if
age = 20
if age >= 18:
print("Adult")
print("Can vote")
print("Can apply for license")
Output:
Adult
Can vote
Can apply for license
if-else Statement
Used when exactly one of two choices should execute.
Syntax:
if condition:
code
else:
code
Example
age = 16
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
Output:
Not Eligible
Real Life Example
password = "python123"
if password == "python123":
print("Login Successful")
else:
print("Wrong Password")
Output:
Login Successful
Flow of if-else
Condition
↓
True ? ---- Yes ----> if Block
|
No
↓
else Block
if-elif-else Statement
Used when there are multiple conditions.
Syntax:
if condition1:
code
elif condition2:
code
elif condition3:
code
else:
code
Example
marks = 85
if marks >= 90:
print("Grade A+")
elif marks >= 80:
print("Grade A")
elif marks >= 70:
print("Grade B")
else:
print("Grade C")
Output:
Grade A
Important Rule
Only the first matching condition executes.
Example:
marks = 95
if marks >= 90:
print("A+")
elif marks >= 80:
print("A")
elif marks >= 70:
print("B")
Output:
A+
Remaining conditions are skipped.
Grade Calculator Example
marks = 72
if marks >= 90:
print("A+")
elif marks >= 80:
print("A")
elif marks >= 70:
print("B")
elif marks >= 60:
print("C")
elif marks >= 40:
print("Pass")
else:
print("Fail")
Output:
B
Multiple if Statements
Each condition is checked independently.
Example:
age = 25
if age > 10:
print("Greater than 10")
if age > 20:
print("Greater than 20")
if age > 30:
print("Greater than 30")
Output:
Greater than 10
Greater than 20
Difference Between if-elif and Multiple if
if-elif
Only one block executes.
score = 95
if score >= 90:
print("Excellent")
elif score >= 80:
print("Very Good")
Output:
Excellent
Multiple if
All matching blocks execute.
score = 95
if score >= 90:
print("Excellent")
if score >= 80:
print("Very Good")
Output:
Excellent
Very Good
Nested if Statement
An if inside another if.
Syntax:
if condition1:
if condition2:
code
Example
age = 20
citizen = True
if age >= 18:
if citizen:
print("Eligible to vote")
Output:
Eligible to vote
Nested if-else Example
age = 20
if age >= 18:
print("Adult")
if age >= 21:
print("Can enter club")
else:
print("Cannot enter club")
else:
print("Minor")
Output:
Adult
Cannot enter club
Multi-Level if Statement
A multi-level if means decisions occurring in multiple layers.
Example:
username = "admin"
password = "1234"
if username == "admin":
if password == "1234":
print("Login Successful")
else:
print("Wrong Password")
else:
print("Wrong Username")
Output:
Login Successful
Multi-Level Banking Example
balance = 10000
withdraw = 5000
if balance > 0:
if withdraw <= balance:
print("Transaction Successful")
else:
print("Insufficient Balance")
else:
print("Account Empty")
Output:
Transaction Successful
Using Logical Operators in Conditions
and
Both conditions must be True.
age = 20
citizen = True
if age >= 18 and citizen:
print("Eligible")
Output:
Eligible
or
At least one condition must be True.
marks = 35
sports_quota = True
if marks >= 40 or sports_quota:
print("Selected")
Output:
Selected
not
Reverses True and False.
is_banned = False
if not is_banned:
print("Access Granted")
Output:
Access Granted
Comparison Operators Used in Conditions
| Operator | Meaning |
|---|---|
| == | Equal |
| != | Not Equal |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal |
| <= | Less Than or Equal |
Truthy and Falsy Values
Python treats some values as False.
Falsy values:
False
None
0
0.0
''
[]
{}
set()
Example:
name = ""
if name:
print("Name Found")
else:
print("Empty")
Output:
Empty
Ternary Operator (Conditional Expression)
Short form of if-else.
Syntax:
value_if_true if condition else value_if_false
Example:
age = 20
message = "Adult" if age >= 18 else "Minor"
print(message)
Output:
Adult
Common Mistakes
Mistake 1: Using = Instead of ==
❌ Wrong
if age = 18:
Output:
SyntaxError
✅ Correct
if age == 18:
Mistake 2: Missing Colon
❌ Wrong
if age >= 18
print("Adult")
Output:
SyntaxError
✅ Correct
if age >= 18:
print("Adult")
Mistake 3: Incorrect Indentation
❌ Wrong
if age >= 18:
print("Adult")
Output:
IndentationError
✅ Correct
if age >= 18:
print("Adult")
Mistake 4: Using Multiple if Instead of if-elif
❌ Wrong
marks = 95
if marks >= 90:
print("A+")
if marks >= 80:
print("A")
Output:
A+
A
Sometimes only one grade is expected.
✅ Better
if marks >= 90:
print("A+")
elif marks >= 80:
print("A")
Mistake 5: Wrong Range Logic
❌ Wrong
age = 20
if age >= 18 or age <= 60:
print("Working Age")
This condition is almost always True.
✅ Correct
if age >= 18 and age <= 60:
print("Working Age")
Mistake 6: Comparing Different Data Types
❌ Wrong
age = input("Enter Age: ")
if age >= 18:
print("Adult")
Output:
TypeError
✅ Correct
age = int(input("Enter Age: "))
if age >= 18:
print("Adult")
Best Practices
Keep Conditions Simple
❌ Avoid
if (((age >= 18 and citizen == True) and verified == True)):
✅ Better
if age >= 18 and citizen and verified:
Use Meaningful Variables
is_logged_in = True
is_verified = True
if is_logged_in and is_verified:
print("Access Granted")
Use if-elif for Multiple Choices
if grade == "A":
pass
elif grade == "B":
pass
else:
pass
Homework 1
Check whether a number is positive.
Solution
number = 10
if number > 0:
print("Positive")
Homework 2
Check whether a number is even or odd.
Solution
number = 7
if number % 2 == 0:
print("Even")
else:
print("Odd")
Homework 3
Create a grade calculator.
Rules:
90+ = A+
80+ = A
70+ = B
60+ = C
Below 60 = Fail
Solution
marks = 85
if marks >= 90:
print("A+")
elif marks >= 80:
print("A")
elif marks >= 70:
print("B")
elif marks >= 60:
print("C")
else:
print("Fail")
Homework 4
Check voting eligibility.
Conditions:
Age >= 18
Citizen = True
Solution
age = 20
citizen = True
if age >= 18 and citizen:
print("Eligible")
else:
print("Not Eligible")
Homework 5
Create a login system.
Requirements:
Username = admin
Password = 1234
Use nested if.
Solution
username = "admin"
password = "1234"
if username == "admin":
if password == "1234":
print("Login Successful")
else:
print("Wrong Password")
else:
print("Wrong Username")
Homework 6
Use a ternary operator to determine whether a person is Adult or Minor.
Solution
age = 17
result = "Adult" if age >= 18 else "Minor"
print(result)
Output:
Minor
Homework 7
Check whether a year is a leap year.
Hint:
year % 4 == 0
Solution
year = 2024
if year % 4 == 0:
print("Leap Year")
else:
print("Not Leap Year")
Quick Revision
| Statement | Purpose |
|---|---|
| if | Execute when True |
| if-else | Choose between two options |
| if-elif-else | Multiple choices |
| Nested if | Decision inside another decision |
| Multiple if | Check all conditions |
| Ternary Operator | Short if-else |
Conditional Statement Flow
if
│
├── True → Execute
│
└── False → Skip
if-else
│
├── True → if block
│
└── False → else block
if-elif-else
│
├── Condition 1
├── Condition 2
├── Condition 3
└── else
Summary
| Concept | Description |
|---|---|
| if | Executes when condition is True |
| else | Executes when condition is False |
| elif | Additional conditions |
| Nested if | if inside another if |
| Multi-Level if | Decision tree structure |
| Logical Operators | and, or, not |
| Comparison Operators | ==, !=, >, <, >=, <= |
| Ternary Operator | One-line if-else |
| Truthy/Falsy | Values treated as True or False |
Conditional statements are the foundation of decision-making in Python and are heavily used in:
- Login Systems
- Banking Applications
- Games
- Web Applications
- APIs
- Data Validation
- Automation Scripts
Master conditional statements before moving to loops (for and while) because loops frequently depend on conditions.