【Hello Python World】Week 2(1):列表简介

2024-08-30 08:58

本文主要是介绍【Hello Python World】Week 2(1):列表简介,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

      • 1 姓名
      • 3-2 问候语
      • 3-3 自己的列表
      • 3-4 嘉宾名单
      • 3-5 修改嘉宾名单
      • 3-6 添加嘉宾你刚找到了
      • 3-7 缩减名单
      • 3-8 放眼世界
      • 3-9 晚餐嘉宾
      • 3-10 尝试使用各个函数
      • 3-11 有意引发错误

第三章主要是介绍Python中的list,比较简单

3.1 姓名

将一些朋友的姓名存储在一个列表中,并将其命名为names 。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。

>>> friends = ['jj','pp','jp']
>>> print(friends[0])
jj
>>> print(friends[1])
pp
>>> print(friends[2])
jp

3-2 问候语

继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。

>>> friends = ['jj','pp','jp']
>>> print(friends[0] + ', nice to meet you!')
jj, nice to meet you!
>>> print(friends[1] + ', nice to meet you!')
pp, nice to meet you!
>>> print(friends[2] + ', nice to meet you!')
jp, nice to meet you!

3-3 自己的列表

想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。

>>> transports = ['ofo','mobike']
>>> for thing in transports:
...  print('I would like to ride ' + thing + " to teaching buildings.")
...
I would like to ride ofo to teaching buildings.
I would like to ride mobike to teaching buildings.

3-4 嘉宾名单

如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用 这个列表打印消息,邀请这些人来与你共进晚餐。

>>> invite = ['bob','amy','wuyifan']
>>> for people in invite:
...     print('Hey ' + people + '! Can you come and have dinner with me tonight? ')
...
Hey bob! Can you come and have dinner with me tonight?
Hey amy! Can you come and have dinner with me tonight?
Hey wuyifan! Can you come and have dinner with me tonight?

3-5 修改嘉宾名单

你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。

以完成练习3-4时编写的程序为基础,在程序末尾添加一条print语句,指出哪位嘉宾无法赴约。

修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。

再次打印一系列消息,向名单中的每位嘉宾发出邀请。

>>> invite = ['bob','amy','wuyifan']
>>> for people in invite:
...     print('Hey ' + people + '! Can you come and have dinner with me tonight? ')
...
Hey bob! Can you come and have dinner with me tonight?
Hey amy! Can you come and have dinner with me tonight?
Hey wuyifan! Can you come and have dinner with me tonight?
>>> print('Oh I am sooooo sad to know that my darling '+ invite[1] + " can't have dinner with me tonight...")
Oh I am sooooo sad to know that my darling amy can't have dinner with me tonight...

3-6 添加嘉宾你刚找到了

一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。

以完成练习3-4或练习3-5时编写的程序为基础,在程序末尾添加一条print语句,指出你找到了一个更大的餐桌。

使用insert()将一位新嘉宾添加到名单开头。

使用insert()将另一位新嘉宾添加到名单中间。

使用append()将最后一位新嘉宾添加到名单末尾。

打印一系列消息,向名单中的每位嘉宾发出邀请。

>>> invite = ['bob','amy','wuyifan']
>>> print("Ha ha, I have bought a 100m*100m golden table in Taobao last night!")
Ha ha, I have bought a 100m*100m golden table in Taobao last night!
>>> invite.insert('mike',0)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> invite.insert(0,'mike')
>>> invite.insert(2,'jake')
>>> invite.append('rose')
>>> for people in invite:
...     print("Hello " + people + ', can you have dinner with me next Friday?')
...
Hello mike, can you have dinner with me next Friday?
Hello bob, can you have dinner with me next Friday?
Hello jake, can you have dinner with me next Friday?
Hello amy, can you have dinner with me next Friday?
Hello wuyifan, can you have dinner with me next Friday?
Hello rose, can you have dinner with me next Friday?

3-7 缩减名单

你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。

以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。

使用pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。

对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。

使用del将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。

