top of page

Python: If Else

Conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.

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

An "if statement" is written by using the if keyword.

If statement:

a = 96
b = 500
if b > a:
print("b is greater than a")

Elif
The elif keyword is "if the previous conditions were false, then use this".

a = 55
b = 55
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")

Else
The else keyword catches anything which isn't caught by the preceding conditions.

Example
a = 400
b = 93
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

©2020 by Global Tech Nomads. 
Special thanks to X

bottom of page