Python:从入门到实践--第四章--列表操作--练习
#1.想出至少三种你喜欢的水果,将其名称存储在一个列表中,再使用for循环将每种水果的名称都打印出来。
#要求:(1)修改这个for循环,使其打印包含名称的句子,而不是仅仅是水果的名称。对于每种水果,都显示一行输出。例如:I like apple
#(2)在程序末尾添加一行代码,它不在for循环中,指出你有多喜欢水果,输出应包含针对每种水果的消息,还有一个总结性性句子,如I really love fruits! fruits = ['apple','banana','orange','watermelon']
for fruit in fruits:
print('I like '+ fruit + '\n') print('I really love fruits!' + '\n') #2.想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,在使用for循环将每种动物的名称都打印出来
#要求:(1)修改这个程序,使其针对每种动物打印一个句子,如"A dog would make a great pet"
#(2)在程序的末尾添加一行代码,指出这些动物的共同之处。 animals = ['lion','tiger','leopard']
for animal in animals:
print(animal + " like eating mealt." + '\n') print("Any of these animals are fierce!")
#1.数到20:使用一个for循环打印数字1-20(含)
for number in range(1,21):
print(number ,end = ' ') print('\n')
#2.一百万:创建一个列表,其中包含数字1-1000,再使用一个for循环将这些数字打印出来;再使用min()和max()核实该列表确实是从1开始,到1000结束
#另外,对于这个列表调用sum()函数,看看python将一百万数字相加徐亚多长时间
#numbers = list(range(1,1000001))
#for number in numbers:
# print(number,end = ' ') #print('\n')
#print(min(numbers))
#print(max(numbers))
#print(sum(numbers)) #3.奇数:通过给定函数range()指定第三个参数来创建一个列表,其中包含1-20的奇数;再使用一个for循环将这些数字打印出
odds_number = list(range(1,21,2))
for odd in odds_number:
print(odd,end=' ') print('\n')
#4的倍数:创建一个列表,其中包含3-30内能被3整除的数字;再使用一个for循环将这个列表中的数字都打印出来。
numbers = range(3,31,3)
for number in numbers:
print(number,end=' ') print('\n')
#5.立方:使用列表解析生成一个列表,其中包含前10个整数的立方,在使用一个for循环将这些数字打印出来
numbers = [values**3 for values in range(1,11)]
for cube in numbers:
print(cube,end = " ") #1.切片:选择在本章编写的程序,在末尾添加几行代码,完成如下要求
#打印消息“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 :”,再使用切片来打印列表末尾的三个元素 foods = ['rice','noodle','apple','laoganma','cake','bread','ice cream']
print("The first three items in the list are: ")
print(foods[0:3]) print("Three items from the middle of the list are:")
print(foods[2:5]) print("The last three items in the list are:")
print(foods[-3:]) print('\n')
#2.你的水果和我的水果:创建水果列表副本,并将其存储到变量friend_fruits中,完成如下要求
#在原来的水果列表中添加另一种水果
#在friend_fruits列表中添加另一种水果
#核实你有两个不同的列表
#使用两个for循环,将各个食品列表都打印出来 My_fruits = ['apple','banana','watermelon']
Friend_fruits = My_fruits[:] My_fruits.append('orange')
Friend_fruits.append('strawberry') print("My favorite fruits are:")
print(My_fruits) print("My friend's favorite fruits are:")
print(Friend_fruits) for my in My_fruits:
print(my) for friend in Friend_fruits:
print(friend)
#食物:将任意五种食物存储在一个元组
#尝试修改其中的一个元素,核实python确实拒绝你这样做
#替换其中的两个食物,并用for循环将他们打印出来 foods = ('rice','meat','apple','orange juice','milk')
print(foods) #foods[1]='water' foods = ('rice','meat','apple','ice cream','water')
print(foods)
Python:从入门到实践--第四章--列表操作--练习的更多相关文章
- Python:从入门到实践--第三章--列表简介--练习
#1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...
- #Python编程从入门到实践#第四章笔记
#Python编程从入门到实践#第四章笔记 操作列表 1.遍历列表 使用for循环,遍历values列表 for value in values: print(value) 2.数字列表 使 ...
- python编程:从入门到实践----第四章>操作列表
一.遍历整个列表 1-1.假设有一个魔术师名单,需要将其中每个魔术师的名字都打印出来. # 用for循环来打印魔术师名单中的名字 magicians=['alice','david','carolin ...
- py从入门到实践 第四章
4.1 遍立列表 ~= shell 数组————————————————————————————————————————————thrink = ['link','path','pwd']for i ...
- Python:从入门到实践--第六章--字典--练习
#1.人:使用一个字典来存储一个熟人的信息;包括姓,名,年龄和居住的城市.将字典中的每项信息都打印出来 friend = { 'last_name':'马', 'first_name':'脑壳', ' ...
- python 从入门到实践 第三章
在第3章,你将学习如何在被称为列表的变量中存储信息集,以及如何通过遍历列表来操作其中的信息 写注释 # 代码越长 标识好代码的重要性 越来越重要要求习惯:在代码中编写清晰,简洁的注释开始研究更复杂的主 ...
- Python:从入门到实践--第十一章--测试代码--练习
#1.城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名. #这个函数返回一个格式为City,Country的字符串,如Santiago,Chile.将这个函数 #存储在一个名为city ...
- Python:从入门到实践--第五章--if语句--练习
#1.编写一系列条件测试:将每个测试以及结果打印出来 car = '宝马' if car == "宝马": print("预测正确") print(car) e ...
- Python:从入门到实践--第七章--用户输入和while循环-练习
#1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...
随机推荐
- 基于观察者模式-----otto源码分析
一.otto简介 otto是支付公司square一个专注于android的开源项目,该项目是一个event bus模式的消息框架,是一个基于Guava的增强型事件总线.旨在将应用程序的不同部分分离,同 ...
- NullPointerException空指针异常——没有事先加载布局文件到acitivy——缺少:setContentView(R.layout.activity_setup_over);
空指针异常: 04-27 01:13:57.270: E/AndroidRuntime(4942): FATAL EXCEPTION: main04-27 01:13:57.270: E/Androi ...
- 得到本地电脑IP4地址
using System.Linq;using System.Net;using System.Net.Sockets; namespace winform_udp{ public class com ...
- day47-python爬虫学习二
2.Request的会话对象 s = requests.session() Python2 S = requests.Session() 所有一次会话的信息都保存在s中,只需要对s进行操作就可以了. ...
- centos6.5删除/boot后恢复
删除/boot下的所有东西 更改为从光盘启动并进入紧急救援模式 语言选择英语 选择美式键盘布局 不配置网络 选择Continue 这里提示系统已经被挂载到了/mnt/sysimage 选择shell, ...
- C# [Win32] [API] Layered Windows
static void* WndProc(void* hwnd, uint uMsg, void* wParam, void* lParam) { switch (uMsg) { case WM_PA ...
- hdu 1754 I Hate It 解题报告(线段树 代码+注释)
题目链接:传送门 I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- dva,清除模块数据
前言: 在项目中,模块过多,dva使用namespace分离模块后,若没有在模块卸载后清除对应的数据,下次进入时,有可能会有上一次数据的残留. 比如详情页,从A商品的详情页离开后,返回选择B商品进入, ...
- P5016 龙虎斗 题解
这题真是*到家了QAQ 我在考场上调了将近75min,总算过了大样例. 首先,我们可以简化这一题,这道题的本质就是让我们找出一个点p2,往那个点上面加上s2个单位的重量,使得以m为中的两边的权值和的差 ...
- 4 扩展库Scipy
https://www.scipy.org/ 1. numpy 矩阵 2. matplotlib 绘图库 3. pandas 高效的Series和DataFrame数据结构 4.5 ndarry ...