4-1 比萨 :想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来。
a.修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对于每种比萨,都显示一行输出,如“I like pepperoni pizza”。
b.在程序末尾添加一行代码,它不在for 循环中,指出你有多喜欢比萨。输出应包含针对每种比萨的消息,还有一个总结性句子,如“I really love pizza!”。
pizza = ["Zunbao","Bishengke","Bipizza"]
for i in pizza:
print(i)
print("I like pepperoni pizza\n")
print("I really love pizza")

  

4-2 动物:想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for 循环将每种动物的名称都打印出来。
a.修改这个程序,使其针对每种动物都打印一个句子,如“Adogwould makea great pet”。
b.在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any oftheseanimals would makea great pet!”这样的句子。
animals = ["tiger","rabbit","panda"]
for i in animals:
print(i)
print("A dog would make a great pet\n")
print("Any of these animals would make a great pet!")

  

4-3 数到20 :使用一个for 循环打印数字1~20(含)。
for i in range(1,20+1):
print(i)
 
4-4 一百万 :创建一个列表,其中包含数字1~1 000 000,再使用一个for 循环将这些数字打印出来(如果输出的时间太长,按Ctrl+ C停止输出,或关闭输出窗口)。
millions = list(range(1,1000000))     #用list把数字转换成列表格式
for i in millions:
print(i)
 
4-5 计算1~1 000 000的总和:创建一个列表,其中包含数字1~1 000 000,再使用min() 和max() 核实该列表确实是从1开始,到1 000 000结束的。另外,对这个列表调用函数sum() ,看看Python将一百万个数字相加需要多长时间。
millions = list(range(1,1000000))
print(min(millions))
print(max(millions))
print(sum(millions))

  

4-6 奇数 :通过给函数range() 指定第三个参数来创建一个列表,其中包含1~20的奇数;再使用一个for 循环将这些数字都打印出来。
numbers= list(range(1,20,2))   #通过range的第三个参数,也就是步长为2,来获取奇数
for i in numbers:
print(i)
 
4-7 3的倍数 :创建一个列表,其中包含3~30内能被3整除的数字;再使用一个for 循环将这个列表中的数字都打印出来。
numbers= list(range(3,30,3))
for i in numbers:
print(i)
 
4-8 立方 :将同一个数字乘三次称为立方。例如,在Python中,2的立方用2**3 表示。请创建一个列表,其中包含前10个整数(即1~10)的立方,再使用一个for 循环将这些立方数都打印出来。
squares=[]
for value in range(1,10+1):
square = value**3
squares.append(square)
print(squares)
 
4-9 立方解析 :使用列表解析生成一个列表,其中包含前10个整数的立方。
square=[value**3 for value in range(1,10+1)]
print(square)

4-10 切片:选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。

a. 打印消息“The first three items in the lis tare:”,再使用切片来打印列表的前三个元素。

b. 打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素

c. 打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三个元素

my_foods = ['pizza', 'falafel', 'carrot cake','fruit','coffee','rice','meat']
print("The first three items in the list are:")
print(my_foods[:3])
print("\nThree items from the middle of the list are:")
print(my_foods[2:5])
print("\nThe last three items in the list are:")
print(my_foods[4:]) #输出结果:
The first three items in the list are:
['pizza', 'falafel', 'carrot cake'] Three items from the middle of the list are:
['carrot cake', 'fruit', 'coffee'] The last three items in the list are:
['coffee', 'rice', 'meat']
4-11 你的比萨和我的比萨 :在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其存储到变量friend_pizzas 中,再完成如下任务。
a. 在原来的比萨列表中添加一种比萨。
b. 在列表friend_pizzas 中添加另一种比萨。
c. 核实你有两个不同的列表。为此,打印消息“My favorite pizzasare:”,再使用一个for 循环来打印第一个列表;打印消息“My friend's favorite pizzasare:”,再使用一个for 循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。
pizza = ["Zunbao","Bishengke","Bipizza"]
friend_pizzas = pizza[:]
pizza.append("Niu")
friend_pizzas.append("Liang") print("My favorite pizzasare:")
for i in pizza:
print(i) print("\nMy friend's favorite pizzasare:")
for j in friend_pizzas:
print(j)
#输出结果:
My favorite pizzasare:
Zunbao
Bishengke
Bipizza
Niu My friend's favorite pizzasare:
Zunbao
Bishengke
Bipizza
Liang
 
4-12 使用多个循环 :在本节中,为节省篇幅,程序foods.py的每个版本都没有使用for 循环来打印列表。请选择一个版本的foods.py,在其中编写两个for 循环,将各个食品列表都打印出来。

