1. and or 可以直接写,就是字面意思
2.检查特定值是否包含在列表中( in 语句中别忘了:)
requested_toppings = ['mushrooms', 'onions', 'pineapple']
if 'mushrooms' in requested_toppings:
else:
检查特定值是否不包含在列表中 (not in)
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(user.title() + ", you can post a response if you wish.")
3.if-elif-else 结构(类似于C)
age = 12
if age < 4:
print("Your admission cost is $0.")
elif age < 18:
print("Your admission cost is $5.")
else:
print("Your admission cost is $10.")
4.遍历字典的键值对(.items)
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in user_0.items():
print("\nKey: " + key)
print("Value: " + value)
结果:Key: last
Value: fermi
Key: first
Value: enrico
Key: username
Value: efermi
这种方法输出的键值对顺序是混乱的,如果要按照字母顺序输出键值对需要这样写:
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in sorted(user_0.items()): # 按照字母顺序排列
print("\nKey: " + key)
print("Value: " + value)
结果:Key: first
Value: enrico
Key: last
Value: fermi
Key: username
Value: efermi
5.遍历字典中所有的键(keys()方法)
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
for name in favorite_languages.keys():
print(name.title())
遍历字典时,会默认遍历所有的键,因此,如果将上述代码中的 for name in favorite_languages.keys(): 替换为 for name in favorite_languages: ,输出将不变。
6.遍历字典中所有的值(values()方法)
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
print("The following languages have been mentioned:")
for language in favorite_languages.values():
print(language.title())
结果:The following languages have been mentioned:
Python
C
Python
Ruby
通过对包含重复元素的列表调用 set()剔除重复项 ,可让Python找出列表中独一无二的元素,并使用这些元素来创建一个集合。
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
print("The following languages have been mentioned:")
for language in set(favorite_languages.values()):
print(language.title())
结果:The following languages have been mentioned:
Python
C
Ruby
7.列表里嵌套字典
aliens = [] # 创建外星人空列表
for alien_number in range(0, 30):
new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
aliens.append(new_alien)
for alien in aliens[0:3]:
if alien['color'] == 'green':
alien['color'] = 'yellow'
alien['speed'] = 'medium'
alien['points'] = 10
elif alien['color'] == 'yellow':
alien['color'] = 'red'
alien['speed'] = 'fast'
alien['points'] = 15
for alien in aliens[0:30]:
print(alien)
8.字典里嵌套列表
favorite_languages = {
'jen': ['python', 'ruby'],
'sarah': ['c'],
'edward': ['ruby', 'go'],
'phil': ['python', 'haskell'],
}
for name, languages in favorite_languages.items():
print("\n" + name.title() + "'s favorite languages are:")
for language in languages: # 在每个键再遍历它的两个值
print("\t" + language.title())
9.字典中嵌套字典
users = {
'aeinstein': {
'first': 'albert',
'last': 'einstein',
'location': 'princeton',
},
'mcurie': {
'first': 'marie',
'last': 'curie',
'location': 'paris',
},
}
for username, user_info in users.items():
print("\nUsername: " + username)
full_name = user_info['first'] + " " + user_info['last']
location = user_info['location']
print("\tFull name: " + full_name.title())
print("\tLocation: " + location.title())
10.修改字典
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8 # 更新
dict['School'] = "RUNOOB" # 添加
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
结果:
dict['Age']: 8 dict['School']: RUNOOB