Introduction to if-else Statement in Python
To test which conditions are either TRUE or FALSE amongst two or more, the Python programming language provides a logical statement structure of the form IF-ELIF-ELSE which can be considered as the form if statement followed by one or more else if represented as elif in Python. Finally, it is followed by an else statement so as to validate desired conditions against certain criteria or assumptions that allow the user to perform a required task based on the output returned, based on the context from the IF ELSE logical statement in Python.
Syntax in Python:
if <condition1>:
<body1>
elif <condition2>:
<body2>
...
elif <conditionn>:
<bodyn>
...
else:
<bodyelse>
The tab always indents the body of the if-else statement.
There are various logical conditions, which can be used inside the if-else statement for evaluation. Let’s say p and q are variables defined in the python environment. The condition could be:
- p == q.
- p != q
- p > q
- p < q
- p >= q
- p <= q
Let’s see these conditions in examples coming below.
Flowchart:
As shown in the flowchart, first, the test expression gets evaluated. If it is “True”, a specified set of statements gets executed. However, if it results in “False”, another specified set of statements gets executed.
- Here is the node representation, which can be converted into the if-else statement.
if (A) {
XX
} elif (B) {
YY
} else{
ZZ
}
Examples of if-else Statement in Python
Let’s see some examples to understand if-else more:
- Code:
var = 100
if var>90:
print("true expression executed")
else:
print("false expression executed")
Output:
As one can see, var = 100. This gets checked in if condition for a value greater than 100, then printed “true expression executed”. However, if a value is less than or equal to 90, see below how this code would have behaved.
var = 90
if var>90:
print("true expression executed")
else:
print("false expression executed")
There could use one-liner code for the above logic.
Code:
var = 100
print("true expression executed") if var>90 else print("false expression executed")
Output:
- Now let’s see some comparison of variables.
Code:
p = 90
q = 20
if p > q:
print("true expression executed")
elif q > p:
print("false expression executed")
else:
print("Nothing")
Output:
As one can see, p and q are two variable that holds value 90 and 20 respectively. These are getting compared in if and else and accordingly printing the respective statements. The last else statement will execute when neither p > q nor q > p when both variables hold equal values, that time last else will get executed.
Like:
- Now let’s see the use of the logical operator in an if-else statement:
Code:
p = 2000
q = 3330
r = 600
if p > r and q > r:
print("Both conditions are True")
else:
print("Nothing")
Output:
As one can notice, if a statement contains logical operators “And” between two expressions, i.e. p>r and q>r. Then, as if the statement is true, it resulted in printing “Both conditions are True”.
The same way it can be done for “OR”.
Code:
p = 2000
q = 30
r = 600
if p > r or q > r:
print("Any one expression is true")
else:
print("Nothing")
Output:
Here one can notice, only one expression is true, i.e. p>r and not q>r. But because of Or operator, if statement resulted True and printed “Anyone expression is true”.
However, more than one variable and logical operator can be used to evaluate expressions in the if-else statement.
Code:
p = 2000
q = 30
r = 600
s = 60
t = 60
if (p > r or q > r) and (s == t):
print("True is this")
else:
print("Nothing")
Output:
- Now let’s see an if-else statement with a pass operator.
There are ample situations where one doesn’t know what logic to put at the writing program’s start. The pass operator is the one that is used at that time.
Code:
r or q > r):
pass
else:
print("Nothing")
Output:
As one can see, nothing printed. That’s because if a statement is evaluated true, and it has a pass operator in it.
Conclusions
As we saw above, how important is the concept of if-else? Knowing its ways of implementation in your code will helps you get a good understanding of it. If one is already familiar with the if-else statement in another computer language, then it becomes really easy to understand if-else in python. Concept wise remains the same; syntactically, it varies in python.
Practically when someone has multiple conditions, one should go for if-else if statements rather than a nested if-else statement. This will make the code more readable and cleaner. Same way, the more one practices various if-else, the more one will get to know about the easiness and practicality of it.
However, one can look for a dictionary concept as an alternative to the if-else statement.
Recommended Articles
This is a guide to if else Statement in Python. Here we discuss the basic concept, examples of if else Statement in Python, along with the flowchart. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses