1、列表类型
insert() 插入 第一个参数:索引 第二个参数:植入得值 list4=['wangjialin',18,'male',3.0,9,'安徽','wangjialin',[1,2]] list4.insert(2,'oldboy') print(list4) # count()#查看个数 print(list4.count('wangjialin')) # index()#索引 print(list4.index(18),'----年龄') #clear() list4.clear() print(list4) # copy()#浅拷贝,深拷贝 list5=list4.copy() list7=list4 print(list5,'添加值前') # 深拷贝 from copy import deepcopy list6=deepcopy(list4) list4.append('linda') list4[8].append('w') print(list7) print(list5,'添加值后') print(list6,'添加值后') # extend()# 合并 list1=[1,2,3] list2=[1,2,3,4] list1.extend(list2) print(list1) # reverse()反转 list1.reverse() print(list1) #.sort()排序 list3=[2,3,7,4,5,2,1] list3.sort() print(list3) # 降序 list3.sort(reverse=True)#Ture必须大写 print(list3) # tab:空四个空格 #shift+tab 退四个空格
2、字典内置方法
#1、按照key取/存值 dict1={'name':'小小','age':18,'sex':'male','school':'安徽工程大学'} print(dict1['school']) print(dict1['sat'])#报错 #get()更好用 print(dict1.get('school','牛'))#输出字典中默认值,不会输出牛 print(dict1.get('sat'))#不会报错 print(dict1.get('sat','狗'))#输出给的值 #长度 print(len(dict1)) #成员运算in or not in print('name'in dict1) print('sat'in dict1) print('sat'not in dict1) #删除 del dict1['name']#删除给定值 print(dict1) #pop name=dict1.pop('name') print(dict1) print(name) #随机取出某值,字典是无序类型 dict1.popitem() print(dict1) #keys values items print(dict1.keys()) print(dict1.values()) print(dict1.items()) # 循环 # 循环字典中所有的key for key in dict1: print(key) update() print(dict1) dict2={'work':'student'} # 把dict2加到dict1字典中 dict1.update(dict2) print(dict1)
3、元组类型
#元组类型(在小括号内,以逗号隔开存放多个值) #注意:元组与列表的区别,元组是不可变类型,列表是可变类型 tuple1=(1,2,3,4,5,6,7) print(tuple1) #按索引取值 print(tuple1[2]) #切片 print(tuple1[0:6]) #步长 print(tuple1[0:6:2]) #长度 print(len(tuple1)) #成员运算 print(1 in tuple1) print(9 in tuple1) print(1 not in tuple1) #循环 for line in tuple1: print(line)
4、集合类型
#集合类型(在大括号内以逗号隔开,可存放多个值,但集合会自带默认去重功能) set={1,3,3,4,3,2,1,4} print(set) #集合无序 set1=set() set2={} print(set1) print(set2) set2['name']='wangjialin' print(set2) print(type(set2))
5、文件处理
#文件处理 #文件读写基本使用 #对文本进行操作 #open(参数1:文件名,参数2:操作方式,参数3:指定字符编码) #f:句柄 f=open(r'C:\Users\85265\PycharmProjects\untitled\venv\文件名.txt',mode='wt',encoding='utf-8') f.write('hello world') f.close() #读文件 f=open(file=r'C:\Users\85265\PycharmProjects\untitled\venv\文件名.txt',mode='rt',encoding='utf-8') print(f.read()) f.close() #文件的追加模式(运行一次追加一次) f=open(file=r'C:\Users\85265\PycharmProjects\untitled\venv\文件名.txt',mode='a',encoding='utf-8') f.write(' linda') f.close() #文件处理之上下文管理:with #with 自带close功能,会在文件处理完之后自动调用close()关闭文件 #写文件 with open(r'C:\Users\85265\PycharmProjects\untitled\venv\文件名.txt','w',encoding='utf-8') as f: f.write('i am boy.') #读文件 with open(r'C:\Users\85265\PycharmProjects\untitled\venv\文件名.txt','r',encoding='utf-8') as f: print(f.readline()) #文件追加 with open(r'C:\Users\85265\PycharmProjects\untitled\venv\文件名.txt','a',encoding='utf-8') as f: f.write('\ni am boy.') # import requests #pip3 install requests res=requests.get('http://pic18.nipic.com/20120204/8339340_144203764154_2.jpg') print(res.content) # 图片与视频的读写操作 # 写入图片 with open('图片.jpg','wb') as f: f.write(res.content) # 读取图片 with open('图片.jpg','rb') as f: res=f.read() print(res) # 图片拷贝操作 with open('图片.jpg','rb') as f,open('bird.jpg','wb') as w: res=f.read() print(res) # 视频拷贝操作后缀不同,其余同上 # 一行一行读文件 with open(r'E:\steam\steamapps\workshop\content\431960\1344567516\screenclean.mp4','rb')as f,open('screenclean_copy.mp4','wb')as w: # f.read() for line in f: w.write(line)
6、函数体系
'''
1、什么是函数?
辅助工具,事先准备好,需要时使用。
2、如何使用函数?
先定义,后调用。
3、函数的语法:
def cup(参数1,参数2,参数3,.....): #(参数是接受外部来的)
"""
函数的声明
水杯用来喝水盛水
"""
函数体代码(逻辑代码)
'''
'''
def:(全称define) 用来生明定义函数的关键字
函数名:看名知义意
():存放接受外界的参数
注释:用来说明函数的作用
函数体代码:逻辑代码
return:后面跟函数的返回值
'''
#注册功能 def register(): while True: user=input('请输入用户名:').strip() pwd=input('请输入密码:').strip() re_pwd=input('请确认密码:').strip() # 判断密码是否相同 if pwd==re_pwd: # user_info='用户名:%s,密码:%s'%(user,pwd) # user_info='用户名:{},密码:{}'.format(user,pwd) # 字符串前写一个f相当于调用format方法 user_info=f'用户名:{user},密码:{pwd}' # 把用户信息写入文件 with open('文件名.txt','w',encoding='utf-8')as f: f.write(user_info) break else: print("密码不一致,请重新输入!") # 函数调用 函数名+括号 即调用函数 register()
# 登录功能 def login(): while True: user_name=input('用户名:') user_pwd=input('密码:') with open('文件名.txt','r',encoding='utf-8')as f: res=f.read() res1=res.split(',') res2 = res1[0].strip('用户名:') if user_name == res2: res3 = res1[1].strip('密码:') print(res3) if user_pwd == res3: print('登录成功') else: print('密码错误') else: print('用户名不存在') login()
注册登录结果: