Python If-Else
Python If-Else
Else
PREPARED BY: JAN LEVI O. PANGILINAN
Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if
statements" and loops.
An "if statement" is written by using the if keyword.
Example: In this example we use two
variables, a and b, which are used
a = 33 as part of the if statement to test
b = 200 whether b is greater than a.
As a is 33, and b is 200, we know
that 200 is greater than 33, and so
if b > a:
we print to screen that "b is
print("b is greater than a") greater than a".
OUTPUT: B
You can also have multiple else statements on
the same line:
• One line if else statement, with 3 conditions:
a = 330
b = 330
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
OUTPUT:
Above ten,
and also above 20!
The pass Statement
• if statements cannot be empty, but if you for some reason have an if
statement with no content, put in the pass statement to avoid getting an
error.
a = 33
b = 200
if b > a:
pass
# having an empty if statement like this, would raise an error without the
pass statement