Conditional Statements
Conditional statements allow a Python program to make decisions. Based on whether a condition is True or False, Python executes the appropriate block of code.
What is a Condition?
A condition is an expression that produces either True or False.
age = 25
print(age >= 18)
Output
True
Since the value of age is 25, the condition age >= 18 is true.
Conditional Decision Flow
Example: marks >= 40
Yes → Execute if block
Execute else / next condition
1. if Statement
The if statement executes a block of code only when the specified condition is true.
Syntax
if condition:
statement
Notice the colon : after the condition. The statement inside the block must be indented.
Example
marks = 80
if marks >= 40:
print("Student Passed")
Output
Student Passed
What Happens When the Condition is False?
If the condition is false, Python simply skips the code inside the if block.
marks = 30
if marks >= 40:
print("Student Passed")
print("Program Completed")
Output
Program Completed
2. if-else Statement
The else block executes when the if condition is false.
Syntax
if condition:
statement
else:
statement
Example
marks = 35
if marks >= 40:
print("Pass")
else:
print("Fail")
Output
Fail
3. if-elif-else Statement
When a program needs to check multiple conditions, Python provides the elif keyword.
Syntax
if condition1:
statement
elif condition2:
statement
else:
statement
Grade Example
marks = 82
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 40:
print("Grade C")
else:
print("Fail")
Output
Grade A
Python checks the conditions from top to bottom. Once it finds the first true condition, its block executes and the remaining conditions are skipped.
Multiple elif Conditions
You can use multiple elif blocks when the application has several possible outcomes.
temperature = 30
if temperature > 35:
print("Very Hot")
elif temperature > 25:
print("Warm")
elif temperature > 15:
print("Cool")
else:
print("Cold")
Output
Warm
Nested if Statements
An if statement placed inside another if statement is called a nested conditional statement.
Nested conditions are useful when a second decision depends on the result of the first decision.
age = 25
has_id = True
if age >= 18:
if has_id:
print("Entry Allowed")
else:
print("ID Required")
else:
print("Entry Not Allowed")
Output
Entry Allowed
Conditions using Comparison Operators
Comparison operators are commonly used inside conditional statements.
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | age == 25 |
| != | Not equal to | age != 25 |
| > | Greater than | marks > 50 |
| < | Less than | marks < 50 |
| >= | Greater than or equal to | marks >= 40 |
| <= | Less than or equal to | marks <= 100 |
Conditions using Logical Operators
Logical operators allow multiple conditions to be combined into a single expression.
Using and
age = 25
attendance = 85
if age >= 18 and attendance >= 75:
print("Eligible")
Using or
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("Weekend")
Using not
is_logged_in = False
if not is_logged_in:
print("Please Login")
Conditions with Strings
Conditional statements can also compare strings.
username = "Samadhan"
if username == "Samadhan":
print("Welcome Samadhan")
else:
print("User Not Found")
Conditions using in
The in operator can check whether a value exists inside a collection or string.
course = "Python Full Stack"
if "Python" in course:
print("Python course selected")
Conditions using Boolean Values
Boolean variables can be directly used in conditional statements.
is_active = True
if is_active:
print("Account is Active")
else:
print("Account is Inactive")
Short-Hand if
A simple one-line condition can sometimes be written on a single line.
age = 25
if age >= 18: print("Adult")
This style is suitable only for very simple statements. For larger logic, normal indentation is easier to read.
Conditional Expression
Python also supports a one-line conditional expression for simple choices.
age = 25
status = "Adult" if age >= 18 else "Minor"
print(status)
Output
Adult
Example: Course Eligibility 📩🤓
Suppose a student wants to join a course at CIIT Training Institute 🤓. The application can check the student's qualification and programming experience.
qualification = "Graduate"
experience = 1
if qualification == "Graduate" and experience >= 1:
print("Eligible for Advanced Training")
elif qualification == "Graduate":
print("Eligible for Beginner Training")
else:
print("Please check eligibility requirements")
Output
Eligible for Advanced Training
Example: Login Check 📩🤓
Conditional statements are commonly used in login and authentication logic.
username = "sam"
password = "python123"
if username == "sam" and password == "python123":
print("Login Successful")
else:
print("Invalid Username or Password")
Output
Login Successful
Importance of Indentation
Python uses indentation to identify which statements belong to a conditional block.
marks = 80
if marks >= 40:
print("Pass")
print("Good Job")
Both statements are part of the if block because they have the same indentation level.
Common Mistakes
- Forgetting the colon : after if, elif, or else.
- Using = instead of == for comparison.
- Incorrect indentation inside the condition.
- Writing conditions in the wrong order.
- Creating unnecessarily complicated nested conditions.
Why Condition Order Matters
In an if-elif-else structure, Python checks conditions from top to bottom. Therefore, the order of conditions matters.
marks = 95
if marks >= 40:
print("Pass")
elif marks >= 90:
print("A+")
Output:
Pass
Here, marks >= 40 is already true, so Python executes that block and never reaches the elif. More specific conditions should generally be checked before broader conditions.
CIIT Learning Point
Conditional statements are the foundation of decision-making in programming. Practice if, if-else, and if-elif-else with different conditions. Then combine them with comparison and logical operators to solve real-world problems.
CIIT Practice Task 1
Create a Python program that accepts a student's marks and displays whether the student has passed or failed.
marks = int(input("Enter marks: "))
if marks >= 40:
print("Pass")
else:
print("Fail")
CIIT Practice Task 2
Create a program that accepts age and displays:
- Below 13 → Child
- 13 to 19 → Teenager
- 20 to 59 → Adult
- 60 and above → Senior Citizen
age = int(input("Enter age: "))
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior Citizen")
Output:
Enter age: 25
Adult
CIIT Practice Task 3
Build a simple login validation program using username and password. Display Login Successful when both values are correct; otherwise display Invalid Login.
Summary :
Conditional statements allow Python programs to make decisions based on conditions. You learned if, if-else, if-elif-else, nested conditions, comparison and logical operators, string and boolean conditions, conditional expressions, indentation, condition ordering, and real-world examples such as login validation and course eligibility.