Python if-else statement#

It is likely that we will want the program to do something even when an if statement evaluates to false. In our grade example of previous tutorial, we will want output whether the grade is passing or failing.

x = 1

if x > 3:
    print ("Case 1")
if x <= 3:
    print ("Case 2")
Case 2

The if-else statement checks the condition and executes the if block of code when the condition is True, and if the condition is False, it will execute the else block of code.

Syntax :#

if condition:
    statement 1
else:
    statement 2
  1. The if..else statement evaluates condition and will execute the body of if only when the test condition is True.

  2. If the condition is False, the body of else is executed. Indentation is used to separate the blocks.

To do this, we will add an else statement to the grade condition above that is constructed like this:

>>> grade = 60   
>>> if grade >= 65:
>>>     print("Passing grade")
>>> else:
>>>     print("Failing grade")

Failing grade

Since the grade variable above has the value of 60, the if statement evaluates as false, so the program will not print out Passing grade. The else statement that follows tells the program to do something anyway.

If we then rewrite the program to give the grade a value of 65 or higher, we will instead receive the output Passing grade.

# Example 1:

grade = 60
    
if grade >= 65:
    print("Passing grade")
else:
    print("Failing grade")
Failing grade
# Example 2: Program checks if the number is positive or negative and displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")
Positive or Zero

Explanation

In the above example, when num is equal to 3, the test expression is true and the body of if is executed and the body of else is skipped.

If num is equal to -5, the test expression is false and the body of else is executed and the body of if is skipped.

If num is equal to 0, the test expression is true and body of if is executed and body of else is skipped.

# Example 3: program to check if a num1 is less than num2

num1, num2 = 6, 5
if (num1 < num2):
    print("num1 is less than num2")
else:
    print("num2 is less than num1")
num2 is less than num1
# Example 4:

def password_check(password):
    if password == "Python@99>":
        print("Correct password")
    else:
        print("Incorrect Password")

password_check("Python@99>")
# Output Correct password

password_check("Python99")
# Output Incorrect Password
Correct password
Incorrect Password

Shortcut of if else (Short Hand if … else or One line if else)#

If you have only one statement each for if and else, then they can be put in the same line. This can be done as shown below

hungry = True
x = 'Feed the bear now!' if hungry else 'Do not feed the bear.'
print(x)
Feed the bear now!
a = 3
print('A is positive') if a > 0 else print('A is negative') # first condition met, 'A is positive' will be printed
A is positive
num1, num2 = 6, 5
print("num1 is less than num2") if (num1 < num2) else print("num2 is less than num1")
num2 is less than num1
number = 96
if number > 0: print("positive") 
else: print("negative")
positive
x = 12
if 10 < x < 11:
    print("hello")
else:
    print("world")
world
'''
x = 1

if x > 3:
    print ("Case 1")
if x <= 3:
    print ("Case 2")
'''

x = 1

if x > 3:
    print ("Case 1")
else:
    print ("Case 2")
Case 2
x = 2

if x > 4:
    print ("Correct")
else:
    print("Incorrect")
Incorrect

The difference…#

Now, let us try to execute the same above discussed code with different input values.

#program to check if num1 is less than num2

num1, num2 = 5, 5
if (num1 < num2):
    print("num1 is less than num2")
else:
    print("num2 is less than num1")

'''
Output: num2 is lesser than num1
'''
num2 is less than num1
'\nOutput: num2 is lesser than num1\n'

Explanation:

  • Why was the output wrong?

Since both the input values are equal, the condition num1<num2 is False. So immediately the control shifts to else block and the print statement inside it gets executed. However, this is not logically correct as both the input values are equal (5 = 5).

  • How to rectify this?

Your program should logically include every possible test case and only then your program will produce accurate results. So this mean you need to add another conditional statement to check if num1 is equal to num 2. Let us now see how this can be done.

💻 Exercises ➞ List#

  1. Get user input using input(“Enter your age: ”). If user is 18 or older, give feedback: You are old enough to drive. If below 18 give feedback to wait for the missing amount of years.

Output:

    Enter your age: 30
    You are old enough to learn to drive.
    Output:
    Enter your age: 15
    You need 3 more years to learn to drive.
  1. Compare the values of my_age and your_age using if-else. Who is older (me or you)? Use input(“Enter your age: ”) to get the age as input. You can use a nested condition to print 'year' for 1 year difference in age, 'years' for bigger differences, and a custom text if my_age = your_age.

Output:

    Enter your age: 30
    You are 5 years older than me.