#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:从入门到实践--第四章--列表操作--练习的更多相关文章

  1. Python:从入门到实践--第三章--列表简介--练习

    #1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...

  2. #Python编程从入门到实践#第四章笔记

    #Python编程从入门到实践#第四章笔记   操作列表 ​​​1.遍历列表 使用for循环,遍历values列表 for value in values: print(value) 2.数字列表 使 ...

  3. python编程:从入门到实践----第四章>操作列表

    一.遍历整个列表 1-1.假设有一个魔术师名单,需要将其中每个魔术师的名字都打印出来. # 用for循环来打印魔术师名单中的名字 magicians=['alice','david','carolin ...

  4. py从入门到实践 第四章

    4.1 遍立列表 ~= shell 数组————————————————————————————————————————————thrink = ['link','path','pwd']for i ...

  5. Python:从入门到实践--第六章--字典--练习

    #1.人:使用一个字典来存储一个熟人的信息;包括姓,名,年龄和居住的城市.将字典中的每项信息都打印出来 friend = { 'last_name':'马', 'first_name':'脑壳', ' ...

  6. python 从入门到实践 第三章

    在第3章,你将学习如何在被称为列表的变量中存储信息集,以及如何通过遍历列表来操作其中的信息 写注释 # 代码越长 标识好代码的重要性 越来越重要要求习惯:在代码中编写清晰,简洁的注释开始研究更复杂的主 ...

  7. Python:从入门到实践--第十一章--测试代码--练习

    #1.城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名. #这个函数返回一个格式为City,Country的字符串,如Santiago,Chile.将这个函数 #存储在一个名为city ...

  8. Python:从入门到实践--第五章--if语句--练习

    #1.编写一系列条件测试:将每个测试以及结果打印出来 car = '宝马' if car == "宝马": print("预测正确") print(car) e ...

  9. Python:从入门到实践--第七章--用户输入和while循环-练习

    #1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...

随机推荐

  1. 前端页面的适配使用rem换算

    前端页面的适配使用rem换算 https://www.cnblogs.com/liangxuru/p/6970629.html 注:本文转载之处:https://www.cnblogs.com/ann ...

  2. C# json转model 以及model转json

    1.json转model TestModel tm = new TestModel(); JavaScriptSerializer js = new JavaScriptSerializer();tm ...

  3. 在kali linux上安装VMware tool

    在安全圈的门口徘徊了一年,一直不知道该如何入门,现在决定先从kali 入手.有同样兴趣的伙伴欢迎一起. 但是刚在VMware上安好系统就遇到了一个大麻烦,看了很多书,还有教程但总是遇到这样那样的问题. ...

  4. golang开源项目qor快速搭建网站qor-example运行实践

    最近想找几个基于Go语言开发的简单的开源项目学习下,分享给大家,github上有心人的收集的awesome-go项目集锦:github地址 发现一个Qor项目: Qor 是基于 Golang 开发的的 ...

  5. HTML、CSS(小笔记)

    这是我自己在学习html.css时觉得要记的东西太多总结一些较为常用的标签. HTML <p></p>段落标签 <hn></hn>标题标签n数值为1~6 ...

  6. ImportError libcublas.so.9.0

    What to do when you've installed cuda and tensorflow, but you get this error right after you import ...

  7. 如何理解JavaScript中的原型和原型链

    首先是一张关系图,避免抽象化理解时产生的困难 Function对象 函数对象是JavaScript学习中不可避免的一部分,而且这一部分相对重要且抽象 函数的创建方式有2种: 字面量创建 var foo ...

  8. cocoapods 换源

    1. 用以下步骤换源: pod repo remove master pod repo add master https://code.aliyun.com/Magi/CocoaPods.git po ...

  9. R语言最优化(一维)

    最优化问题是普遍存在的,以前上运筹学课的时候也接触过最优化相关的问题,当时主要是理论课,并且关注的重点是单纯形法.运输问题以及图论等,这里指的最优化是指函数的最优化,即函数的极值,由于寻找一个局部最优 ...

  10. EFCore Lazy Loading + Inheritance = 干净的数据表 (一) 【献给处女座的DB First程序猿】

    前言 α角 与 β角 关于α角 与 β角的介绍,请见上文 如何用EFCore Lazy Loading实现Entity Split. 本篇会继续有关于β角的彩蛋在等着大家去发掘./斜眼笑 其他 本篇的 ...