精准扶贫 之 python基础003
前期回顾:
001:https://www.jianshu.com/p/f8ae338a1c8d
002:https://www.jianshu.com/p/aae6c16a0f44
6.字符串
定义、输入、输出
①定义:单、双、三引号里面的内容都是字符串。如果内容中本身就就有单引号,两种书写形式:a =“I’m Tom”, b =‘I\’m Tom’。
②字符串的输入:需注意,input函数接收到的内容都是字符串格式!!

③字符串的输出:格式化符号和f‘’之间可以互换。

字符串的下标
下标也叫索引,索引值:

字符串下标的应用——切片


字符串的查找
find():检测某子串是否存在于字符串,如有,返回这个子串开始位置的下标,无则返回-1。index():检测某子串是否存在于字符串,如有,返回这个子串开始位置的下标,无则报错。

字符串的替换
这里需注意,字符串是不可修改的类型,我们需要把替换后的结果赋值给一个新变量。
split():按照指定字符分割字符串,且得到的结果是列表。语法:字符串序列.split(分割字符,number),number表示的是分割字符出现的次数。

join():将子串合并为完整字符串。语法:字符或子串(将来连接的符号).join(字符串名字)

replace():替换。语法:字符串序列.replace(旧子串,新子串,替换次数)

字符串大小写互换
str = ‘hello world’
capitalize():将字符串第一个字符转写成大写,即句首单词首字母大写。 print(str.capitalize()) #Hello world
title():将字符串每个单词首字母大写。 print(str.title()) #Hello World
lower():字符串大写转小写。 print(str.lower()) #hello world
upper():字符串小写转大写。 print(str.upper()) #HELLO WORLD

字符串删除空格

字符串对齐方式
ljust():左对齐,剩下位置用其他字符(默认空格)补齐
rjust():右对齐,剩下位置用其他字符(默认空格)补齐
center():居中对齐,剩下位置用其他字符(默认空格)补齐
字符串序列.ljust/rjust/center(长度,填充字符)

字符串里的判断

7.列表
列表的定义
定义:[ ],且列表中的数据可修改:[数据1,数据2,数据3,…],数据直接要用逗号隔开,各数据从左到右被分配到0123...

列表的查找
• index():返回指定数据所在位置的下标。
语法:列表名称.index(数据,开始位置下标,结束位置下标),位置不写则默认为在全部数据里查找,找不到则报错。
• count():统计指定数据在当前列表中出现的次数。
• len(序列名):统计列表长度,即列表中数据的个数。
• in:判定指定数据在某个列表序列,如果在返回Ture,否则返回False。
• not in:判定指定数据不在某个列表序列,如果不在返回Ture,否则返回False。

列表的增加
• append():列表结尾追加数据(整体)。语法:列表序列.append(数据),可以添加字符串,数字,序列(另一个列表)。
• extend():把数据序列里面的内容拆开后,逐一添加到列表结尾。语法:列表序列.extend(数据),如‘xiao’=》‘x,i,a,o’;而[‘xiao’,’ming’]=》 ‘xiao’,‘ming’。
• insert():指定位置新增数据。语法:列表序列.insert(位置下标,数据)。
列表的删除
• del:既可以删除列表,也可以删除指定下标的数据。 del name_list或者del (name_list)
• pop():删除指定下标的数据,如果不指定,默认删除最后一个数据。 无论删除任何一个数据,pop都会返回删除的数据,即显示删除了哪个。
• remove:删除某一个数据。name_list.remove(数据)
• clear():清空列表。name_list.clear #结果: [ ]
列表的修改
• 修改指定下标的数据:根据下标找到某数据,对其进行重新赋值即可。

• 逆序reverse()。

• sort() 排序:升序和降序,默认是升序排序。key在需要根据关键字排序时使用。

列表的复制
• copy():复制列表内容

列表的循环遍历
遍历:按顺序依次访问到序列中每个数据。

列表嵌套
列表嵌套指一个列表里包含了其他的子列表:
• name_list = [ [a,b.c], [d,e,f],[h,i,j] ]
• 列表嵌套时候的数据查询:
print(name_list[0])则拿到[a,b.c],这个时候我们可以把name_list[0])看作一个序列名,直接print(name_list[0][1])则可以拿到b,以此类推。
8.元组
元组的定义
• 元组:( ),且元组中的数据不可修改。定义元组使用小括号,逗号隔开。

元组的查找
• 元组数据不支持修改,只支持查找,基本和列表一样。

元组的修改(特殊)
元组中的数据不可修改,强行修改直接报错。但是如果元组里嵌套了列表,则可以修改。

9.字典
字典的定义
定义:{ },且字典中的数据可修改:里面的数据都是以键值对形式出现,逗号隔开,无顺序,也不支持下标。

字典的查找


字典的增加/修改

字典的删除

字典的遍历循环

10.集合
集合的定义
定义:创建集合使用{ }或set( ),但是如果创建空集合只能用set( ),有去重功能。
集合里面数据无顺序,输入顺序和输出数据完全不一样。 用set( )存储字符串,打印出来会变成单个字符,逗号隔开。
集合的增加

集合的查找
因为集合不存在顺序,所以无法根据下标查找,只能判断存在与否。

集合的删除
