Python 3 – Nested IF Statements
In Python, conditional statements are used to execute a block of code only if certain conditions are met. The nested IF statement is used when another IF statement is required within an IF statement. This allows for additional conditions to be checked before executing a block of code. In this article, we will explore nested IF statements in Python 3.
Syntax of Nested IF Statements
The syntax of nested IF statements in Python is similar to the syntax of the regular IF statement. The main difference is that another IF statement is present within the first IF block. The general syntax of nested IF statements is as follows:
if condition:
# code to execute if condition is true
if another_condition:
# code to execute if both conditions are true
In the above syntax, the first IF statement is evaluated, and if the condition is true, then the code block following the first IF is executed. If another_condition is also true, then the code within the nested IF statement is executed.
Example of Nested IF Statements
Consider the following example to understand nested IF statements in Python:
x = 10
if x > 5:
print("x is greater than 5")
if x < 15:
print("x is less than 15")
In the above code, we have used nested IF statements. The first IF statement checks if x is greater than 5. If the condition is true, the code block following the first IF statement is executed, which is to print “x is greater than 5”. The second IF block checks if x is less than 15. If this condition is also true, the code block within the second IF statement is executed, which is to print “x is less than 15”. Here, we have used nested IF statements to check more than one condition.
Multiple Nested IF Statements in Python
We can use multiple nested IF statements to check more than two conditions. Consider the following example:
x = 10
y = 8
z = 15
if x > 5:
print("x is greater than 5")
if y > 7:
print("y is greater than 7")
if z < 20:
print("z is less than 20")
In the above code, we have used three nested IF statements. The first IF statement checks if x is greater than 5. If the condition is true, the code block following the first IF statement is executed, which is to print “x is greater than 5”. The second IF statement checks if y is greater than 7. If this condition is also true, the code block within the second IF statement is executed, which is to print “y is greater than 7”. The third IF statement checks if z is less than 20. If all the conditions are true, the code block within the third IF statement is executed, which is to print “z is less than 20”.
Nested IF-ELSE Statements
We can also use nested IF-ELSE statements if we want to execute different blocks of code depending on the conditions. Consider the following example:
x = 10
y = 3
if x > 5:
print("x is greater than 5")
if y > 7:
print("y is greater than 7")
else:
print("y is less than or equal to 7")
In the above code, we have used nested IF-ELSE statements. The first IF statement checks if x is greater than 5. If the condition is true, the code block following the first IF statement is executed, which is to print “x is greater than 5”. The second IF statement checks if y is greater than 7. If this condition is true, the code block within the second IF statement is executed, which is to print “y is greater than 7”. If the condition is false, the ELSE block is executed, which is to print “y is less than or equal to 7”.
Conclusion
In this article, we have learned how to use nested IF statements in Python. We have seen the syntax of nested IF statements and how to use them with examples. We have learned how to use multiple nested IF statements and nested IF-ELSE statements. Nested IF statements are useful when we need to check more than one condition before executing a block of code. They allow us to write more complex programs that implement conditional logic.