您的位置:首页 > 编程语言 > Python开发

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确实会拒绝你这样做。

餐馆调整了菜单, 替换了它提供的其中两种食品。 请编写一个这样的代码块: 给元组变量赋值, 并使用一个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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