赞
踩
- # 4.1披萨:
- citys = ['Shanghai','Beijing','Wuhu','Shenzhen','HongKong']
- for city in citys:
- print("I like " + city + ".\n")
- print("I really love city.")
-
- # 4.2动物:
- animals = ['cat', 'dog', 'panda', 'monkey']
- for animal in animals:
- print(animal)
- print(f"A {animal} would make a great pet.")
- print("\nAny of these animals would make a great a great pet!")
-
- # 4-3 数到20
- for value in range(1,21):
- print(value)
-
- # 4-4 一百万
- one_millions =list(range(1,1000001))
- print(f"打印1~1000000的列表:{one_millions}")
-
- # 4-5 计算1~1000000的总和
- print(min(one_millions))
- print(max(one_millions))
- print(sum(one_millions))
-
- # 4-6 奇数:用range()创建一个列表,包含1~20的奇数;再用for循环打印出来
- odd_numbers = list(range(1,20,2))
- print(f"1~20之间的奇数:{odd_numbers}")
-
- for value in odd_numbers: # 再用for循环打印出来
- print(value)
-
- # 4-7 3的倍数:创建一个列表,包含3~30内能被3整除的数字;再用for循环打印出来
- three_numbers = list(range(3,31,3))
- print(f"3~30之间的奇数:{three_numbers}")
- for value in three_numbers:
- print(value)
-
- # 4-8 立方:打印1~10的立方
- cubes =[]
- for value in range(1,11):
- cube = value*value*value
- cubes.append(cube)
- print(cubes)
-
- for num in cubes:
- print(num)
-
- # 或者:不使用中间变量cube
- cubes = []
- for value in range(1,11):
- cubes.append(value*value*value)
- print(cubes)
-
- # 4-9 立方解析
- cubes = [value*value*value for value in range(1,11) ]
- print(f"1~10的立方:{cubes}")
- for value in cubes:
- print(value)
-
- # 4-10 切片
- my_list = [1,2,3,4,5,6,7,8,9]
- print(f"前三个元素:{my_list[:3]}")
- print(f"中间三个元素:{my_list[3:6]}")
- print(f"末尾三个元素:{my_list[-3:]}")
-
- # 4-11 你的披萨和我的披萨
- my_magicians = ['alice','david','carolina']
- friend_magicians = my_magicians[:] # 相当于副本
- my_magicians.append('bob')
- friend_magicians.append('liming')
- print(f"My_magicians are {my_magicians}")
- for my_magician in friend_magicians:
- print(my_magician)
- print("\n")
- print(f"Friend_magicians are {friend_magicians}")
- for friend_magician in friend_magicians:
- print(friend_magician)
-
- # 4-12 使用多个循环:
- my_foods = ['pizza', 'falafel', 'carrot cake']
- friend_foods = my_foods[:]
- for my_food in my_foods:
- print(my_food)
-
- # 4-13-自助餐
- foods = ('rich','peanut','dumpling','potato','cake') # 创建一个食物元组
- for food in foods:
- print(food)
- # foods[0] = ice cream #尝试修改元组,但出现报错,说明元组不可修改
- foods = ('rich','beef','dumpling','bacon','noodle') # 重新定义变量,这是可以的
- print("\n")
- for food in foods:
- print(food)
-
- # 4-14 PEP8:
-
- # 4-15 代码审核:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。