Python编程从入门到实践练习第三章:列表简介
目录
- 一、字符串
- 1.1 在字符串中使用变量
- 二、列表
- 2.1 遍历列表
- 练习题
- 代码
- 2.2 列表元素的插入和删除
- 涉及方法
- 练习题
- 代码
- 2.3 组织列表
- 涉及方法
- 练习题
- 代码
- 2.4 索引
参考书:Python从入门到实践(第二版)
一、字符串
1.1 在字符串中使用变量
f “{ 变量名 }”
f代表format,Python把花括号里的变量名替换为其值来设置字符串的格式。(Python3.6引入)
代码示例:
first_name = input("Your first name is: ")
last_name = input("Your last name is : ")
full_name = f"{first_name} {last_name}"
print(f"My name is {full_name.title()}")
二、列表
2.1 遍历列表
练习题
将一些朋友的姓名存储在一个列表中,并将其命名为names 。依次访问该列表中的每个元素,从而将每个朋友的姓名打印出来。
代码
names = ['winnie','jack','lili','will','diana']
for name in names:print(f'Hello, my name is {name.title()}')
else:print("The list is overlooped")
代码解释:
- 使用for循环遍历列表中的所有信息
- 使用format字符串形式输出字符串中的变量
- 使用title方法对列表中的字母进行首字母大写
2.2 列表元素的插入和删除
涉及方法
- append() : 在列表末尾添加元素
- insert() : 在列表任意位置添加元素
- remove() :根据具体值删除元素
- del 语句 :删除整个列别或某一元素
- pop() :弹出任意元素
练习题
代码
#3-4
names = ['diane','kalinda','alicia','will','peter','cary']
for name in names:print(f"Dear {name.title()}, let's have a dinner together this evening.")
num_guests = len(names)
print(f'We have invited {num_guests} guests now',)
print("\n")#3-5
print("Ops! Peter is busy with his campaign so he can't joy the dinner.\n")
names.remove('peter')
names.append('zerk')
for name in names:print(f"Dear {name.title()}, let's have a dinner together this evening.")
num_guests = len(names)
print(f'We have invited {num_guests} guests now',)
print("\n")#3-6
print("We can invite more guests now because I have found a bigger dinner table now.\n")
names.insert(0,'tom')
names.insert(int(len(names)/2),'mary')
names.append('winnie')
for name in names:print(f"Dear {name.title()}, let's have a dinner together this evening.")
num_guests = len(names)
print(f'We have invited {num_guests} guests now',)
print("\n")#3-7
print('Sorry, I can only invite 2 guests to joy the dinner.\n')
while len(names)>2:name = names.pop()print(f"Dear {name.title()}, sorry for the change, we can have dinner the next time!")
for name in names:print(f"Dear {name.title()}, you are still invited to the dinner tonight.")
del names[0]
del names[0]
print(names)
输出如下:
Dear Diane, let’s have a dinner together this evening.
Dear Kalinda, let’s have a dinner together this evening.
Dear Alicia, let’s have a dinner together this evening.
Dear Will, let’s have a dinner together this evening.
Dear Peter, let’s have a dinner together this evening.
Dear Cary, let’s have a dinner together this evening.
We have invited 6 guests now
Ops! Peter is busy with his campaign so he can’t joy the dinner.
Dear Diane, let’s have a dinner together this evening.
Dear Kalinda, let’s have a dinner together this evening.
Dear Alicia, let’s have a dinner together this evening.
Dear Will, let’s have a dinner together this evening.
Dear Cary, let’s have a dinner together this evening.
Dear Zerk, let’s have a dinner together this evening.
We have invited 6 guests now
We can invite more guests now because I have found a bigger dinner table now.
Dear Tom, let’s have a dinner together this evening.
Dear Diane, let’s have a dinner together this evening.
Dear Kalinda, let’s have a dinner together this evening.
Dear Mary, let’s have a dinner together this evening.
Dear Alicia, let’s have a dinner together this evening.
Dear Will, let’s have a dinner together this evening.
Dear Cary, let’s have a dinner together this evening.
Dear Zerk, let’s have a dinner together this evening.
Dear Winnie, let’s have a dinner together this evening.
We have invited 9 guests now
Sorry, I can only invite 2 guests to joy the dinner.
Dear Winnie, sorry for the change, we can have dinner the next time!
Dear Zerk, sorry for the change, we can have dinner the next time!
Dear Cary, sorry for the change, we can have dinner the next time!
Dear Will, sorry for the change, we can have dinner the next time!
Dear Alicia, sorry for the change, we can have dinner the next time!
Dear Mary, sorry for the change, we can have dinner the next time!
Dear Kalinda, sorry for the change, we can have dinner the next time!
Dear Tom, you are still invited to the dinner tonight.
Dear Diane, you are still invited to the dinner tonight.
2.3 组织列表
涉及方法
- sort():对列表排序
- sorted():对列表暂时排序,不改变原有顺序
- reverse():翻转列表
练习题
代码
#3-8
travel = ['italy','france','america','spain','denmark']
print("original:",travel)
print("sorted:",sorted(travel))
print("after sorted:",travel)
travel.reverse()
print("reverse:",travel)
travel.reverse()
print("reverse again:",travel)
travel.sort()
print("sort:",travel)
travel.sort(reverse=True)
print("sort reverse=True:",travel)#3-9
names = ['diane','kalinda','alicia','will','peter','cary']
print("The number of guests are %d." %len(names))
2.4 索引
列表从前往后的下标从0开始,从后往前的下标从-1开始
相关文章:

