9 If Statement
1) if-else:
We can implement decision making in our code using if-else statement. It is widely used statement to check the condition and take decisions accordingly.
if-block contains condition (numeric or logical condition)
The statements inside if-block are only executed when defined if-block condition is True.
The statements inside else-block are executed when if-block condition is False.
if-block or else-block statements are defined below respective blocks by providing 4 spaces i.e called 1 indentation.
Syntax:
1 2 3 4 5 | # -- if-else condition syntax --
if (numeric or logical condition):
# if-block statements here ...
else:
# else-block statements here ...
|
Lets implement traffic light decision making using if-else statement.
💻 Example 1:
1 2 3 4 5 6 | # -- if block execution --
color='red'
if color=='red':
print('Stop')
else:
print('Go')
|
✅ Output:
Stop
💻 Example 2:
1 2 3 4 5 6 | # -- else block execution --
color='yellow'
if color=='red':
print('stop')
else:
print('Wait')
|
✅ Output:
Wait
💻 Example 3:
1 2 3 4 5 6 | # -- if-else example with numeric condition --
num=3
if num%2==0:
print('even number')
else:
print('odd number')
|
✅ Output:
odd number
2) if-elif-else:
We can also implement multiple conditions, after first if condition using elif clause.
💻 Example 1:
1 2 3 4 5 6 7 8 | # -- Example1: elif block execution --
color = 'yellow'
if color == 'red':
print('Stop')
elif color == 'yellow':
print('Wait')
else:
print('Go')
|
✅ Output:
Wait
💻 Example 2:
1 2 3 4 5 6 7 8 | # -- Example2: elif block execution --
num = 100
if num > 100:
print('number is greater than 100')
elif num%2==0:
print('number is even')
else:
print('number is odd and less than 100')
|
✅ Output:
number is even
If num = 102 in above code, it will provide the first print statement as output, however number 102 is also even number. So, above example is not logically correct implementation. It can be corrctly implemented using nested if-else statements.
3) Nested if-else:
We can also implement if else statement inside another if else statements, those are called nested if-else statement.
💻 Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 | num = int(input('enter number :'))
if num%2==0:
if num > 100:
print('num is even and greater than 100')
else:
print('num is even and less than 100')
else:
if num < 100:
print('num is odd and less than 100')
else:
print('num is odd and greater than 100')
|
✅ Output:
enter number :111
num is odd and greater than 100
enter number :99
num is odd and less than 100
enter number :50
num is even and less than 100
enter number :102
num is even and greater than 100
if condition is always True when non-empty variables used as if-block condition.
💻 Example 1:
1 2 3 4 5 | a = 35
if a:
print('variable a is having value')
else:
print('variable a is empty')
|
✅ Output:
variable a is having value
💻 Example 2:
1 2 3 4 5 | mylist = [1,2,3]
if mylist:
print('mylist contains elements')
else:
print('mylist is empty')
|
✅ Output:
mylist contains elements
Lets check if-block condition with empty variables.
💻 Example 1:
1 2 3 4 5 6 | # ---- Example1: if-block condition variable with zero value ----
a = 0
if a:
print('a is non-empty or non-zero or not None')
else:
print('a is empty or zero or None')
|
✅ Output:
a is empty or zero or None
💻 Example 2:
1 2 3 4 5 6 | # ---- Example2: if-block condition variable with None value ----
a = None
if a:
print('a is non-empty or non-zero or not None')
else:
print('a is empty or zero or None')
|
✅ Output:
a is empty or zero or None'
💻 Example 3:
1 2 3 4 5 6 | # ---- Example2: if-block condition variable with empty value ----
a = []
if a:
print('a is non-empty or non-zero or not None')
else:
print('a is empty or zero or None')
|
✅ Output:
a is empty or zero or None
4) Single line if-else:
We can also implement if-else condition in single line.
Syntax:
1 2 3 | # Single line if-else syntax
value1 if condition else value2
|
If condition specified after if-block is True, then value1 is considered otherwise value2 will be considered.
💻 Example 1:
1 2 3 4 5 6 7 8 9 | # -- True if condition --
a = 50
b = 'fifty' if a==50 else 'not fifty'
print(b)
# -- False if condition --
a = 51
b = 'fifty' if a==50 else 'not fifty'
print(b)
|
✅ Output:
fifty
not fifty
💻 Example 2:
1 2 3 4 5 6 7 8 9 | # -- True if condition --
num=10
odd_even = 'even' if num%2==0 else 'odd'
print(odd_even)
# -- False if condition --
num=11
odd_even = 'even' if num%2==0 else 'odd'
print(odd_even)
|
✅ Output:
even
odd
Be prepared with below questions prior to your interview !
Frequently asked Interview Questions
What is the use of if keyword in Python?
How do you write a basic if statement in Python?
Why we need to use the elif keyword?
Can we have multiple conditions in an if statement?
What is the difference between if and elif statements?
Can we write an if statement without an else block?
What are nested if statements in Python?
Can we write if-else condition in a single line?
What is an indentation in python?
What is indentation error?