概述
#4-3 数到20:
代码:
#4-3 数到20
for num in range(1, 21):
print(num)
输出:pass
#4-5 计算1~1 000 000的总和:
代码:
#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
1000000
500000500000
It costs 0.1980368107604632s
#4-6 奇数:
代码:
#4-6 奇数
nums = list(range(1, 20, 2))
for num in nums:
print(num)
输出:pass
#4-7 3的倍数:
代码:
#4-7 3的倍数
nums = list(i*3 for i in range(1, 11))
for num in nums:
print(num)
输出:pass
#4-8 立方:
代码:
#4-8 立方
nums = list(i**3 for i in range(1, 11))
for num in nums:
print(num)
输出:pass
#4-9 立方解析:
代码:
#4-9 立方解析
nums = list(i**3 for i in range(1, 11))
print(num)
输出:
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
#4-10 切片:
#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, 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编程》第四章部分课后练习题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复