A conditional statement allows to check several conditions and execute instruction based on whether it will be true or false.
The "if statement" is a conditional statement in Python that checks a condition.
The simple if statement block executes only when satisfied with a specific condition.
age = 18
if age >= 18:
print("The person is eligible to vote.")
The person is eligible to vote.
The above example, we declared a variable age
and assigned 18
as a numeric value. Then after checking the condition with the "if keyword."
If the age
value is greater than or equal to eighteen(18
) age >= 18
, it prints the text "The person is eligible to vote."
Here age
value is eighteen(18
) because, for this reason, it prints "The person is eligible to vote."
The if-else checks the condition if it is true, then it executes the "if block" otherwise it executes the "else block."
age = 15
if age >= 18:
print("The person is eligible to vote.")
else:
print("The person is not eligible to vote.")
The person is not eligible to vote.
The above example, we declared a variable age
and assigned 15
as a numeric value. Then after checking the condition with the "if keyword."
If the age
value is greater than or equal to eighteen(18
) age >= 18
, then print the text "The person is eligible to vote." otherwise, in the else block that print "The person is not eligible to vote."
Here age
value is fifteen(15
) because, for this reason, it prints "The person is not eligible to vote."
An if statement inside another if statement.
A = 20
B = 40
C = 60
if A > B:
if A > C:
print("A value is greater.")
else:
print("C value is greater.")
else:
print("Either the B or C value is greater.")
Either the B or C value is greater.
In the above example, variables A
, B
, and C
declared and assigned values of 20
, 40
, and 60
.
Now, check the condition whether A
is greater than B
(A > B
).
If the first condition is true, then check if A
is greater than C
(A > C
).
If the inner condition is true, then print "A value is greater." otherwise, print "C value is greater." or first condition is false (A > B
), then print "Either the B or C value is greater."
This example prints the text "Either the B or C value is greater." because both (A > B
) and (A > C
) conditions are not satisfied. (A > C
) is not checked because the first condition (A > B
) is not true, so it goes directly to the else part and prints "Either the B or C value is greater."
Using if-elif-else, you can check multiple conditions, and if none match, then the else statements will execute.
number = 1
if number == 0:
print("The number is zero.")
elif number == 1:
print("The number is one.")
else:
print("The number is neither zero nor one.")
The number is one.
In the above example, we declared a variable number
and assigned the value 1
.
Check that the number
is equal to 0
(number == 0
), if that condition is true then print "The number is zero" or if that condition is false then move to the second condition,
and check that the number
is equal to 1
(number == 1
), if that condition is true then print "The number is one" or if that condition is false then move to the else block and print "The number is neither zero nor one."
In the above example, the number
value is 1
since it satisfies the number == 1
condition and prints the text "The number is one."
Here are a few important points about if statements.