Python If/Elif/Else Statements
If/Else Decision Making With Python
Python is a programming language created by Guido van Rossum in 1991. It is one of the most widely used programming language. Like many other programming languages like Java, C and C++, python supports the usual logical conditions from mathematics. Logical conditions like equals(a == b), not equals to(a !=b), less than(a < b), greater than(a>b) are widely used in programming. These logical conditions can be used in several ways such as, “if statements” and loops. There are few ways to use if statements.
1. Using if keyword
if keyword is used to compare two variables or constants. A condition is placed, if the condition is true, the following statement is executed. If statement works as follows:
if condition:
#statement is executed if condition is true
Below is an example of using if keyword in python.
if (10 > 5):
print(“statement1”)
In the above example, “statement1” is printed because the given condition, that is 10 > 5 is true.
2. Using if-else
if-else is also used to compare two variables or constants, but if the condition is false, then the statement in else block gets executed. If-else statement works as follows:
if condition:
#statement is executed if condition is true
else:
#statement is executed if condition is false
Below is an example of using if-else keyword in python.
if (10 < 5):
print(“statement1”)
else:
print(“statement2”)
In the above example, “statement2” is printed because the given condition, that is 10 < 5 is false.
3. Using if-elif-else
if-elif-else ladder is used when there are multiple options. if first condition is false, it checks second condition, if it is also false, then it goes through the else statement. if-elif-else statement works as follows:
if condition1:
#statement is executed if condition1 is true
elif condition2:
#statement is executed if condition1 is false and condition2 is true
else:
#statement is executed if both, condition1 and condition2 are false
Below is an example of using if-elif-else ladder in python.
if (10 < 5):
print(“statement1”)
elif (10 == 5):
print(“statement2”)
else:
print(“statement3”)
In the above example, “statement3” gets printed because the first condition, that is (10 > 5) and the second condition, that is (10 < 5), are false. If any of the condition is true, statement in only that block is executed. Statement in else is only executed when all the above conditions are false.
4. Using if-elif
if-elif is similar to if-elif-else, but there is no else block. It works if any of the given conditions is true. if-elif statement works as follows:
if condition1:
#statement is executed if condition1 is true
elif condition2:
#statement is executed if condition1 is false and condition2 is true
Below is an example of using if-elif ladder in python.
if (10 < 5):
print(“statement1”)
elif (10 == 5):
print(“statement2”)
In the above example, nothing gets printed because none of the given conditions is true.
Please note a few points while using if statements:
1. There cannot be else without if.
2. Any number of elif can be used after an if and before an else.
3. Else should be used in the end only.
4. Else is optional after if or elif.
Responses