B1. Python if...else Statement
In Python, the if...else
statement is a conditional statement that allows you to execute different code blocks based on whether a certain condition is true or false. The basic syntax of an if...else
statement is as follows:
if condition:
# code to execute if condition is true
else:
# code to execute if condition is false
Here, condition
is a boolean expression that evaluates to either True
or False
. If the condition is true, the code block inside the if
statement is executed, and if the condition is false, the code block inside the else
statement is executed.
You can also use elif
(short for “else if”) statements to chain multiple conditions together, like this:
if condition1:
# code to execute if condition1 is true
elif condition2:
# code to execute if condition2 is true and condition1 is false
else:
# code to execute if both condition1 and condition2 are false
In this case, if condition1
is true, the code block inside the first if
statement is executed, and the rest of the elif
and else
statements are skipped. If condition1
is false, then condition2
is checked.
If condition2
is true, the code block inside the second elif
statement is executed, and if both condition1
and condition2
are false, the code block inside the else
statement is executed.
Example for Python if..else :
Beow is an example of how to use the if...else
statement in Python:
# Check if a number is even or odd
num = 4
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
In this example, we’re checking if the value of num
is even or odd. We use the modulo operator (%
) to check if num
is divisible by 2 with no remainder.
If the remainder is 0, then num
is even and the code block inside the if
statement is executed, which prints “The number is even.” If the remainder is not 0, then num
is odd and the code block inside the else
statement is executed, which prints “The number is odd.” In this case, since num
is 4, which is even, the output will be “The number is even.”
Python if..elif..else explained :
The if...elif...else
statement in Python allows you to evaluate multiple conditions and execute different blocks of code based on the result of those conditions. It’s similar to the if...else
statement, but allows you to test additional conditions if the first condition is false.
The basic syntax of an if...elif...else
statement is as follows:
if condition1:
# code to execute if condition1 is true
elif condition2:
# code to execute if condition2 is true and condition1 is false
elif condition3:
# code to execute if condition3 is true and both condition1 and condition2 are false
else:
# code to execute if all conditions are false
Here, condition1
, condition2
, and condition3
are boolean expressions that evaluate to either True
or False
. If condition1
is true, the code block inside the first if
statement is executed, and the rest of the elif
and else
statements are skipped. If condition1
is false, then condition2
is checked. If condition2
is true, the code block inside the second elif
statement is executed, and so on.
If none of the conditions are true, the code block inside the else
statement is executed.
Example for Python if…elif…else :
Here’s an example of how to use the if...elif...else
statement in Python:
# Check if a number is positive, negative, or zero
num = -4
if num > 0:
print("The number is positive.")
elif num == 0:
print("The number is zero.")
else:
print("The number is negative.")
In this example, we’re checking if the value of num
is positive, negative, or zero. We use if
to check if num
is greater than 0, and if it is, we print “The number is positive.”
If num
is not greater than 0, we move on to the elif
statement, where we check if num
is equal to 0, and if it is, we print “The number is zero.” If num
is not greater than 0 and not equal to 0, we move on to the else
statement, where we print “The number is negative.” In this case, since num
is -4, the output will be “The number is negative.”
Nested if..else in Python :
In Python, a nested if
statement is an if
statement that is inside another if
statement. This is useful when you need to test multiple conditions and execute different blocks of code based on the result of those conditions.
The basic syntax of a nested if
statement is as follows:
if condition1:
# code to execute if condition1 is true
if condition2:
# code to execute if condition1 and condition2 are true
else:
# code to execute if condition1 is true and condition2 is false
else:
# code to execute if condition1 is false
Here, condition1
and condition2
are boolean expressions that evaluate to either True
or False
. If condition1
is true, the code block inside the first if
statement is executed, and then the code inside the nested if
statement is executed if condition2
is also true. If condition1
is true but condition2
is false, the code block inside the else
statement is executed.
If condition1
is false, the code block inside the else
statement is executed.
Example for Nested if..else in python :
Here’s an example of how to use a nested if
statement in Python:
# Check if a number is positive, negative, or zero and even or odd
num = -4
if num >= 0:
if num % 2 == 0:
print("The number is positive and even.")
else:
print("The number is positive and odd.")
else:
if num % 2 == 0:
print("The number is negative and even.")
else:
print("The number is negative and odd.")
In this example, we’re checking if the value of num
is positive, negative, or zero, and whether it’s even or odd. We use a nested if
statement to check if num
is greater than or equal to 0.
If it is, we check if num
is even or odd using the modulo operator (%
). If num
is even, we print “The number is positive and even.” If num
is odd, we print “The number is positive and odd.”
If num
is not greater than or equal to 0, we move on to the else
statement. If num
is even, we print “The number is negative and even.” If num
is odd, we print “The number is negative and odd.” In this case, since num
is -4, which is negative and even, the output will be “The number is negative and even.”