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+" He's a baby") #当age为非字符串类型是应使用str()转换。不同类型无法相加 5 elif int(age) < 12 and int(age) >= 2 : 6 print(age + " He's a child") 7 elif int(age) < 18 and int(age) >= 12 : 8 print(age + " He's a Juvenile") 9 elif int(age) < 60 and int(age) >= 18 : 10 print(age + " He's a Youth") 11 else : 12 print(age + " He's a old age")
判断列表是否为空
1 requested_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?")
-------------------------------------------------分割线---------------------------------------------------分割线-----------------------------------------------------------------------------------------------------------------------------------------------------------------
字典
访问字典中的值
1 alien_0 = {'color': 'geeen','points' : 5}#字典,键值对应 2 print(alien_0['color'])#访问字典中键的对应值
3 ace = alien_0['points']
4 print(ace)
添加键-值对
//Python不关心键值对的添加顺序,只关心键和值之间的关联关系
1 alien_0['x_position'] = 0#为字典添加键值对 2 alien_0['x_position'] =25 #修改字典中的值 3 print(alien_0)#打印键值对 4 for da in alien_0 :#利用for打印字典,会根据字典中键值对的数量来确定打印的次数 5 print(alien_0)
删除键-值对
del alien_0['color']
遍历字典中的键-值对
items() 遍历键值对 //把字典中每对 key 和 value 组成一个元组,并把这些元组放在列表中返回
1 dictionaries = { 2 'int' : '整数类型', 3 'str' : '字符串类型', 4 'for' : '循环', 5 'if' : '判断', 6 'pop' : '弹出', 7 } 8 for key,value in dictionaries.items() : 9 print('key: ' + key + ' value: ' + value)
按固定顺序排列遍历字典
sorted() 方法
1 for key, values in sorted(dictionaries.items()) : 2 print('key: ' + key + 'values: ' + values)
遍历字典中的所有键
1 for key in dictionaries.keys() :#使用keys方法 2 print('key: ' + key) 3 for key in dictionaries :#不是使用keys,遍历字典时会默认遍历所有键 4 print('key: ' + key)
遍历字典中的所有值
values() 它返回一个值列表
1 for value in dictionaries.values():
2 print('value: ' + value)
判断某键是否在某字典内
1 if 'int' not in dictionaries :#此处省略.keys() 2 print('Erin,plase take our poll!')
集合(set)
集合同列表相似,但每个元素都必须是独一无二的
可用来剔除“重复项”
1 for language in set(dictionaries.values()) :#剔除重复值 2 print(language) 3 for language in set(dictionaries) :#剔除重复键 4 print(language) 5 for language,value in set(dictionaries.items()) :#剔除重复的键值对 6 print(language + value)
在字典中这3种的效果,都是去除重复的键值对
嵌套
列表字典
1 alien_0 = {'color': 'green', 'points':5,} 2 alien_1 = {'color': 'red', 'points':10,} 3 alien_2 = {'color': 'red', 'points':15} 4 aliens = [alien_0,alien_1,alien_2,]#字典列表 5 for alien in aliens : 6 print(aliens) 7 aliens = (alien_0,alien_1,alien_2,)#字典元组 8 for alien in aliens : 9 print(aliens)
1 #批量操作 2 aliens = []#创建空列表 3 for alien_number in range(0,1000) :#rang(2,1000)即for循环1000次 4 new_alien = {'color': 'red','points': 100,'speed': 'slow'} 5 aliens.append(new_alien)#创建1000个字典并加入到列表中 6 for alien in aliens[0:10] :#修改前10个字典的speed键对应的值 7 if alien['speed'] == 'slow' : 8 alien['speed'] ='High' 9 for alien in aliens[0:5] : 10 print(alien) 11 print('...') 12 for alien in aliens[9:11] :#打印第10与12这俩字典 13 print(alien) 14
字典列表
需要在键里添加多个值时,可采取在字典内嵌入列表
1 favoritr_languages = { 2 'jen': ['python','ruby'], 3 'sarah': ['c'], 4 'esward': ['go', 'ruby'], 5 'phil': ['python','haskell'], 6 } 7 for name, laguages in favoritr_languages.items() : 10 print(name.title()) 11 for laguage in laguages : 12 #因为键的对应值变为了一个列表,所以仍需一个for循环来遍历列表中的元素 13 print(laguage.title())
字典字典
例如:储存网站用户的信息
1 cities = { 2 'Gushi': {'country' : 'china', 'fact': 'backward', 'population': '180w' }, 3 'san francisco': {'country': 'u.s.a', 'fact': 'top', 'population': '88w'}, 4 'edo': {'country': 'japan', 'fact': 'top', 'population': '3719w'}, 5 }#写法上与字典中嵌入列表不同的是:内部的[]变为{}。不要忘记键值对末尾的,号哦 6 for citie, information in cities.items() :#遍历键值对 7 print(citie.title())#进一步处理,打印 8 a = information['country'] 9 b = information['fact']+" "+information['population'] 10 print(a.title()) 11 print(b.title())
待续...如有不正还请斧正!