与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
网友评论