1 Python基本数据类型
1.1 字符串str类型的处理
#str 转换成bool print(bool(" ")) #字符串是有索引的 name='TracyMcGrady' name_1=name[0] print(name_1) name_2=name[-1] print(name_2) #切片 name_1=name[0:5] #顾头不顾尾 print(name_1) name_2=name[-7:] print(name_2) name_3=name[:5:2] print(name_3) #倒着取 name_4=name[-1:-8:-1] print(name_4) #颠倒字符串 name_5=name[-1::-1] print(name_5) name="TracyMcGrady" print(name.capitalize()) #*只首字母大写 print(name.upper()) #全部大写 print(name.lower()) #全部小写 print(name.swapcase()) #*大小写翻转 print(name.title()) #首字母大写,非字母隔开的每个单词首字母大写 print(name.center(30,"*")) #居中,默认填充物是空格 print(name.startswith("Mc",5)) #以什么什么开头 print(name.endswith("y",-1)) #以什么什么结尾 name=" TracyMcGrady " print(name.strip()) #***去重换行符号, ,登录的时候会用到 names="TracyMcGrady:Kobe:James" print(names.split(":")) #***切割 print("+".join(names)) #把加号插入到每个字符中间 L1=["xiaojin","hujingjing","hanoeng"] print("_".join(L1)) #把列表的值转换成字符串 name="TracyMcGrady" print(name.replace("TracyMcGrady","James")) #***替换 #公共方法: s="nikending不知道zheyouduochang" print(len(s)) #多长 print(s.count("n")) #出现几次 #格式化输出format res="My name is {0},I am born in {1},I like {2},My name is {0}".format("xiaojin",1995,"Basketball") print(res) ''' 1,验证码不区分大小 2,登录的去除空格 ''' #输入验证码,不会区别大写小 code="AbCd" your_code=input("your code >>:") auth=your_code.upper() code=code.upper() if auth == code: print("ok") #登录去除收尾空格,制表符号 name=input("user>>:").strip() print(name)
1.2 列表list的处理
''' 列表: 1,能储存大量的数据 2,切片 3,列表增删改查 ''' #增加 info=["a","c","d"] info.append("e") #在后面增加 print(info) info.insert(1,"b") #插入到1号索引位置 print(info) info.extend(["f","g","h"]) #把迭代器的每个迭代对象逐个添加到列表里面 print(info) #删除 print(info.pop(-1)) #按索引删除 print(info.remove("g")) #按元素删除 #info.clear() #清空列表,但在内存中,只是数据没有 #del info #从内存删除,能切片删除 print(info) del info[-3:] print(info) #更改 info[1]="a" #按元素去改 print(info) info[:3]=["q","w","e"] #按区域更改 print(len(info)) #查找列表有几个元素 L1=[9,8,7,6,5,4,3,2,1,3,4] #sort从小到大 L1.sort() print(L1) L1.sort(reverse=True) #颠倒 print(L1) L1=[3,5,1,2,3,6] L1.reverse() #直接颠倒不排序,相当于L2=L1[::-1] print(L1) #***列表的嵌套 L1=[1,2,"TracyMcGrady",[4,5,"kobe","肖进",77],"GoodBye"] #将TracyMcGrady全部变成大写 #给[4,5,"肖进"]追加一个"胡婧婧" #把kobe大写 #把77变成777 L1[2]=L1[2].upper() print(L1) L1[3].append("胡婧婧") print(L1) L1[3][2].title() print(L1) #L1[3][4]=str(L1[3][4])+"7" #L1[3][4]=int(L1[3][4])+700 ''' 漂亮的人的工资涨500 丑的人从的重列表中删除 ''' students=[ {"name":"肖进","age":23,"salary":5000,"is_pretty":True}, {"name":"胡婧婧","age":23,"salary":7000,"is_pretty":True}, {"name":"韩鹏","age":23,"salary":2000,"is_pretty":False}, ] count=-1 for student in students: count=count+1 if student.get("is_pretty"): student["salary"]=student.get("salary")+500 else: del students[count] print(students)
1.3 字典的数据处理
info={"name":"肖进","age":21,"is_cool":True} #增改 info["high"]=180 info["name"]="韩鹏" info["is_cool"]=False info.setdefault("home","8511") #默认,如果有,则覆盖 print(info) #删除 info.pop("age") #删除某个key print(info.pop("{0}","没有此key{0}".format("age1"))) #防止报错 # print(info) # info.clear() #清空字典 # del info["name"] #删除字典的某个key-value # print(info) #查询 print(info.get("name","没有此key")) print(info.get("name1","没有此key")) for key in info.keys(): print(key) for value in info.values(): print(value) for key,item in info.items(): print(key,item) # fromkeys,多个key对应一个value dic=dict.fromkeys("abc",[]) dic.get("a").append("cool") print(dic) #字典的覆盖 info1={"name":"xiaojin","age":23,"is_cool":True} info2={"name":"hanpeng","sex":"man"} #把字典1覆盖到字典2 info2.update(info1) print(info1) print(info2) info={"name":"肖进", "age":"23", "home":"黑龙江", "company":{ "name":"指掌易", "salary":5000, "work_time":"4个月", "beautiful":True, } } is_beautiful=info["company"]["beautiful"] if is_beautiful: pay_rise=info["company"]["salary"] info["company"]["salary"]=pay_rise+500 print(info) ''' None,0,空(空字符串,空列表,空字典等)三种情况下布尔值为False ''' name_info={ "name_list":["Iverson","James","Kobe","TracyMcGrady"], "TracyMcGrady":{ "age":36, "born":1974, "profession":"basketball", "home":"usa", } } #1,["Iverson","James","Kobe","TracyMcGrady"]追加一个元素,'Arlen' #2,将Kobe全部变成大写。 #3, 将basketball首字母大写。 #3,将usa改成美国。 name_info.get("name_list",None).append("Arlen") print(name_info) name_info.get("name_list",None)[2]=name_info.get("name_list",None)[2].upper() print(name_info) name_info.get("TracyMcGrady")["profession"]=name_info.get("TracyMcGrady")["profession"].capitalize() print(name_info) name_info.get("TracyMcGrady")["home"]="美国" print(name_info)
1.4 集合的处理
#集合 ''' 无序 关系测试,交集,并集,子集,差集 去重(列表的去重) ''' # set1={1,"alex",False,(1,2,3)} # L1=[1,1,2,3,4,4,4,5,6,7] # L2=set(L1) # print(L2) #交集 set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1&set2) #并集 print(set1|set2) #差集 #set1独有的 print(set1-set2) #反交集 print(set1^set2)
1.5 防错指南
''' for 循环当中不要轻易去删除列表,字典中的值 因为删除会破坏索引,或者对应的key,使循环不完整,容易出错 ''' #列表删除值的方法 name_lists=["TracyMcGrady","Kobe","James","Iverson","Carter"] #删除基数的索引 # 方法一: del name_lists[1::2] print(name_lists) #方法二:颠倒列表索引 for i in range(len(name_lists)-1,-1,-1): if i % 2 == 1: del name_lists[i] print(name_lists) #删除字典里面带c并且不区分大小写 name_dict={ "TracyMcGrady":"火箭", "Kobe":"湖人", "James":"骑士", "Carter":"小牛", "Iverson":"费城76人", } list_name=[] for name in name_dict: name1=name.upper() if "C" in name1: list_name.append(name) for l in list_name: name_dict.pop(l,None) print(name_dict)
1.6 深浅copy
#浅拷贝copy: #对于浅copy来说,第一层创建的是新的内存地址,而从第二层开始,指向的都是同一个内存地址, #所以,对于第二层以及更深的层数来说,保持一致性。 L1=[1,2,[1,2]] L2=L1.copy() L1[2].append(3) print(id(L1),L1) print(id(L2),L2) L1.append(4) #深拷贝deepcopy import copy L1=[1,2,[1,2]] L2=copy.deepcopy(L1) L1[2].append(3) print(id(L1),L1) print(id(L2),L2)