📅  最后修改于: 2023-12-03 15:18:55.084000             🧑  作者: Mango
在Python中,elif语句是if语句的一部分,它用于在多个条件语句中选择一个执行。相对于if语句和else语句,elif语句通常有更多的条件需要判断,它可以使代码更加简洁、优雅。
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
num = 5
if num == 1:
print("num is 1")
elif num == 2:
print("num is 2")
elif num == 3:
print("num is 3")
elif num == 4:
print("num is 4")
else:
print("num is not in 1, 2, 3 or 4")