21/10/2024, 13:10 Python
Python - Lecture 14 : If-Else Loop
If-else Statements
Sometimes the programmer needs to check the evaluation of certain expression(s), whether the expression(s)
evaluate to True or False. If the expression evaluates to False, then the program execution follows a different
path than it would have if the expression had evaluated to True.
Based on this, the conditional statements are further classified into following types:
if
if-else
if-else-elif
nested if-else-elif.
An if……else statement evaluates like this:
if the expression evaluates True:
Execute the block of code inside if statement. After execution return to the code out of the if……else block.\
if the expression evaluates False:
Execute the block of code inside else statement. After execution return to the code out of the if……else
block.
Example:
applePrice = 210
budget = 200
if (applePrice <= budget):
print("Alexa, add 1 kg Apples to the cart.")
else:
print("Alexa, do not add Apples to the cart.")
Output:
Alexa, do not add Apples to the cart.
elif Statements
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9oe2ac7a0702c84166a55e0fac14ff2b3e 1/3
21/10/2024, 13:10 Python
Sometimes, the programmer may want to evaluate more than one condition, this can be done using an elif
statement.
Working of an elif statement
Execute the block of code inside if statement if the initial expression evaluates to True. After execution return
to the code out of the if block.
Execute the block of code inside the first elif statement if the expression inside it evaluates True. After
execution return to the code out of the if block.
Execute the block of code inside the second elif statement if the expression inside it evaluates True. After
execution return to the code out of the if block.
.
.
.
Execute the block of code inside the nth elif statement if the expression inside it evaluates True. After
execution return to the code out of the if block.
Execute the block of code inside else statement if none of the expression evaluates to True. After execution
return to the code out of the if block.
Example:
num = 0
if (num < 0):
print("Number is negative.")
elif (num == 0):
print("Number is Zero.")
else:
print("Number is positive.")
Output:
Number is Zero.
Nested if statements
We can use if, if-else, elif statements inside other if statements as well.
Example:
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9oe2ac7a0702c84166a55e0fac14ff2b3e 2/3
21/10/2024, 13:10 Python
num = 18
if (num < 0):
print("Number is negative.")
elif (num > 0):
if (num <= 10):
print("Number is between 1-10")
elif (num > 10 and num <= 20):
print("Number is between 11-20")
else:
print("Number is greater than 20")
else:
print("Number is zero")
Output:
Number is between 11-20
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9oe2ac7a0702c84166a55e0fac14ff2b3e 3/3