28 Aug  Admin

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

x = 22
y = 160

if y > x:
  print(“y is greater than x”)

Output

y is greater than x

Elif

The elif keyword in pythons is used to check “if the previous conditions were not true, then try this condition”.

Example

a = 12
b = 18
if a == b:
 print(“a and b are equal”)


elif b > a:

print(“b is greater than a”)
  

Output

b is greater than a