python学习记录二

时间:2022-01-10 00:11:06

一、列表(习惯了OC,总想把它叫成数组):

names = ["ZhangYang","GuYun","XiangPeng","CuLiangChen"]

names.append("LeiHaiDong")#添加
names.insert(1,"ChenRongHua")#插入
names[2] = "XieDi"#修改
#删除
names.remove("ChenRongHua") #del names[1] or names.pop(1)

print(names)
print(names[0],names[2])
print(names[1:3]) #切片
print(names[-2:]) #切片
print(names[:3]) #切片

print(names.index("XieDi"))

names.append("ZhangYang")
print(names.count("ZhangYang"))

names.reverse()#反转
names.sort()#排序

names2 = [1,2,3,4]

names.extend(names2)

二、字符串:

name = "my name is alex"

print(name.capitalize())#首字母大写
print(name.count("a"))
print(name.center(50,"-"))
print(name.endswith("ex"))#判断以ex结尾
print(name.find("name"))#取索引 用于切片 print(name[name.find("name"):])

string
= "my name is {name} and I am {age} years old"
print(string.format(name = 'alex',age = 23))
print(string.format_map({'name':'alex','age':23}))

print('1A'.isdigit())#判断是否为整数
print('+'.join(['1','2','3','4']))
print(name.ljust(50,'*'))
print(name.rjust(50,'*'))
print('Alex'.lower())
print('Alex'.upper())
print('\nAlex\n'.strip())#去换行和空格
p = str.maketrans("abcdef","123456")
print("alex li".translate(p)) #用p加密


print("alex li".replace('l','L',1))#替换
print("alex li".rfind('l'))
 

 三、字典:

info = {'stu1101':"TengLan Wu",'stu1102':"LongZe",'stu1103':"XiaoZe"}

info[
"stu1101"] = "武藤兰"
info[
"stu1104"] = "CangJingKong"
del info["stu1101"] #info.pop("stu1101")
print(info.get('stu1103'))
print('stu1104' in info)

print(info.values())
print(info.keys())

info.setdefault(
"stu1105","TengLan Wu")

b
= {'stu1101':"Alex",1:3,2:5}
info.update(b)
print(info.items())
c
= dict.fromkeys([6,7,8],"test")