Python入门与实践 Chapter 4 动手试一试
2018-03-17 15:20
537 查看
4-3 数到20 : 使用一个for 循环打印数字1~20(含) 。
4-5 计算1~1 000 000的总和 : 创建一个列表, 其中包含数字1~1 000 000, 再使用min() 和max() 核实该列表确实是从1开始, 到1 000 000结束的。 另外, 对这个列表
调用函数sum() , 看看Python将一百万个数字相加需要多长时间。
4-6 奇数 : 通过给函数range() 指定第三个参数来创建一个列表, 其中包含1~20的奇数; 再使用一个for 循环将这些数字都打印出来。
4-9 立方解析 : 使用列表解析生成一个列表, 其中包含前10个整数的立方。
4-10 切片 : 选择你在本章编写的一个程序, 在末尾添加几行代码, 以完成如下任务。
打印消息“The first three items in the list are:”, 再使用切片来打印列表的前三个元素。
打印消息“Three items fromthe middle of the list are:”, 再使用切片来打印列表中间的三个元素。
打印消息“The last three items in the list are:”, 再使用切片来打印列表末尾的三个元素。
4-13 自助餐 : 有一家自助式餐馆, 只提供五种简单的食品。 请想出五种简单的食品, 并将其存储在一个元组中。
使用一个for 循环将该餐馆提供的五种食品都打印出来。
尝试修改其中的一个元素, 核实Python确实会拒绝你这样做。
4000
pre>
4-5 计算1~1 000 000的总和 : 创建一个列表, 其中包含数字1~1 000 000, 再使用min() 和max() 核实该列表确实是从1开始, 到1 000 000结束的。 另外, 对这个列表
调用函数sum() , 看看Python将一百万个数字相加需要多长时间。
4-6 奇数 : 通过给函数range() 指定第三个参数来创建一个列表, 其中包含1~20的奇数; 再使用一个for 循环将这些数字都打印出来。
4-9 立方解析 : 使用列表解析生成一个列表, 其中包含前10个整数的立方。
4-10 切片 : 选择你在本章编写的一个程序, 在末尾添加几行代码, 以完成如下任务。
打印消息“The first three items in the list are:”, 再使用切片来打印列表的前三个元素。
打印消息“Three items fromthe middle of the list are:”, 再使用切片来打印列表中间的三个元素。
打印消息“The last three items in the list are:”, 再使用切片来打印列表末尾的三个元素。
4-13 自助餐 : 有一家自助式餐馆, 只提供五种简单的食品。 请想出五种简单的食品, 并将其存储在一个元组中。
使用一个for 循环将该餐馆提供的五种食品都打印出来。
尝试修改其中的一个元素, 核实Python确实会拒绝你这样做。
餐馆调整了菜单, 替换了它提供的其中两种食品。 请编写一个这样的代码块: 给元组变量赋值, 并使用一个for 循环将新元组的每个元素都打印出来。
实现上述题目的代码如下:
# 4-3 for i in range(20): print(i + 1) # 4-5 import time billion = range(1, 1000001) print(max(billion)) print(min(billion)) start = time.clock() print(sum(billion)) elapsed = (time.clock() - start) print("It takes lovely Python " + str(elapsed) + " seconds to deal with the sum.") # 4-9 cube = [pow(i, 3) for i in range(1, 10)] print(cube) # 4-10 names = ['Mencius', 'Irish gay', 'Lesbian group', 'Chiang Kai-Shek', 'Stephen Hawking'] print("The first three items in the list are " + str(names[:3])) print("Three items from the middle of the list are " + str(names[1:4])) print("The last three items in the list are " + str(names[-3:])) # 4-13 foods = ('Cheddar Cheese Stuffed Burgers', ' French Fries', "The Burger Lover's Burger", "Blueberry Buttermilk Pancakes", " Blueberry Crumble Pie") print("The ordinary foods :" + str(foods)) for item in foods: print(item) # foods[1]='Strawberry Pop Tarts' Python refuse foods = ('Cheddar Cheese Stuffed Burgers', ' French Fries', "The Burger Lover's Burger", "Strawberry Pop Tarts", "Bacon Corn Muffins") print("Now the new dishes:") for item in foods: print(item)
输出如下:
<4000
pre>
F:\Python_Code\Homework\venv\Scripts\python.exe F:/Python_Code/Homework/hw3.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1000000 1 500000500000 It takes lovely Python 0.16019139973301696 seconds to deal with the sum. [1, 8, 27, 64, 125, 216, 343, 512, 729] The first three items in the list are ['Mencius', 'Irish gay', 'Lesbian group'] Three items from the middle of the list are ['Irish gay', 'Lesbian group', 'Chiang Kai-Shek'] The last three items in the list are ['Lesbian group', 'Chiang Kai-Shek', 'Stephen Hawking'] The ordinary foods :('Cheddar Cheese Stuffed Burgers', ' French Fries', "The Burger Lover's Burger", 'Blueberry Buttermilk Pancakes', ' Blueberry Crumble Pie') Cheddar Cheese Stuffed Burgers French Fries The Burger Lover's Burger Blueberry Buttermilk Pancakes Blueberry Crumble Pie Now the new dishes: Cheddar Cheese Stuffed Burgers French Fries The Burger Lover's Burger Strawberry Pop Tarts Bacon Corn Muffins Process finished with exit code 0
相关文章推荐
- Python入门与实践 Chapter 6 动手试一试
- Python入门与实践 Chapter 8 动手试一试
- Python入门与实践 Chapter 5 动手试一试
- Python入门与实践 Chapter 7 动手试一试
- Python学习 第二天任务 (一:【基于Python编程从入门到实践】第二章 变量和字符串 书本及动手试一试)
- Python学习 第一天任务 (四:【基于Python编程从入门到实践】动手试一试)
- Python学习 第二天任务 (二:【基于Python编程从入门到实践】第二章 数字和注释 书本及动手试一试)
- Python编程:从入门到实践的动手试一试答案(第九章)
- Python编程:从入门到实践的动手试一试答案(第三章)
- Python编程:从入门到实践的动手试一试答案(第十章)
- Python编程:从入门到实践的动手试一试答案(第七章)
- Python编程:从入门到实践的动手试一试答案(第十一章)
- Python编程:从入门到实践的动手试一试答案(第六章)
- Python编程:从入门到实践的动手试一试答案(第七章)
- 第四天任务 (【基于Python编程从入门到实践】第四章 操作列表后两节 书本及动手试一试)
- Python编程:从入门到实践的动手试一试答案(第四章)
- Python编程:从入门到实践的动手试一试答案(第十章)
- 第五天任务 (【基于Python编程从入门到实践】第五章 if语句 书本及动手试一试)
- Python编程:从入门到实践的动手试一试答案(第八章)
- Python编程:从入门到实践的动手试一试答案(第四章)