当前位置: 首页 > news >正文

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")

代码解释:

  1. 使用for循环遍历列表中的所有信息
  2. 使用format字符串形式输出字符串中的变量
  3. 使用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开始

http://www.lryc.cn/news/106971.html

相关文章:

  • 【Spring Boot】请求参数传json数组,后端采用(pojo)新增案例(103)
  • Redis 持久化RDB和AOF
  • 【ThinkPHP】PHP实现分页功能
  • chrome 插件开发
  • 开源MinDoc wiki系统搭建
  • pytest.ini 文件说明
  • 遥感、GIS、GPS在土壤空间数据分析、适应性评价、制图、土壤普查中怎样应用?
  • git | git使用心得记录
  • java策略模式三种实现方案
  • VMWare虚拟系统上网设置及VMWare虚拟机三种工作模式详解
  • 计算机网络(3) --- 网络套接字TCP
  • 大数据技术之Hadoop(二)
  • 运维工程师第二阶段linux基础
  • ChatGPT安全技术
  • 使用cmd查看3568主板相关
  • SpringBoot限制(限流)接口访问频率
  • 蓝桥杯,我劝你不要参加的8个完美理由
  • ChatGPT及其工作原理;OpenAI申请注册商标GPT-5,引发关注
  • [C++项目] Boost文档 站内搜索引擎(2): 文档文本解析模块parser的实现、如何对文档文件去标签、如何获取文档标题...
  • 若依框架vue使用Element 如何把当前页面的所有Table表格row.id和一个表单的16个字段内容通过js传Java后台,Java后台是如何接收的
  • 迁移学习:使用Restnet预训练模型构建高效的水果识别模型
  • 浅谈机器视觉
  • 助力保险行业数字化创新,麒麟信安参展2023中国财险科技应用高峰论坛
  • eclipse was unable to locate its companion shared library
  • 【MySQL】使用C/C++连接MySQL数据库
  • 【Python】从同步到异步多核:测试桩性能优化,加速应用的开发和验证
  • 使用checkBox组件时,动态设置disabled,仍能触发click事件的原因及解决办法
  • 【JavaScript】如何进行除法运算且保留小数部分不参与四舍五入【推荐库bignumber.js 】
  • 掌握Java JDK 1.8 API帮助文档中文版,事半功倍编程
  • Spring Boot的自动配置原理