【HW】4-1到4-13的选题,代码
2018-03-18 16:12
218 查看
4-2 动物 :想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for 循环将每种动物的名称都打印出来。
修改这个程序,使其针对每种动物都打印一个句子,如“A dog would make a great pet”。
在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any of these animals would make a great pet!”这样的句子。
4-8 立方 :将同一个数字乘三次称为立方。例如,在Python中,2的立方用2**3 表示。请创建一个列表,其中包含前10个整数(即1~10)的立方,再使用一个for 循环将这些立方数都打印出来。
4-9 立方解析 :使用列表解析生成一个列表,其中包含前10个整数的立方。
4-10 切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个元素。
打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素。
打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三个元素。
修改这个程序,使其针对每种动物都打印一个句子,如“A dog would make a great pet”。
在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any of these animals would make a great pet!”这样的句子。
animals = ['rabbit','cat','dog'] for animal in animals : print('A ' + animal + ' is a great pet.') print('Any of these animals would make a great pet!')
4-8 立方 :将同一个数字乘三次称为立方。例如,在Python中,2的立方用2**3 表示。请创建一个列表,其中包含前10个整数(即1~10)的立方,再使用一个for 循环将这些立方数都打印出来。
4-9 立方解析 :使用列表解析生成一个列表,其中包含前10个整数的立方。
#4-8 numbers = [] for i in range(1,10): numbers.append(i**3) print(numbers[-1]) #4-9 numbers2 = [i**3 for i in range(1,10)] print(numbers2)
4-10 切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个元素。
打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素。
打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三个元素。
# use the 4-9's numbers numbers = [i**3 for i in range(1,10)] print(numbers) print("The first three items in the list are:") print(numbers[:3]) print('Three items from the middle of the list are:') print(numbers[3:6]) print('The last three items in the list are:') print(numbers[-3:])
相关文章推荐
- 【HW】书本6-1至6-13选题和代码
- 【HW】5-1至5-11的选题和代码和输出
- 【HW-2】书本2-1至2-11中的选题,代码及输出
- 【HW-3】书本3-1至3-11的选题,代码和输出
- 【HW】书本8-1至8-17选题,代码和输出
- 【HW】书本7-1至7-10选题,代码和输入输出
- 【HW】书本9-11至9-16选题代码和输出
- JAVA功能代码《13》----在Java中调整数组大小
- 《高效编程十八式》(9/13)代码编辑
- 注释代码的13技巧
- 13个人遇3自杀(约瑟夫算法)代码实现
- 《驯服烂代码——代码内在质量的改善之道》写作大纲之一:选题思路和读者对象
- JAVA功能代码《13》----在Java中调整数组大小
- GCC-3.4.6源代码学习笔记(13)
- Canvas引用图像【每日一段代码13】
- 一起看代码来玩玩QT之13 IO(two TCPServer TcpClient TcpScoket)
- HTML+CSS学习笔记 (13) - CSS代码缩写,占用更少的带宽
- 微信公共服务平台开发(.Net 的实现)13-------网页授权(下 :C#代码的实现 )
- 一周最新示例代码回顾 (5/7–5/13)