Updated August 19, 2023
Introduction to Python if then else
Python “if then else” is a conditional statement that is used to derive new variables based on several conditionals over the existing ones. This also helps in decision making in Python, preferably when we wish to execute code only if certain conditionals are met. Different logical operators like equal to, less than, greater than, less than equal to, greater than equal to, etc., can be used with if statement. We will discuss this in detail about the conditional operators and how to use them with if then else statement in python.
Syntax:
#Python program to understand the syntax for if then else statement
var1 = 10
var2 = 20
if var2 > var1:
print("var2 is greater than var1")
else:
print("var1 is greater than var2")
Output:
How Does Python if then else Work?
- We declared variables in the start, that is, var1, var2, and assigned them static values, that is, 10 and 20, respectively.
- Then came the if statement, which is comparing var1 and var2 using the greater than the operator
- Based on this logical comparison’s boolean output, the execution of the next indented statement will be decided.
- If the resultant output is true, then the next statement will be executed, and the else statement will be skipped.
- Otherwise, the control will be passed to the else statement, and the statement under that will get executed.
- That is why. it’s called if then else statement. Because of the logical conditional evaluates to be true, then only the first set of statements is executed; else, the second set of statements under the else statement is executed.
- The same is verified by the result of this program as well; if conditional evaluated to be true as var2 (having the value 20) is greater than var1 (having the value as 10), and thus we can see the output as an outcome of the first print statement under if conditional.
Conditional Operators that Can be Used with the if Statement
Let’s discuss in detail the conditional operators that can be used with the if statement to evaluate the boolean expression:
- Equal to(==): In order to check for the equality of two variables, an “equal to” operator is used. It consists of two equal to mathematical signs, and that’s what makes it different from the assignment operator. For example : x == p
- Not Equal to(!=): In order to check for the inequality of two variables, a “Not equal to” operator is used. It consists of a not and an equal to a mathematical sign. For example : x != p
- Less Than( < ): In order to check for the inequality of two variables, particularly if variable1 is less than variable2, a “Less Than” operator is used. It consists of a less than mathematical sign. For example: x < p
- Less Than Equal to( <= ): In order to check for the inequality of two variables, particularly if variable1 is less than equal to variable2, a “Less Than equal to” operator is used. It consists of a less than and an equal to mathematical sign. For example: x <= p
- Greater Than( > ): In order to check for the inequality of two variables, particularly if variable1 is greater than variable2, a “Greater Than” operator is used. It consists of a greater than mathematical sign. For example: x > p
- Greater Than Equal to( >= ): In order to check for the inequality of two variables, particularly if variable1 is greater than equal to variable2, a “Greater Than equal to” operator is used. It consists of a greater than and an equal to mathematical sign. For example: x >= p
How Does Indentation Work with “if then else” Statement in Python?
Lets’ take an example to understand this in detail:
Example #1
Code:
#Python program to understand the indentation for if then else statement
var1 = 110
var2 = 20
if var2 > var1:
print("var2 is greater than var1")
else:
print("var1 is greater than var2")
Output:
This will throw the below error.
The reason being, we need to use an indented piece of code after if and else statement like below:
Code:
#Python program to understand the indentation for if then else statement
var1 = 110
var2 = 20
if var2 > var1:
print("var2 is greater than var1")
else:
print("var1 is greater than var2")
Output:
Example #2 – Use of “AND” logical operator in if then else
If we have more than one condition to check, then, in that case, an AND operator can be used. Let’s take an example to understand the same.
Code:
#Python program to understand the usage of AND operator if then else statement
var1 = 10
var2 = 20
var3 = 30
if var2 > var1 and var3 > var1:
print("var1 is smaller than var2 and var 3")
Output:
Example #3 – Use of “OR” logical operator in if then else
If we have more than one condition to check, then, in that case, an OR operator can also be used. Let’s take an example to understand the same.
Code:
#Python program to understand the usage of OR operator if then else statement
var1 = 10
var2 = 20
var3 = 30
if (var2 > var1) | (var3 > var1):
print("var1 is not the greatest variable")
Output:
Here we have encapsulated the comparative piece of code into circular parenthesis, and to that, OR(|) operator is applied
Conclusion – Python if then else
Python “if then else” conditional statement is quite useful for decision-making in programs to run a certain piece of code based on the values of the conditionals. We have numerous operators that can be used with the “if then else” statement to evaluate these conditionals. A tab must indent the statements to be run; else, Python will throw an error.
Recommended Articles
This is a guide to Python if then else. Here we discuss the Introduction and How Does Python if then else, Work along with different examples and its code implementation. You may also look at the following articles to learn more –