Python If&字典 初学者笔记

and 当俩个条件都满足时为True否为False

or 任意一个条件满足时为True否为Flase

not in  通常用于If语句,用来判断一个元素是否在某个列表中

1 banned_user = ['andrew','carolina','david']
2 user = 'marie'
3 if user not in banned_user:
4     print(user.title()+',you can post a response if you wish.')

If elif else 组合使用:

 1 age = input("Please enter his age: ")#input()获取的输入默认为字符串
 2 #可在此行写上 age = int(age) ,在此后的if中即可把int(age)换为age
 3 if int(age) < 2 :
 4     print(age+"\tHe's a baby") #当age为非字符串类型是应使用str()转换。不同类型无法相加
 5 elif int(age) < 12 and int(age) >= 2 :
 6     print(age + "\tHe's a child")
 7 elif int(age) < 18 and int(age) >= 12 :
 8     print(age + "\tHe's a Juvenile") 
 9 elif int(age) < 60 and int(age) >= 18 :
10     print(age + "\tHe's a Youth")
11 else :
12     print(age + "\tHe's a old age")

判断列表是否为空

 1 equested_topings = []   
 2 if requested_topings ://空列表等于 False
 3 """
 4 还可使用len与is None判断列表是否为空
 5 if requested_topings is None :
 6 if len(requested_topings) == 0 :
 7 if len(requested_topings) :
 8 """
 9     for requested_toping in requested_topings:
10         print("Adding" + requested_toping + ".")
11 else :
12     print("Are you sure want a plain pizza?")

待续...如有不正还请斧正!

猜你喜欢

转载自www.cnblogs.com/MR---Zhao/p/12319493.html