Introduction to Conditional Statements in Python
Instead of a sequential execution, we often need to execute a specific code based on a conditional, and that’s where “Conditional statements are utilized in Python.” It also helps in decision-making in Python, preferably when we prefer to execute a piece of code only if certain conditionals are met. In a nutshell, a conditional statement directs the order of execution of the statements mentioned as a part of it. There are different types of control statements in Python. Let’s discuss those, one by one, along with Syntax and How they work.
Understanding if Conditional Statement in Python
We will quickly start with the most basic version of the if conditional statement. The basic syntax is mentioned as below:
Syntax:
if <conditional>:
<Statement to be executed if the conditional evaluates to be True>
- Here <conditional> can be any condition based on a combination of one or more variables.
- The output of this conditional is a boolean expression that evaluates to be either 0 or 1 and based on the same statement or the set of statements mentioned under this conditional is executed
- If this conditional evaluates to be true then the statement is executed else skipped
- The colon(:) following the <conditional> is required, else python will throw an error.
Examples of Conditional Statements in Python
Let’s go through the below examples in order to understand the same.
Example #1
Code:
#Python program to understand if statement
var1 = 1
var2 = 200
if var2 > var1:
print("var2 is greater than var1")
Output:
As the conditional statement turned out to be true, thus the above statement got executed.
Example #2
What will happen if the conditional does not evaluate to be true? Let’s go through the below example in order to understand the same.
Code:
#Python program to understand if statement
var1 = 1000
var2 = 200
if var2 > var1:
print("var2 is greater than var1")
This program won’t provide any output at all. The reason being, the conditional evaluated to be false, and hence the print statement was skipped as it was dependent on the conditional.
Example #3
Understanding the usage of else clause with if Conditional Statement.
Syntax:
if <conditional>:
<Statement 1 to be executed if the conditional evaluates to be True>
else:
<Statement 2 to be executed if the conditional evaluates to be False>
- In this case, if the conditional evaluates to be true then statement 1 gets executed, else if the conditional evaluates to be false then statement 2 gets executed.
- That’s how the else clause works with the if conditional.
Let’s go through the below example in order to understand the same.
Code:
var1 = 1000
var2 = 200
if var2 > var1:
print("var2 is greater than var1")
else:
print("var1 is greater than var2")
Output:
In this case, the conditional was evaluated to be false thus statement 1 got skipped, and due to the same reason, statement 2 got executed. What if we have more than one condition to verify and based on that we wish to execute a different set of code. Elif clause can be utilized in that case to check for the subsequent conditions and then finally an else statement can be used to execute a set of a statement if nothing evaluates to be true.
Example #4
Understanding the usage of elif clause with If Conditional Statement.
Syntax:
if <conditional 1>:
<Statement 1 to be executed if the conditional 1 evaluates to be True>
elif <conditional 2>:
<Statement 2 to be executed if the conditional 2 evaluates to be True and conditional 1 evaluates to be false>
else:
<Statement 3 to be executed if the conditional evaluates to be conditional 1 and 2 evaluates to be false>
- In this case, conditional 1 evaluates to be true then statement 1 gets executed, else if conditional 2 evaluates to be true then statement 2 gets executed, else statement 2 gets executed.
- That’s how the elif clause works with the if conditional if in case we want to test for multiple conditions.
Let’s go through the below example in order to understand the same.
Code:
var1 = 1000
var2 = 1000
if var2 > var1:
print("var2 is greater than var1")
elif var2 == var1:
print("var1 is equal to var2")
else:
print("var1 is greater than var2")
Output:
- In this case, conditional 1 evaluated to be false thus statement 1 got skipped.
- Conditional 2 evaluated to be true, thus statement 2 got executed.
- Finally, the rest of the subsequent conditional won’t even be checked for if any of the conditional evaluates to be true.
There is an alternate syntax as well for the if conditional in python and that too in a single line itself. It’s user’s own choice of which one to opt for.
Syntax:
if <conditional 1>: <Statement 1 to be executed if the conditional 1 evaluates to be True>
It is totally permissible to write the statement as well in a single line, moreover, there can be multiple statements as well aligned to a single conditional as well as mentioned below:
if <conditional 1>: <Statement 1 to be executed if the conditional 1 evaluates to be True>; <Statement 2 to be executed if the conditional 1 evaluates to be True> ; <Statement 3 to be executed if the conditional 1 evaluates to be True>
These statements need to be separated by a semi-colon (;) though.
Conclusion
Python conditional statement is quite useful when it comes to 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 Python conditional statements like and, or, not, etc. To evaluate these conditionals. The statements to be run must be indented by a tab else python will throw an error.
Recommended Articles
This is a guide to Conditional Statements in Python. Here we discuss the Introduction to Conditional Statements in Python along with examples and code implementation. You may also have a look at the following articles to learn more –