my_foods=['pizza','falafel','carrot','cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
for my_food in my_foods:
print(my_food)
print("\nMy friend's favorite foods are:")
for friend_food in friend_foods:
print(friend_food) #输出结果:
My favorite foods are:
pizza
falafel
carrot
cake
cannoli My friend's favorite foods are:
pizza
falafel
carrot
cake
ice cream

  

4-13 自助餐:有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。
a. 使用一个for 循环将该餐馆提供的五种食品都打印出来。
b. 尝试修改其中的一个元素,核实Python确实会拒绝你这样做。
c. 餐馆调整了菜单,替换了它提供的其中两种食品。请编写一个这样的代码块:给元组变量赋值,并使用一个for 循环将新元组的每个元素都打印出来。
foods =('rice','meat','seafood','vegetable','fruit')
print("The original foods are:")
for food in foods:
print(food) #foods[0]='coffee'
#print(foods[0]) foods=('coffee','meat','seafood','vegetable','tea')
print("\nThe modify foods are:")
for food in foods:
print(food) #输出结果:
The original foods are:
rice
meat
seafood
vegetable
fruit The modify foods are:
coffee
meat
seafood
vegetable
tea

  

python编程:从入门到实践----基础知识>第4章练习的更多相关文章

  1. 《Python编程从入门到实践》_第六章_字典

    一个简单的字典 #用户信息 user = {','city':'shanghai'} print(user['name']) print(user['age']) print(user['city'] ...

  2. 《Python编程从入门到实践》_第五章_if语句

    条件测试 每条if语句的核心都是一个值为Ture或False的表达式,这种表达式被称为为条件测试.Python根据条件测试的值为Ture还是False来决定是否执行if语句中的代码.如果条件测试的值为 ...

  3. 《Python编程从入门到实践》_第四章_操作列表

    for循环遍历整个列表 pizzas = ['pizzahut','dicos','KFC'] for pizza in pizzas: print ("I like "+ piz ...

  4. 《Python编程从入门到实践》_第七章_用户输入和whlie循环

    函数input()的工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,python将其存储在一个变量中,以方便你使用. #输入用户名 username = input( ...

  5. 《Python编程从入门到实践》_第三章_列表简介

    什么是列表呢? 官方说明就是由一些列按特点顺序排列的元素组成.其实可以看出很多个字符串的有序组合吧,里面的内容可以随时的删除,增加,修改. 下面这个就是一个列表,python打印列表的时候会将中括号和 ...

  6. 《python编程从入门到实践》2.3字符串

    书籍<python编程从入门到实践> 2.3字符串 知识模块 print()函数,函数名称突出为蓝色,输出括号内的变量或者字符创. 变量名的命名:尽量小写字母加下划线并且具有良好的描述性, ...

  7. 《python编程从入门到实践》读书实践笔记(一)

    本文是<python编程从入门到实践>读书实践笔记1~10章的内容,主要包含安装.基础类型.函数.类.文件读写及异常的内容. 1 起步 1.1 搭建环境 1.1.1 Python 版本选择 ...

  8. Python编程从入门到实践笔记——异常和存储数据

    Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...

  9. Python编程从入门到实践笔记——文件

    Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...

随机推荐

  1. 第二阶段scrum-8

    1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 消息收发制作中

  2. ACM-Divide Tree

    题目描述:Divide Tree   As we all know that we can consider a tree as a graph. Now give you a tree with n ...

  3. Redis Sentinel 学习笔记

    转载出处: http://blog.csdn.net/lihao21 概述 Redis Sentinel 是用来实现 Redis 高可用的一套解决方案.Redis Sentinel 由两个部分组成:由 ...

  4. hdu 3790 最短路径dijkstra(多重权值)

    最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. P 1023 组个最小数

    转跳点:

  6. Day3-T4

    原题目 Describe:有点恶心的DP+最短路 code: #include<bits/stdc++.h> using namespace std; long long A,B,C,z, ...

  7. java课程之团队开发冲刺阶段2.9

    总结昨天进度: 已经完成查询课程信息任务 遇到的困难: 已经全部解决 今天的任务: 修改APP图标 当日总结: manifest中管理着APP的基本信息资料,所以是在manifest文件中修改APP的 ...

  8. windows elasticsearch-head插件安装教程

    elasticsearch-head下载地址:https://github.com/mobz/elasticsearch-head 1.git下载 git clone git://github.com ...

  9. spring 官方文档-片段学习——webflux-ann-controller

    spring 官方文档-片段学习总结 片段所在连接:https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-referenc ...

  10. tomcat的8080,8009,8443,8005都是什么端口

    <Server port="8005" shutdown="SHUTDOWN"> 远程停服务端口<Connector port="8 ...