美文网首页
Chapter 05:if语句

Chapter 05:if语句

作者: 蜜糖_7474 | 来源:发表于2018-12-11 19:00 被阅读0次

与C不同,python用and or not表示条件连接

age_0 = 18
age_0 >=17 and age_0 <= 22

True

age_0 >=3 or age_0 <= -2

True

in/not in 可以检查特定元素是否存在于列表

vegetables = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' in vegetables)
print('pepperoni' in vegetables)

True
False

if-elif-else 结构,elif可以有多个,else可以为0

age = 12
if age < 4:
    print("free!")
elif age >= 4 and age < 18:
    print("half-price")
else:
    print("full-price")  

half-price

if判断一个空列表返回False

void_list = []
if void_list:
    print("list not empty")
else:
    print("list empty")

list empty

相关文章

网友评论

      本文标题:Chapter 05:if语句

      本文链接:https://www.haomeiwen.com/subject/gjwehqtx.html