Saturday, July 2, 2022

Python - If... Else

# Fun with if conditions

a = 33
b = 200
if b > a:
  print("b is greater than a")

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

a = 200
b = 33
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")

a = 200
b = 33
if b > a:
  print("b is greater than a")
else:
  print("b is not greater than a")

print("---")

a = 200
b = 33

# Short Hand If
if a > b: print("a is greater than b")

# Short Hand If ... Else
a = 2
b = 330
print("A") if a > b else print("B")

print("-----")

# AND

a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")

# Parentheses seems to work
if (a > b) and (c > a):
  print("Both conditions are True")
 
a = 200
b = 33
c = 500
if a > b or a > c:
  print("At least one of the conditions is True")

print("<><><><>")

x = 41

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")

# If statements cannot be empty but we can use pass
a = 33
b = 200

if b > a:
    # Do nothing
    # Python won't let me just have comments here
    # I have to use the pass statement
    pass