33
loading...
This website collects cookies to deliver better user experience
# basic elif syntax
if condition1:
action1
elif condition2:
action2
else:
action3
# elif example
price = 10000 # there should be some int value
if price > 5000:
print("That's too expensive!")
elif price > 500:
print("I can afford that!")
else:
print("That's too cheap!")
pet = 'cat' # or 'dog'?
# cats vs dogs conditional
if pet == 'cat':
print('Oh, you are a cat person. Meow!')
elif pet == 'dog':
print('Oh, you are a dog person. Woof!')
if pet == 'cat':
print('Oh, you are a cat person. Meow!')
if pet == 'dog':
print('Oh, you are a dog person. Woof!')
price = 6000
if price > 5000:
print("That's too expensive!")
if price > 500:
print("I can afford that!")
else:
print("That's too cheap!")
The output is 'That's too expensive!\nI can afford that!'
animal = 'unicorn'
if animal in 'crow, dog, frog, pony':
print('This animal exists')
if animal in 'unicorn, pegasus, pony':
print('This animal is a horse')
# multiple elifs syntax
if condition1:
action1
elif condition2:
action2
# here you can add as many elifs as you need
elif conditionN:
actionN
else:
actionN1
# multiple elifs example
light = "red" # there can be any other color
if light == "green":
print("You can go!")
elif light == "yellow":
print("Get ready!")
elif light == "red":
print("Just wait.")
else:
print("No such traffic light color, do whatever you want")
# nested elifs example
traffic_lights = "green, yellow, red"
# a string with one color
light = "purple" # variable for color name
if light in traffic_lights:
if light == "green":
print("You can go!")
elif light == "yellow":
print("Get ready!")
else:
# if the lights are red
print("Just wait.")
else:
print("No such traffic light color, do whatever you want")