Python if else


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:

OperatorNameExampleDescription
==Equal toa == bReturns True if a is equal to b else False.
!=Not Equal toa != bReturns True if a is not equal to b else False.
>Greater thana > bReturns True if a is greater then b else False.
<Less thana < bReturns True if a is less then b else False.
>=Greater than or equal toa >= bReturns True if a is greater then equal to b else False.
<=Less than or equal toa <= 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.

Python if Statement

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.

In [1]:
rating  = 9
salary = 5000

if rating > 8:
    salary = salary + 100

print(salary)
5100

If condition when performance rating is 7, there is no increment in salary.

In [2]:
rating  = 7
salary = 5000

if rating > 8:
    salary = salary + 100

print(salary)   
5000

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.

Python if else Statement

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.

In [1]:
rating = 9
salary = 5000
bonus = 0

if rating > 8:
    bonus = 100
else:
    bonus = 50

salary = salary + bonus

print(salary)
5100

When performance rating is 7, else block gets executed.

In [2]:
rating = 7
salary = 5000
bonus = 0

if rating > 8:
    bonus = 100
else:
    bonus = 50

salary = salary + bonus

print(salary)
5050

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.

Python if elif else Statement

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.

In [1]:
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)
5100

When the performance rating is 7, first elif block is executed.

In [2]:
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)
5060

When the performance rating is 5, second elif block is executed.

In [3]:
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)
5040

When the performance rating is 3, only else is executed.

In [4]:
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)
5020

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.

In [1]:
rating  = 9
salary = 5000

if rating > 8:
    salary = salary + 100

print(salary)
5100

Bad example.

In [2]:
rating  = 9
salary = 5000

if rating > 8:
salary = salary + 100

print(salary)
  File "<ipython-input-2-459e8340057e>", line 5
    salary = salary + 100
         ^
IndentationError: expected an indented block

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

In [1]:
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)
5100

Salary = 3000 so bonus = 80

In [2]:
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)
3080

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.

In [1]:
rating  = 9
salary = 3000

if rating > 8 and salary > 4000:
    salary = salary + 100

print(salary)
3000

OR

To evaluate two or more conditions and any of them should be true.

Bonus is given as rating satisfies the condition.

In [1]:
rating  = 9
salary = 3000

if rating > 8 or salary > 4000:
    salary = salary + 100

print(salary)
3100

NOT

To reverse the result of the condition.

Bonus is not given if rating is less than 6

In [1]:
rating  = 9
salary = 3000

if not(rating < 6):
    salary = salary + 100

print(salary)
3100
In [2]:
rating  = 5
salary = 3000

if not(rating < 6):
    salary = salary + 100

print(salary)
3000

References

  1. Python if…else Statement
  2. Python If-else statements Examples