day②-python基础
本节内容[*]列表、元组操作
[*]字符串操作
[*]字典操作
[*]集合操作
[*]文件操作
[*]字符编码与转码
1. 列表、元组操作<br><br>列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
定义列表
names = ['Alex',"Tenglan",'Eric']
通过下标访问列表中的元素,下标从0开始计数
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831175621749-2010919821.png
切片:取多个元素
>>> names = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
>>> names#取下标1至下标4之间的数字,包括1,不包括4
['Tenglan', 'Eric', 'Rain']
>>> names #取下标1至-1的值,不包括-1
['Tenglan', 'Eric', 'Rain', 'Tom']
>>> names
['Alex', 'Tenglan', 'Eric']
>>> names[:3] #如果是从头开始取,0可以忽略,跟上句效果一样
['Alex', 'Tenglan', 'Eric']
>>> names #如果想取最后一个,必须不能写-1,只能这么写
['Rain', 'Tom', 'Amy']
>>> names #这样-1就不会被包含了
['Rain', 'Tom']
>>> names #后面的2是代表,每隔一个元素,就取一个
['Alex', 'Eric', 'Tom']
>>> names[::2] #和上句效果一样
['Alex', 'Eric', 'Tom']
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831175727395-768827588.png
追加
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831175712943-160842007.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831175749282-686086476.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831175825564-1991880823.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831175851851-700078345.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831175905396-1993980456.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831175914515-1425877539.png
copy真的这么简单么?那我还讲个屁。。。
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831175938303-1015436454.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180119080-539786335.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180128941-1630492595.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180141640-1239895888.png
2. 字符串操作 <br><br>特性:不可修改
name.capitalize() 首字母大写
name.casefold() 大写全部变小写
name.center(50,"-")输出 '---------------------Alex Li----------------------'
name.count('lex') 统计 lex出现次数
name.encode()将字符串编码成bytes格式
name.endswith("Li")判断字符串是否以 Li结尾
"Alex\tLi".expandtabs(10) 输出'Alex Li', 将\t转换成多长的空格
name.find('A')查找A,找到返回其索引, 找不到返回-1
format :
>>> msg = "my name is {}, and age is {}"
>>> msg.format("alex",22)
'my name is alex, and age is 22'
>>> msg = "my name is {1}, and age is {0}"
>>> msg.format("alex",22)
'my name is 22, and age is alex'
>>> msg = "my name is {name}, and age is {age}"
>>> msg.format(age=22,name="ale")
'my name is ale, and age is 22'
format_map
>>> msg.format_map({'name':'alex','age':22})
'my name is alex, and age is 22'<br><br>msg.index('a')返回a所在字符串的索引
'9aA'.isalnum() True
'9'.isdigit() 是否整数
name.isnumeric
name.isprintable
name.isspace
name.istitle
name.isupper
"|".join(['alex','jack','rain'])
'alex|jack|rain'<br><br>maketrans
>>> intab = "aeiou"#This is the string having actual characters.
>>> outtab = "12345" #This is the string having corresponding mapping character
>>> trantab = str.maketrans(intab, outtab)
>>>
>>> str = "this is string example....wow!!!"
>>> str.translate(trantab)
'th3s 3s str3ng 2x1mpl2....w4w!!!'
msg.partition('is') 输出 ('my name ', 'is', ' {name}, and age is {age}')
>>> "alex li, chinese name is lijie".replace("li","LI",1)
'alex LI, chinese name is lijie'
msg.swapcase 大小写互换<br><br> >>> msg.zfill(40)
'00000my name is {name}, and age is {age}'
>>> n4.ljust(40,"-")
'Hello 2orld-----------------------------'
>>> n4.rjust(40,"-")
'-----------------------------Hello 2orld'<br><br>>>> b="ddefdsdff_哈哈"
>>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180249120-2030249876.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180259663-448894391.png
3. 字典操作<br><br>字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。
语法:
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}字典的特性:
[*]dict是无序的
[*]key必须是唯一的,so 天生去重
增加
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180437876-2714003.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180458663-930001977.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180519039-92880806.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180549226-1684394584.png
程序练习
程序: 三级菜单
要求:
[*]打印省、市、县三级菜单
[*]可返回上一级
[*]可随时退出程序
menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
}<br><br>exit_flag = False
current_layer = menu
layers =
while notexit_flag:
for k in current_layer:
print(k)
choice = input(">>:").strip()
if choice == "b":
current_layer = layers[-1]
#print("change to laster", current_layer)
layers.pop()
elif choice notin current_layer:continue
else:
layers.append(current_layer)
current_layer = current_layer
三年菜单文艺青年版
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180642677-183675113.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180655451-2094276278.png
4.集合操作<br><br>集合是一个无序的,不重复的数据组合,它的主要作用如下:
[*]去重,把一个列表变成集合,就自动去重了
[*]关系测试,测试两组数据之前的交集、差集、并集等关系
常用操作
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180729399-1362514101.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180741754-102398565.png
5. 文件操作<br><br>对文件操作流程
[*]打开文件,得到文件句柄并赋值给一个变量
[*]通过句柄对文件进行操作
[*]关闭文件
现有文件如下
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180804973-879369294.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180815374-150318592.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180827477-813068271.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180840982-2037820505.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180858777-1869311693.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180924430-2006024927.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180936012-1578289288.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831180946540-763912647.png
原配置文件
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831181004368-268576300.png
6. 字符编码与转码<br><br>详细文章:
http://www.cnblogs.com/yuanchenqi/articles/5956943.html
http://www.diveintopython3.net/strings.html
需知:
1.在python2默认编码是ASCII, python3里默认是unicode
2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节), so utf-16就是现在最常用的unicode版本, 不过在文件里存的还是utf-8,因为utf8省空间
3.在py3中encode,在转码的同时还会把string 变成bytes类型,decode在解码的同时还会把bytes变回string
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831181045041-466307103.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831181059085-844214374.png
https://img2023.cnblogs.com/blog/1907115/202308/1907115-20230831181110929-656046024.png
7. 内置函数
<br><br>
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]