>>> invite = ['mike','bob','amy','wuyifan','rose']
>>>> print("Dame! I can't have my new table in time! I can just invite two friends now.")
Dame! I can't have my new table in time! I can just invite two friends now.
>>> print('Sorry ' + invite.pop() + ", I can't invite you for some bad things")
Sorry rose, I can't invite you for some bad things
>>> print('Sorry ' + invite.pop() + ", I can't invite you for some bad things")
Sorry wuyifan, I can't invite you for some bad things
>>> print('Sorry ' + invite.pop() + ", I can't invite you for some bad things")
Sorry amy, I can't invite you for some bad things
>>> print(invite[0] + ", let's have dinner tonight!")
mike, let's have dinner tonight!
>>> print(invite[1] + ", let's have dinner tonight!")
bob, let's have dinner tonight!
>>> del invite[0]
>>> del invite[0]
>>> print(invite)
[]

3-8 放眼世界

想出至少5个你渴望去旅游的地方。

将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。

按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表。

使用sorted()按字母顺序打印这个列表,同时不要修改它。

再次打印该列表,核实排列顺序未变。 使用sorted()按与字母顺序相反的顺序打印这个列表,同时不要修改它。

使用reverse()修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。

使用reverse()再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。

使用sort()修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。

>>> places = ['Beijing','Shanghai','Sanya','Hangzhou','Shenzhen']
>>> print(places)
['Beijing', 'Shanghai', 'Sanya', 'Hangzhou', 'Shenzhen']
>>> print(sorted(places))
['Beijing', 'Hangzhou', 'Sanya', 'Shanghai', 'Shenzhen']
>>> print(places)
['Beijing', 'Shanghai', 'Sanya', 'Hangzhou', 'Shenzhen']
>>> places.reverse()
>>> print(places)
['Shenzhen', 'Hangzhou', 'Sanya', 'Shanghai', 'Beijing']
>>> places.reverse()
>>> print(places)
['Beijing', 'Shanghai', 'Sanya', 'Hangzhou', 'Shenzhen']
>>> places.sort()
>>> places.reverse()
>>> print(places)
['Shenzhen', 'Shanghai', 'Sanya', 'Hangzhou', 'Beijing']

3-9 晚餐嘉宾

在完成练习3-4~练习3-7时编写的程序之一中,使用len()打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐。

>>> invite = ['mike','bob','amy','wuyifan','rose']
>>> print(len(invite))
5

3-10 尝试使用各个函数

想想可存储到列表中的东西,如山岳、河流、国家、城市、语言或你喜欢的任何东西。编写一个程序,在其中创建一个包含这些元素的列表,然后,对于本章介绍的每个函数,都至少使用一次来处理这个列表。

>>> numbers = [1,9,5,3,7,8,6,2,4,0]
>>> print(len(numbers))
10
>>> del numbers[0]
>>> numbers.insert(0,1)
>>> print(numbers)
[1, 9, 5, 3, 7, 8, 6, 2, 4, 0]
>>> numbers.pop()
0
>>> numbers.append(0)
>>> print(numbers)
[1, 9, 5, 3, 7, 8, 6, 2, 4, 0]
>>> print(sorted(numbers))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(numbers)
[1, 9, 5, 3, 7, 8, 6, 2, 4, 0]
>>> numbers.reverse()
>>> print(numbers)
[0, 4, 2, 6, 8, 7, 3, 5, 9, 1]
>>> numbers.sort()
>>> print(numbers)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

3-11 有意引发错误

如果你还没有在程序中遇到过索引错误,就尝试引发一个这种错误。在你的一个程序中,修改其中的索引,以引发索引错误。关闭程序前,务必消除这个错误。

>>> numbers = [0,2]
>>> print(numbers[2])
Traceback (most recent call last):File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> print(numbers[-3])
Traceback (most recent call last):File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> print(numbers[-2])
0

这篇关于【Hello Python World】Week 2(1):列表简介的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1120371

相关文章

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

Python打包成exe常用的四种方法小结

《Python打包成exe常用的四种方法小结》本文主要介绍了Python打包成exe常用的四种方法,包括PyInstaller、cx_Freeze、Py2exe、Nuitka,文中通过示例代码介绍的非... 目录一.PyInstaller11.安装:2. PyInstaller常用参数下面是pyinstal

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数