Python if else
Many times we have to run our code based on some conditions. For example, if the age is 18 or greater than 18, then only the person can apply for a driving license. In this case, our software should have a condition for age and should process applications only if the age is 18 or greater than 18.
Let us understand it with the help of a real-world example. Your manager wants to give you a yearly bonus, and he would like to decide this based on your yearly rating. If the rating is less than 6, then 50 dollars, if between 6 and 8, then 100 dollars and 200 dollars for more than 8. So, this can be achieved easily with the help of an if-else statement.
Comparison Operators
With the if else statement, we can use any of the following comparison operators:
Operator | Name | Example | Description |
---|---|---|---|
== | Equal to | a == b | Returns True if a is equal to b else False. |
!= | Not Equal to | a != b | Returns True if a is not equal to b else False. |
> | Greater than | a > b | Returns True if a is greater then b else False. |
< | Less than | a < b | Returns True if a is less then b else False. |
>= | Greater than or equal to | a >= b | Returns True if a is greater then equal to b else False. |
<= | Less than or equal to | a <= b | Returns True if a is less then equal to b else False. |
if
Statement
A simple if
statement is used when we want to execute some lines of code only in case the expression for the if
statement is true, otherwise is no action.
Example: If an employee’s yearly rating is 8 and more, we would like to give a bonus of $100. We can simply write an if condition to achieve this.
If condition when performance rating is 9, salary gets incremented by 100 dollar.
rating = 9
salary = 5000
if rating > 8:
salary = salary + 100
print(salary)
If condition when performance rating is 7, there is no increment in salary.
rating = 7
salary = 5000
if rating > 8:
salary = salary + 100
print(salary)
if else
Statement
With the if-else
statement, we have an additional else
block along with the if
block. This else
block is executed when the expression for the if
block is false.
Example: We want to give a bonus to all the employees of the company but if their performance rating is 8+, then it is 100 dollars, else it is 50 dollars.
When performance rating is 9, if
block gets executed.
rating = 9
salary = 5000
bonus = 0
if rating > 8:
bonus = 100
else:
bonus = 50
salary = salary + bonus
print(salary)
When performance rating is 7, else
block gets executed.
rating = 7
salary = 5000
bonus = 0
if rating > 8:
bonus = 100
else:
bonus = 50
salary = salary + bonus
print(salary)
if elif else
Statement
We may want to evaluate our code for multiple expressions, the elif
statement facilitates us with that. We can have one if
block, as many elif
blocks as we need, and one else
block. If the condition for the if
block is false, then the condition for the elif
block will be evaluated. After evaluating the condition for all elif
blocks, if all are false, the else
block will be executed.
Example: If performance rating is 8+ then a bonus is 100 dollars, if 6+ then a bonus is 60 dollars, if 4+ then 40 dollars and else 20 dollars.
When the performance rating is 9, if
bock gets executed.
rating = 9
salary = 5000
bonus = 0
if rating > 8:
bonus = 100
elif rating > 6:
bonus = 60
elif rating > 4:
bonus = 40
else:
bonus = 20
salary = salary + bonus
print(salary)
When the performance rating is 7, first elif
block is executed.
rating = 7
salary = 5000
bonus = 0
if rating > 8:
bonus = 100
elif rating > 6:
bonus = 60
elif rating > 4:
bonus = 40
else:
bonus = 20
salary = salary + bonus
print(salary)
When the performance rating is 5, second elif
block is executed.
rating = 5
salary = 5000
bonus = 0
if rating > 8:
bonus = 100
elif rating > 6:
bonus = 60
elif rating > 4:
bonus = 40
else:
bonus = 20
salary = salary + bonus
print(salary)
When the performance rating is 3, only else
is executed.
rating = 3
salary = 5000
bonus = 0
if rating > 8:
bonus = 100
elif rating > 6:
bonus = 60
elif rating > 4:
bonus = 40
else:
bonus = 20
salary = salary + bonus
print(salary)
Role of Indentation
Indentation plays a significant role in the if-else statement. Python uses white spaces at the beginning of the line to mark the scope of the block.
Good example.
rating = 9
salary = 5000
if rating > 8:
salary = salary + 100
print(salary)
Bad example.
rating = 9
salary = 5000
if rating > 8:
salary = salary + 100
print(salary)
Nested If
We can also use an if statement inside the if statement. This is known as a nested if statement.
Example: If performance rating is 8+ and if salary is 4000+, then the bonus is 100 and if salary is less than 400, then it is 80.
Salary = 5000 so bonus = 100
rating = 9
salary = 5000
bonus = 0
if rating > 8:
#Nested If
if salary > 4000:
bonus = 100
else:
bonus = 80
elif rating > 6:
bonus = 60
elif rating > 4:
bonus = 40
else:
bonus = 20
salary = salary + bonus
print(salary)
Salary = 3000 so bonus = 80
rating = 9
salary = 3000
bonus = 0
if rating > 8:
#Nested If
if salary > 4000:
bonus = 100
else:
bonus = 80
elif rating > 6:
bonus = 60
elif rating > 4:
bonus = 40
else:
bonus = 20
salary = salary + bonus
print(salary)
Logical Operators
We can also combine two or more conditions using the Logical Operators. Any of the AND, OR and NOT can be used.
AND
To evaluate two or more conditions and both should be true.
No bonus is given as salary does not satisfy the condition.
rating = 9
salary = 3000
if rating > 8 and salary > 4000:
salary = salary + 100
print(salary)
OR
To evaluate two or more conditions and any of them should be true.
Bonus is given as rating satisfies the condition.
rating = 9
salary = 3000
if rating > 8 or salary > 4000:
salary = salary + 100
print(salary)
NOT
To reverse the result of the condition.
Bonus is not given if rating is less than 6
rating = 9
salary = 3000
if not(rating < 6):
salary = salary + 100
print(salary)
rating = 5
salary = 3000
if not(rating < 6):
salary = salary + 100
print(salary)
Resources
These are the Free Notes shared with the YouTube Video.
**Note: All links are Password Protected. For the PASSWORD, please follow the steps in the DESCRIPTION of the respective Video.
Chapter 4 - Conditionals and Loops
Linked video : https://youtu.be/HYQHk803sL4