Python编程从入门到实践练习第三章:列表简介
目录 一、字符串1.1 在字符串中使用变量 二、列表2.1 遍历列表练习题代码 2.2 列表元素的插入和删除涉及方法练习题代码 2.3 组织列表涉及方法练习题代码 2.4 索引 参考书:Python从入门到实践(第二版) 一、字符串 1.1 在字符串中使用变量 f…...

【Spring Boot】请求参数传json数组,后端采用(pojo)新增案例(103)
请求参数传json数组,后端采用(pojo)接收的前提条件: 1.pom.xml文件加入坐标依赖:jackson-databind 2.Spring Boot 的启动类加注解:EnableWebMvc 3.Spring Boot 的Controller接受参数采用:Reque…...
Redis 持久化RDB和AOF
Redis 持久化之RDB和AOF Redis 有两种持久化方案,RDB (Redis DataBase)和 AOF (Append Only File)。如果你想快速了解和使用RDB和AOF,可以直接跳到文章底部看总结。本章节通过配置文件,触发快照…...
【ThinkPHP】PHP实现分页功能
查询列表数据,需要用到分页功能,下面是分页功能的代码: /*** Summary of userList* return \think\response\Json*/public function userList(){$page input("page")?:1;//当前页数$size input("size")?:10;//每页大…...
chrome 插件开发
参考: https://www.cnblogs.com/amboke/p/16718855.html 设计和实现一个 Chrome 插件提升登录效率_若川的技术博客_51CTO博客 最新版 V3 chrome 插件开发~ demo 坑 - 掘金 官方文档:https://developer.chrome.com/docs/extensions/...
开源MinDoc wiki系统搭建
部署文档参考 https://cloud.tencent.com/developer/beta/article/2134667 https://mp.weixin.qq.com/s?__bizMzU0MzEyODAyNA&mid2247485475&idx1&snac5ac76beac0a1405ca7a0f045f44db3&chksmfb116894cc66e182b197601420b8b5409a91ac538ba67d01248659de913fe7…...
pytest.ini 文件说明
pytest.ini 文件是用于配置 pytest 测试用例运行规则的文件。pytest.ini 配置文件支持的参数有以下几类: 匹配测试文件和测试函数的过滤参数测试用例执行参数测试报告输出参数临时文件及路径参数插件参数 以下是一些常见的 pytest.ini 配置参数及其用法示例&#…...

遥感、GIS、GPS在土壤空间数据分析、适应性评价、制图、土壤普查中怎样应用?
摸清我国当前土壤质量与完善土壤类型,可以为守住耕地红线、保护生态环境、优化农业生产布局、推进农业高质量发展奠定坚实基础,为此,2022年初国务院印发了《关于开展第三次全国土壤普查的通知》,决定自2022年起开展第三次全国土壤…...

git | git使用心得记录
公司里项目最近使用Git进行协作开发,总结一下使用心得 一、第一次用git,完全同步最新代码checkout 按照以下步骤操作 1、git init 2、git remote add origin 远程仓库的地址https://gitlab.xxxx.com.cn/xx/xx/xxx/Android/baseline/x.x.x.git(远程仓库…...
java策略模式三种实现方案
方案1:Autowired Map public interface ClientService {void hanlde(Object obj);String type(); }Service public class PcClientService implements ClientService { Overridepublic void handle(Object obj) {// todo pc客户端逻辑} Overridepublic Stri…...

VMWare虚拟系统上网设置及VMWare虚拟机三种工作模式详解
很多网友安装了VMWare虚拟机,但是在虚拟机上网问题上却卡住了。要想虚拟机上网,首先让我们了解一下VMWare虚拟机三种工作模式。现在,让我们一起走近VMWare的三种工作模式。 理解三种工作模式 …...

计算机网络(3) --- 网络套接字TCP
计算机网络(2) --- 网络套接字UDP_哈里沃克的博客-CSDN博客https://blog.csdn.net/m0_63488627/article/details/131977544?spm1001.2014.3001.5501 目录 1.TCP 1.服务端接口介绍 1.listen状态 2.accept获取链接 2.客户端接口介绍 2.TCP的服务器…...

大数据技术之Hadoop(二)
目录 一、Hadoop的诞生 二、大数据概述 三、大数据软件生态 3.1 数据存储相关技术 3.2 数据计算相关技术 3.3 数据传输相关技术 四、什么是Hadoop 本篇主要讲解大数据的核心概念以及Hadoop的基本介绍。 一、Hadoop的诞生 大数据的发展与日益庞大的数据量是密不可分的。从…...
运维工程师第二阶段linux基础
文章目录 linux基础1.linux发展史--前身unix2.linux特点3.linux操作系统安装4.linux基础操作5.shell与终端6.Linux基础命令使用管理员找回密码linux的目录和管理1.根目录下目录结构及作用文件类型2.基本的目录管理命令查看基本的文件管理命令重定向① 几个概念② Linux打包操作…...

ChatGPT安全技术
前言 近期,Twitter 博主 lauriewired 声称他发现了一种新的 ChatGPT"越狱"技术,可以绕过 OpenAI 的审查过滤系统,让 ChatGPT 干坏事,如生成勒索软件、键盘记录器等恶意软件。 他利用了人脑的一种"Typoglycemia&q…...

使用cmd查看3568主板相关
主要是说清楚思路的 rk3568主板能运行的程序都在system/bin里面,这个是我们直接可以使用cmd用到的 所以,往后我们想通过cmd了解RK3568的某一项参数的时候,或者想使用RK3568某一个系统功能的时候。应该先去system/bin里面查找对应的系统程序。…...
SpringBoot限制(限流)接口访问频率
限流整个流程过程 1.首先用户的请求进来,将用户ip和uri组成key,timestamp为value,放入zset 2. 更新当前key的缓存过期时间,这一步主要是为了定期清理掉冷数据,和上面我提到的常见错误设计2中的意义不同 3. 删除窗口之…...

蓝桥杯,我劝你不要参加的8个完美理由
蓝桥杯,是一个全国高校的IT技术比拼,如果你参加了,可能不止是刷题数量的剧增,还有你的软件人生 我劝你不要参加,因为如果你参加了,可能会有以下烦恼: 目录 1、会让你变得上进 2、会提前感受码…...

ChatGPT及其工作原理;OpenAI申请注册商标GPT-5,引发关注
🦉 AI新闻 🚀 OpenAI申请注册商标GPT-5,引发关注 摘要:OpenAI已在上月18日申请注册商标GPT-5,显示该模型将提供文本生成、自然语言理解、语音转录、翻译、分析等功能。此前OpenAI曾表示尚未开始训练GPT-4的后继者GPT…...

[C++项目] Boost文档 站内搜索引擎(2): 文档文本解析模块parser的实现、如何对文档文件去标签、如何获取文档标题...
项目开始的准备工作 在上一篇文章中, 已经从Boost官网获取了Boost库的源码. 接下来就要编写代码了. 不过还需要做一些准备工作. 创建项目目录 所有的项目文件肯定要在一个目录下, 找一个位置执行下面这行指令 mkdir Boost-Doc-Searcher将文档html文件, 存放到项目中 cd Boost…...
五年级数学知识边界总结思考-下册
目录 一、背景二、过程1.观察物体小学五年级下册“观察物体”知识点详解:由来、作用与意义**一、知识点核心内容****二、知识点的由来:从生活实践到数学抽象****三、知识的作用:解决实际问题的工具****四、学习的意义:培养核心素养…...
鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院查看报告小程序
一、开发环境准备 工具安装: 下载安装DevEco Studio 4.0(支持HarmonyOS 5)配置HarmonyOS SDK 5.0确保Node.js版本≥14 项目初始化: ohpm init harmony/hospital-report-app 二、核心功能模块实现 1. 报告列表…...

CocosCreator 之 JavaScript/TypeScript和Java的相互交互
引擎版本: 3.8.1 语言: JavaScript/TypeScript、C、Java 环境:Window 参考:Java原生反射机制 您好,我是鹤九日! 回顾 在上篇文章中:CocosCreator Android项目接入UnityAds 广告SDK。 我们简单讲…...
【决胜公务员考试】求职OMG——见面课测验1
2025最新版!!!6.8截至答题,大家注意呀! 博主码字不易点个关注吧,祝期末顺利~~ 1.单选题(2分) 下列说法错误的是:( B ) A.选调生属于公务员系统 B.公务员属于事业编 C.选调生有基层锻炼的要求 D…...
GitHub 趋势日报 (2025年06月08日)
📊 由 TrendForge 系统生成 | 🌐 https://trendforge.devlive.org/ 🌐 本日报中的项目描述已自动翻译为中文 📈 今日获星趋势图 今日获星趋势图 884 cognee 566 dify 414 HumanSystemOptimization 414 omni-tools 321 note-gen …...

IT供电系统绝缘监测及故障定位解决方案
随着新能源的快速发展,光伏电站、储能系统及充电设备已广泛应用于现代能源网络。在光伏领域,IT供电系统凭借其持续供电性好、安全性高等优势成为光伏首选,但在长期运行中,例如老化、潮湿、隐裂、机械损伤等问题会影响光伏板绝缘层…...
Android第十三次面试总结(四大 组件基础)
Activity生命周期和四大启动模式详解 一、Activity 生命周期 Activity 的生命周期由一系列回调方法组成,用于管理其创建、可见性、焦点和销毁过程。以下是核心方法及其调用时机: onCreate() 调用时机:Activity 首次创建时调用。…...
高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数
高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数 在软件开发中,单例模式(Singleton Pattern)是一种常见的设计模式,确保一个类仅有一个实例,并提供一个全局访问点。在多线程环境下,实现单例模式时需要注意线程安全问题,以防止多个线程同时创建实例,导致…...

【Linux】Linux 系统默认的目录及作用说明
博主介绍:✌全网粉丝23W,CSDN博客专家、Java领域优质创作者,掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域✌ 技术范围:SpringBoot、SpringCloud、Vue、SSM、HTML、Nodejs、Python、MySQL、PostgreSQL、大数据、物…...

C++ 设计模式 《小明的奶茶加料风波》
👨🎓 模式名称:装饰器模式(Decorator Pattern) 👦 小明最近上线了校园奶茶配送功能,业务火爆,大家都在加料: 有的同学要加波霸 🟤,有的要加椰果…...