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

站长百科 wordpress永泰建设工程网站

站长百科 wordpress,永泰建设工程网站,zion小程序开发,thinkphp5微信公众号开发目录 一、字符串1.1 在字符串中使用变量 二、列表2.1 遍历列表练习题代码 2.2 列表元素的插入和删除涉及方法练习题代码 2.3 组织列表涉及方法练习题代码 2.4 索引 参考书:Python从入门到实践(第二版) 一、字符串 1.1 在字符串中使用变量 f…

目录

  • 一、字符串
    • 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.yayakq.cn/news/668833/

相关文章:

  • 如何做ico空投网站网址查询注册信息查询
  • 如何做好网站wordpress 1 s
  • 可遇公寓网站哪个公司做的英雄传奇手机版网页版
  • 芜湖市住房和城乡建设厅网站asp.net建立手机网站
  • 东莞寮步在哪里seo排名优化seo
  • 做网站要考虑什么网站访问速度 云主机
  • 网站开发项目计划书模板组织建设方面
  • 响应式网站模板 视差wordpress后台慢js
  • 做网站什么框架方便广西模板哪家最好
  • 网站页面链接怎么做的上市网络公司排名
  • 外贸工厂网站做seo多吗武夷山景区网站建设优点
  • 网站外包要花多少钱女生喜欢的100种迷你小手工
  • 2018网站内容和备案广告装饰 技术支持 东莞网站建设
  • 中航建设集团有限公司网站温州做网站制作哪家好
  • 网站设计样例抖音代运营服务明细表
  • 自己怎么做搬家网站镇江网站制作网站建设
  • 个人简历模板免费下载网站小程序开发公司简介范本
  • 网站建设学习 服务器桂林论坛爆料
  • 建个站的网站打不开服务外包公司是干什么的
  • 楼盘网站开发报价h5页面制作是什么
  • 开发一个网站需要多少钱做汽车的网站
  • 网站设置页面指什么网络热词2021
  • 设计师用的素材网站有哪些做ui设计工资一般多少
  • 做外贸的物流网站有哪些热门网站有哪些
  • 网站哪里可以做网站开发是怎么开发的
  • 牟平网站制作公司网站登录 效果代码
  • 商标设计logo免费生成器网站深圳科源建设集团有限公司网站
  • 什么网站可以接模具做做网站多少钱一年
  • 一流的中小型网站建设福建设计招聘网站
  • 河北承德建设工程信息网站宁夏建设职业技术学院官方网站