#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. Git bash 配置多个远端仓库

    $ cat .ssh/config #aliyeye Host aliyeye.com.cn HostName aliyeye.com.cn PreferredAuthentications publ ...

  2. hdu 1754 I Hate It 解题报告(线段树 代码+注释)

    题目链接:传送门 I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. Windows Server 2008 R2 服务器系统安装及配置全过程图文详解

    前言 本文主要介绍了 windows Server 2008 R2 服务器系统的安装及相关配置. 介绍的是以优盘的方式安装. 写这篇博文的目的一来是为了供有需要的网友参考, 二来自己也在此做个记载. ...

  4. Windows挂载NFS共享盘

    Centos7添加NFS方法请见如下链接: https://www.cnblogs.com/jackyzm/p/10285845.html 一:添加NFS服务 1.1:此电脑-右键-管理-window ...

  5. 阿里规范学习总结-不要再foreach对元素进行add()/remove()操作,

    在foreach循环中,对元素进行 remove()/add() 操作需要使用Iterator ,如果运行在多线程环境下,需要对Iterator对象枷锁. public class ForeachTe ...

  6. css flex布局,小程序flex布局,垂直居中完美解决

    flex弹性布局,很好的解决了垂直居中的问题,上代码: wxml: <view class='container'> <view class='item item1'>item ...

  7. 团队作业4——beta冲刺

    beta冲刺准备 冲刺准备 5天冲刺博客 Beta冲刺一 Beta冲刺二 Beta冲刺三 Beta冲刺四 Beta冲刺五 用户使用报告 用户使用用报告 冲刺总结 冲刺总结随笔 项目码云地址 码云地址

  8. QT之两种模态对话框的调用

    模态对话框:就是没有关闭它之前,不能再与同一个应用程序的其他窗口进行交互. 1.show调用 LoginDialog *dlg = new LoginDialog(); dlg->setModa ...

  9. my first homepage

    <!DOCTYPE html><html><head><style type="text/css">p{ text-indent:2 ...

  10. RabbitMQ,为应对消息从发送到消费,各个环节消息丢失的解决方案

      1.发送方   为保证消息到达exchange,在这个过程中不丢失.  用事务或者发送方确认机制  见<RabbitMQ实战指南>4.8节 2.为保证消息不会因为到达exchange后 ...