Python

Python If Else statement

Python if-else statement is used in cod for decision making. More often, decision making is required to execute a certain piece of code if a particular condition is true.

In Python, the intended purpose of using if-else is decision making. The syntax of the if-else statement in Python is similar to most programming languages like Java, C, C++, and C#.

This article describes the Python if-else statements in detail.

Syntax

if test_expression:
     statement(s) to execute
else:
     statement(s) to execute

In the if condition, test expression is evaluated. The test expression can be called a condition as well. In case of a true condition, the statement of if block is executed, otherwise, the program flow is passed to the else condition. The flow diagram depicts the execution of the if-else statement.

Python if else statement examples

Let’s see an example of an if-else in Python. In the provided example, a num variable is declared for a number with a value of 13. If condition checks whether the number is greater than 10 or not. If the number value is more than 10, then the if block body is executed, and it prints that the number is greater than 10. In case if the number is less than 10, then else block is executed, and it prints that the number is less than 10.

num = 13
# applying condition
if num>10:
    print("The number is greater than 10")
# declaring the else statement
else:
    print("The number is less than 10")

Output
The output shows that the number 13 is greater than 10.


Now, let’s see an example of a number that is less than 10.

num = 3
# declaring the if statement to check whether the number is greater than 10 or not
if num>10:
    print("The number is greater than 10")
else:
    print("The number is less than 10")

Output

The elif statement in Python

Python allows us to evaluate or check the multiple test expressions by using the elif statement. The elif is the short form for else if statement. We can use the mathematical operators to evaluate the condition like =, !=(not equal), <, >, etc.

In the elif statement, if the first if condition is not true, the program will evaluate the next elif block condition and so forth. In case if all the conditions are false, then the else condition will be executed at last.

Let’s see an example of this.

# declaring a name variable
name = input("Enter the name of animal\n")
if name=="cow":
    print("You entered cow")
elif name == "dog":
    print("You entered Dog")
elif name == "cat":
    print("You entered cat")
else:
    print("This is the else block")

Output

Nested if condition in Python

We can declare multiple if conditions inside an if condition. This phenomenon is called the nested if condition. Let’s see an example of this.

# declaring an age variable
age = 13
if age>10:
    print("You are above 10")
    # an if condition inside the if condition
    if age >12:
        print("You are also above 12")
    if age == 13:
        print("You are 13 years old")
    else:
     print("You are not 13 years old")
else:
     print("You are less than 10 years old")

Output

Conclusion

This article explains the if-else statement in Python by using simple examples. If-else statement is used to evaluate the test expression.

About the author

Kamran Sattar Awaisi

I am a software engineer and a research scholar. I like to write article and make tutorial on various IT topics including Python, Cloud Computing, Fog Computing and Deep Learning. I love to use Linux based operating systems.