What is an if Statement?
The if statement allows us to execute a block of code only when a specified condition is True.
Syntax:
if condition:
# Code executes if condition is True
else:
# Code executes if condition is False
Example 1: Rain Condition
raining = False
if raining == True:
print("Take Umbrella")
else:
print("No need for Umbrella")
Output:
No need for Umbrella
Here, the condition is False, so the else block executes.
Example 2: Age Verification
user_age = 18
if user_age >= 18:
print("Permission Granted")
else:
print("No Permission")
Output:
Permission Granted
Since the user's age is 18, the condition is True.
Note: We can also write if raining: instead of if raining == True:.
if, elif and else
When we have multiple conditions to check, we can use elif.
- if checks the first condition.
- elif checks another condition if the previous condition is False.
- else executes when all previous conditions are False.
Example: Movie Censor Rules
Movie certification categories include U, UA and A.
Let's consider a simplified example where a person must be 18 or older, or have a parent accompanying them.
movie_censor = 'UA'
age = 16
parent_came = True
if age >= 18:
print("Unrestricted")
elif parent_came == True:
print("Allowed")
else:
print("Not Allowed")
Output:
Allowed
Here:
- Age is 16, so the first condition is False.
- A parent has accompanied the person, so the second condition is True.
- Therefore, "Allowed" is printed.
Electricity Bill Calculation Using if-elif
We can use conditional statements to calculate electricity charges based on units consumed.
Example:
units_consumed = 350
if units_consumed <= 100:
print("Free Electricity")
elif units_consumed <= 400:
amount = units_consumed * 4.7
print(amount)
elif units_consumed <= 500:
amount = units_consumed * 6.3
print(amount)
elif units_consumed <= 600:
amount = units_consumed * 8.4
print(amount)
else:
print("Units exceed the given range")
Output:
1645.0
Finding Grades Using if-elif-else
We can use conditions to assign grades based on marks.
Example:
mark = 89
if mark >= 90:
print("A+ Grade")
elif mark >= 75:
print("A Grade")
elif mark >= 60:
print("B Grade")
elif mark >= 35:
print("C Grade")
else:
print("Re Appear")
Output:
A Grade
How does it work?
- 90 or above → A+ Grade
- 75 or above → A Grade
- 60 or above → B Grade
- 35 or above → C Grade
- Below 35 → Re Appear
Python checks the conditions from top to bottom and executes the first matching block.
Nested if Statements
An if statement inside another if statement is called a nested if statement.
Example: Physical Test and Written Test
physical_test = True
written_test = True
if physical_test == True:
if written_test == True:
print("Send them appointment order")
else:
print("Physical Test Pass, Written Test - Fail")
else:
print("Physical Test - Not Clear")
Output:
Send them appointment order
How does it work?
- First, Python checks the physical test.
- If the physical test is passed, it checks the written test.
- If both tests are passed, it prints the appointment message.
- If either test fails, the corresponding message is printed.
Nested if with a Cricket Example
Let's consider an India vs South Africa cricket match.
quarter_finals = "LOST"
if quarter_finals == "WIN":
print("IND Vs SA")
ind = 270
sa = 267
if ind > sa:
print("India enters Semi finals")
else:
print("SA Enters Semi Finals")
Output:
No output.
Why?
Because quarter_finals is "LOST", so the first condition is False. The code inside the if block is skipped.
Logical Operators in Conditional Statements
Python supports three logical operators:
- and
- or
- not
AND Operator
The and operator returns True only when both conditions are True.
Example:
a = False
b = True
if a and b:
print("Hi")
else:
print("Hello")
Output:
Hello
OR Operator
The or operator returns True when at least one condition is True.
Example:
a = False
b = True
if a and b:
print("Hi")
elif a or b:
print("Hello")
else:
print("Vanakkam")
Output:
Hello
NOT Operator
The not operator reverses the Boolean value.
- not True → False
- not False → True
Example:
name = "Subash"
name2 = ""
name3 = " "
if name3:
print("Welcome")
else:
print("Vanakkam")
Output:
Welcome
Why?
- name contains text, so it is truthy.
- name2 is an empty string, so it is falsy.
- name3 contains a space, so it is truthy.
Combining Multiple Conditions
We can combine multiple conditions using logical operators.
Example:
no1, no2, no3 = 5, 10, 15
if no1 < no2 and no2 < no3 or no3 == 100:
print("Hi")
elif no1 > no2 or no2 > no3:
print("Hello")
else:
print("Vanakkam")
Output:
Hi
Understanding the condition
Python evaluates and before or.
The first condition is:
(no1 < no2 and no2 < no3) or no3 == 100
Substituting the values:
(5 < 10 and 10 < 15) or 15 == 100
(True and True) or False
True or False
The final result is True, so "Hi" is printed.
Case-Sensitive String Comparison
Python is case-sensitive.
It means uppercase and lowercase letters are treated differently.
Example:
name1 = 'prem'
name2 = 'Prem'
if name1 == name2:
print("Hi Prem")
else:
print("Hello Prem")
Output:
Hello Prem
Here, 'prem' and 'Prem' are different strings because their first letters have different cases.
Boolean and Integer Comparison
In Python, Boolean values are a subclass of integers.
- True is treated as 1.
- False is treated as 0.
Example:
result = True
if result == 1:
print("One")
elif result == True:
print("True")
else:
print("Hello")
Output:
One
Why?
Because True == 1 evaluates to True. Therefore, the first condition executes.
Top comments (0)