Python If-Else Statements
The if-else
statement is one of the most fundamental and frequently used concepts in programming. It is used to make decisions in your code. Based on the evaluation of a given condition, the program can take different actions. If the condition is True
, the program will execute the block of code under the if
statement, and if the condition is False
, it will execute the block of code under the else
statement.
What is an if-else Statement?
An if-else
statement is a simple conditional structure that allows your program to execute one block of code when a condition is true and another block when it is false. It helps in decision-making by controlling the flow of execution.
The syntax of an if-else
statement typically looks like this:
Syntax of if-else Statement
Syntax
if condition:
# Code block to execute if condition is True
else:
# Code block to execute if condition is False
Explanation:
In the above syntax, the condition
is a boolean expression that evaluates to either True
or False
. Based on the evaluation of the condition, either the code inside the if
block or the else
block will be executed.
If the condition
is true, the code within the if
block runs. If the condition
is false, the code inside the else
block runs instead.
Example
x = 10
y = 20
if x == y:
print("x is equal to y")
else:
print("x is not equal to y")
Output
In this example, the condition x == y
is evaluated. Since 10 == 20
is False
, the program prints x is not equal to y
.
What is the Difference Between if-else and elif?
In some cases, you may want to check multiple conditions. The elif
(short for "else if") statement is used when you want to check more than two conditions. The if-elif-else
structure is useful when you need to test several expressions for true or false.
Syntax of if-elif-else Statement
Syntax
if condition1:
# Code block to execute if condition1 is True
elif condition2:
# Code block to execute if condition2 is True
else:
# Code block to execute if none of the conditions are True
Explanation:
With if-elif-else
, you can check multiple conditions. If the first condition is false, it moves on to the next condition, and so on. The else
block at the end runs if all the conditions are false.
Example
# Example:
x = 10
y = 20
if x > y:
print("x is greater than y")
elif x < y:
print("x is less than y")
else:
print("x is equal to y")
Output
In this example, the first condition checks if x > y
, which is false. So, the program moves on to check the next condition, x < y
, which is true. Therefore, it prints x is less than y
.
Nested if Statements
Sometimes, you may need to test conditions within another condition. This is known as a "nested if" statement. It allows you to add more layers of decision-making.
Syntax of Nested if Statements
Syntax
if condition1:
# Code block to execute if condition1 is True
if condition2:
# Code block to execute if condition2 is True
else:
# Code block to execute if condition2 is False
else:
# Code block to execute if condition1 is False
Example
# Example:
x = 10
y = 20
if x == 10:
if y == 20:
print("x is 10 and y is 20")
else:
print("x is 10 but y is not 20")
else:
print("x is not 10")
Output
In this example, the first condition checks if x == 10
, which is true. Then, it checks if y == 20
, which is also true, so it prints x is 10 and y is 20
.
Best Practices
- Use elif for multiple conditions: If you have more than two possible conditions, use
elif
. - Maintain readability: Indent properly and use meaningful variable names.