Python编程从入门到实践(4)学习笔记-if语句,字典
第五章 if语句
第六章 字典
1 # 第五章 if语句 2 3 # if语句 ==号是精确匹配 4 cars = ['bmw', 'audi', 'toyota', 'subaru'] 5 for car in cars: 6 if car == 'bmw': 7 print(car.upper()) 8 else: 9 print(car.title()) 10 11 # 如果想无视大小写匹配可以先转小写再比 12 car.lower() == 'audi' 13 14 # 不等于 != 15 requested_topping = 'mushrooms' 16 if requested_topping != 'anchovies': 17 # 抓住鳀(咸水小鱼); 18 print("Hold the anchovies") 19 20 # 等于 大于 小于 21 age = 19 22 age < 21 23 age <= 21 24 age > 21 25 age >= 21 26 27 # and or 28 age_0 = 22 29 age_1 = 18 30 age_0 >= 21 and age_1 >= 21 31 age_0 >= 21 or age_1 >= 21 32 33 # in 34 requested_topping = ['mushrooms', 'onions', 'pineapple'] 35 # 精确匹配 36 if 'mushroomS' in requested_topping: 37 print("True") 38 else: 39 print("False") 40 # not in 41 if 'zz' not in requested_topping: 42 print("zz is not in list ") 43 44 # 布尔表达式 45 game_active = True 46 can_edit = False 47 48 # 多路if elif else 49 age = 12 50 51 if age < 4: 52 price = 0 53 elif age < 18: 54 price = 5 55 elif age < 65: 56 price = 10 57 else: 58 price = 5 59 60 print(price) 61 62 # 列表不是空的检查 63 # 会走else 判断 64 requested_topping = [] 65 # 至少含有1个元素才 66 if requested_topping: 67 print("not empty") 68 else: 69 print("yes is empty") 70 71 # 第六章 字典 72 alien_0 = {'color': 'green', 'point': 5} 73 print(alien_0['color']) 74 print(alien_0['point']) 75 print(alien_0) 76 77 # 加了2对键值对 78 alien_0['x_position'] = 0 79 alien_0['y_position'] = 25 80 print(alien_0) 81 82 # 空字典 83 alien_0 = {} 84 alien_0['color'] = 'green' 85 alien_0['points'] = 5 86 print(alien_0) 87 88 # 修改键值对 89 alien_0['color'] = 'red' 90 print(alien_0) 91 92 # 删除键值对 93 del alien_0['color'] 94 print(alien_0) 95 96 favorite_languages = { 97 'jen': 'python', 98 'sarah': 'c', 99 'edward': 'ruby', 100 'phil': 'python', 101 } 102 # 拼接可以换行 103 print("sarah's favorite language is " + 104 favorite_languages['sarah'].title() + 105 ".") 106 107 # 遍历字典 108 for name, language in favorite_languages.items(): 109 print(name.title() + "'s favorite language is " + 110 language.title() + ".") 111 112 # 提取key 113 for name in favorite_languages.keys(): 114 print(name.title()) 115 116 # 排序 favorite_languages.keys的 .keys可以省略 117 for name in sorted(favorite_languages): 118 print(name.title()) 119 120 # 提取values 121 for language in favorite_languages.values(): 122 print(language.title()) 123 124 # 提取values 去重复,进一个set集合 125 print("去除重复") 126 for language in set(favorite_languages.values()): 127 print(language.title()) 128 129 # 字典列表 P93 130 aliens = [] 131 for alien_number in range(30): 132 new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'} 133 aliens.append(new_alien) 134 135 for alien in aliens[:5]: 136 print(alien) 137 print("Totle number of aliens :" + str(len(aliens))) 138 139 # 字典中存列表 140 pizza = { 141 'crust': 'thick', 142 'toppings': ['mushrooms', 'extra cheese'] 143 } 144 145 # 字典中value是多值 146 favorite_languages = { 147 'jen': ['python', 'ruby'], 148 'sarah': ['c'], 149 'edward': ['ruby', 'go'], 150 'phil': ['python', 'haskell'], 151 } 152 153 for k, v in favorite_languages.items(): 154 print(name.title() + "'s favorite language are:") 155 for language in v: 156 print("\t" + language.title()) 157 158 #在字典中存字典 159 users = { 160 'aeinstein':{ 161 'first':'albert', 162 'last':'einstein', 163 'location':'princeton', 164 }, 165 'mucurie':{ 166 'first':'marie', 167 'last':'curie', 168 'location':'paris', 169 }, 170 }