If Else Statements in Python
Conditional statements are an essential part of any programming language, and Python is no exception. In Python, we can use the
if-else
statement to create conditional logic. The
if-else
statement allows us to execute one block of code if a particular condition is true, and another block of code if the condition is false.
The If Statement
The
if
statement in Python is used to test a particular condition. If the condition is true, the code inside the
if
block will be executed. For example:
# Example using if statement
x = 10
if x > 5:
print("x is greater than 5")
In the above example, the
if
statement tests if
x
is greater than 5. Since the condition is true, the code inside the block is executed, and the message "x is greater than 5" is printed to the console.
The If-Else Statement
The
if-else
statement allows us to execute one block of code if a condition is true, and another block of code if the condition is false. The syntax for the
if-else
statement in Python is:
# Example using if-else statement
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
In the above example, the
if
statement tests if
x
is greater than 5. Since the condition is false, the code inside the
else
block is executed, and the message "x is less than or equal to 5" is printed to the console.
The If-Elif-Else Statement
Sometimes we need to test multiple conditions and execute different code blocks based on which condition is true. For this purpose, we can use the
if-elif-else
statement. The syntax for the
if-elif-else
statement in Python is:
# Example using if-elif-else statement
x = 3
if x > 5:
print("x is greater than 5")
elif x > 0:
print("x is greater than 0 but less than or equal to 5")
else:
print("x is less than or equal to 0")
In the above example, the first
if
statement tests if
x
is greater than 5. Since the condition is false, the second
elif
statement tests if
x
is greater than 0. Since the condition is true, the code inside the
elif
block is executed, and the message "xis greater than 0 but less than or equal to 5" is printed to the console.
Nested If Statements
We can also nest
if
statements within other
if
statements to create more complex conditional logic. For example:
# Example using nested if statements
x = 10
if x > 0:
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5 but greater than 0")
else:
print("x is less than or equal to 0")
In the above example, the outer
if
statement tests if
x
is greater than 0. Since the condition is true, the inner
if
statement tests if
x
is greater than 5. Since the condition is true, the code inside the inner
if
block is executed, and the message "x is greater than 5" is printed to the console.
Conclusion
The
if-else
statement is a powerful tool in Python that allows us to create conditional logic in our programs. By understanding the different types of conditional statements and how to use them, we can create more complex and useful programs.