我是靠谱客的博主 狂野毛衣,这篇文章主要介绍《Python编程》第四章部分课后练习题,现在分享给大家,希望可以做个参考。

#4-3 数到20:

代码:

复制代码
1
2
3
#4-3 数到20 for num in range(1, 21): print(num)

输出:pass


#4-5 计算1~1 000 000的总和:

代码:

复制代码
1
2
3
4
5
6
7
8
9
10
#4-5 计算1~1 000 000的总和 import time nums = list(range(1, 1000001)) print(min(nums)) print(max(nums)) time1 = time.clock() print(sum(nums)) time2 = time.clock() message = 'It costs ' + str(time2 - time1) + 's' print (message)

输出:

复制代码
1
2
3
4
1 1000000 500000500000 It costs 0.1980368107604632s


#4-6 奇数:

代码:

复制代码
1
2
3
4
#4-6 奇数 nums = list(range(1, 20, 2)) for num in nums: print(num)

输出:pass


#4-7 3的倍数:

代码:

复制代码
1
2
3
4
#4-7 3的倍数 nums = list(i*3 for i in range(1, 11)) for num in nums: print(num)

输出:pass


#4-8 立方:

代码:

复制代码
1
2
3
4
#4-8 立方 nums = list(i**3 for i in range(1, 11)) for num in nums: print(num)

输出:pass


#4-9 立方解析:

代码:

复制代码
1
2
3
#4-9 立方解析 nums = list(i**3 for i in range(1, 11)) print(num)

输出:

复制代码
1
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]


#4-10 切片:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
#4-10 切片 nums = list(i**3 for i in range(1, 11)) print(nums) message = 'nThe first three items in the list are:' print(message) print(nums[:3]) message = 'nThree items from the middle of the list are:' print(message) print(nums[3:6]) message = 'nThe last three items in the list are:' print(message) print(nums[-3:])

输出:

复制代码
1
2
3
4
5
6
7
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] The first three items in the list are: [1, 8, 27] Three items from the middle of the list are: [64, 125, 216] The last three items in the list are: [512, 729, 1000]

最后

以上就是狂野毛衣最近收集整理的关于《Python编程》第四章部分课后练习题的全部内容,更多相关《Python编程》第四章部分课后练习题内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部