how to add multiple if statements in python code example

Example 1: python if elif

num = 20
if num > 30:
  print("big")
elif num == 30:
  print("same")
else:
  print("small")
#output: small

Example 2: how to add multiple if statements in python

var = 100
if var < 200:
   print "Expression value is less than 200"
   if var == 150:
      print "Which is 150"
   elif var == 100:
      print "Which is 100"
   elif var == 50:
      print "Which is 50"
   elif var < 50:
      print "Expression value is less than 50"
else:
   print "Could not find true expression"

print "Good bye!"

Example 3: if and elif

'''In this program, we input a number
check if the number is positive or
negative or zero and display
an appropriate message
This time we use nested if statement'''

num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Example 4: if and elif

if test expression:
    Body of if
elif test expression:
    Body of elif
else: 
    Body of else

Tags:

Misc Example