今日作业
一.笔记
# 今日内容
# 1.数据类型剩余的内置方法
# 2.字符编码
# 3.文件管理
# 4.函数处理
#1.数据类型剩余的内置方法
# 需要掌握的知识点
#列表类型
1.insert()#插入
# list1.insert(3,"zym")
print(list1)
list1 = ['tank', 18, 'male', 3.0, 9, '广东']
list1.insert(2, 'oldboy')
print(list1)
2.pop()#取出
3.remove()#移除
4.count()#查看个数
5.index()#查看值得索引
print(list1.index(_'广东'),‘---广东’)
6.clear#清空列表的值
list1.clear()
print(list1)
7.copy()#浅拷贝
#将list1的内存地址浅拷贝赋值给list2 list2=list1.copy() print(list2,'添加值前') #将list1的原地址直接赋值给list3 list3=list1 print(list3,‘添加值前’) list1.append('jason') print(list2,‘添加值后’) print(list3,‘添加值后’) #给list1中的可变列表进行追加值 list[8].append('tank') #打印直接赋值、深、浅拷贝的结果 #浅拷贝:list1的列表中外层值改变对其不影响 #但对list1中的可变类型进行修改则会随之改变值 print(list2) print(list3) #深拷贝 from copy import deepcopy #将list1深拷贝赋值给list4 list4=deepcopy(list1) #追加jason到list1中国 list1.append('jason') print(list2,‘添加值后’) print(list3,‘添加值后’) #深拷贝:把list1中的所有值完全拷贝到一个新的地址中 #进而将list1完全隔离 print(list4)
8.extend()#合并
list1=[1,2,3] list1=[4,5,6] list1.extend(list2) print(list1)
9.reverse()#反转
list1.reverse()
print(list1)
10.sort()#排序
list3=[1,3,5,8,10,2,4,6] #升序 list3.sort() print(list3) #降序 list3,sort(reverse=Ture) print(list3) #tab:往右空四个空格 #shift+tab:往左减四个空格
2.字典的内置方法(字典是无序的)
#1.按照key取/存值 dict=(‘name’:'xingming','age':18,'sex':'male','school':'ahgcdx') #根据key取他的学校 print(dict1['school']) print(dict1['sal']) get() #第一个参数是字典的key #第二个参数是默认值,若key存在则取key对应的值,否则取默认值 print(dict1.get('school','moudaxue')]) print(dict1.get('sal','15000'))
2.len
print(len(dict1))
3.成员运算in和not in
print('name' in dict1) #Ture print('sal' in dict1) #Flase print('sal'not in dict1) #Ture
4.删除
#del dict1["name"]
#print(dict1)
根据字典中的key取出对应的值赋值给变量name
#name=dict1.pop('name') dict2=dict1.pop('name') print(dict1) print(name) #随机取出字典中的某个值 dict1.popitem() print(dict1)
key/values.items print(dict1.key()) 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.元组类型(在小括号内,以逗号隔开存放多个值)
#注意:元组与列表的区别,元组不是
tuple=(1,2,3)
#优先掌握 #1.按索引取值 #print(tuple1[2]) #2.切片(顾头不顾尾) print(tuple[0:6]) #3.步长 print(tuple[0:6:2])#(1,3,5) #4.成员运算 in 和 not in print(1 in tuple1) #Ture print(1 not in tuple1) #Flase #5.循环 for line in tuple1: print(line)
集合类型(一般用于去重)
#在{}以逗号隔开,可存放多个值,饭集合会自带默认取重功能
#set1={1,2,3,4,2,1,3,4}
print(set1)
集合是无需的 set1=set() set2={} print(set1) print(set2) set2=['name']='tank' print(type(set2))
#文件读写基本使用
#对文本进行操作
#open('文件的名字.txt',mode='wt',encoding='uft-8')
#f:称之为 句柄
#r:避免转义符
#打开文件会产生两种资源,一种是python解释器与Python文件的资源,程序结束Python会自动收回
#另一种是操作系统打开文件的资源,文件打开后,操作系统并不会帮我们自动收回,所以需要手动收回资源
#写文件
f=open('/python相关/python_files/ahgcdx/files/文件的名字.txt',mode ='wt',encoding=utf-8) f.write('hello tank!') f.close() #读文件 f=open( 路径 ) res=f.read() print(res) f.close() #文件追加模式a f=open( 路径 ) f.write('hello jason') f.close()
#文件处理之上下文管理:with
#with会自带close()功能
#会在文件处理以后自动调用close()关闭文件
with open(路径) f.write('life is short,u need python!') import requswsts #res=reques.get(' lianjie') #写入图片 #with open('图片名.jpg','wb') as f: #res =f.read() #print(res) with open('图片名.jpg','rb') as f,open('ming.jpg','wb' )as w: #读写视频 with open ('视频文件.mp4','rb') as f,open('视频文件——mp4','wb') as w: res = f.read() print(res) w.write(res)
#一行一行读文件
#with open('tianyan_sys.mp4','rb') as f, open('tianyan_sys_copy.mp4',)
#一次打开文件所有内容,若文件的大小超出内存的大小会导致内存溢出
#f.read() #for line in f: # w.write(line) # res = f.read() # print(res) # w.write(res) #改变视频路径,注意不能放主盘 with open('D:/1.mp4', 'rb') as f, open('E:/asd.mp4', 'wb') as w: for line in f: w.write(line)
#函数
#1.什么是函数
#函数相当于工具,需要先准备好,在需要的时候再使用
# 2.如何使用函数
# 函数必须先定义、后调用
# 3.函数的语法:
# def cup(参数1,参数二···):
# “““
# 函数的声明
# 水杯,用来盛水与喝水
# “““
# 函数体代码(逻辑代码)
# ···
# def: (全称defind)用来声明定义函数的关键词
# 函数名:看其名,知其意
# ():括号,存放的是接收外界的参数
# 注释:用来说明函数的作用
# 函数体代码:逻辑代码
# retur:后面跟函数的返回值
# ···
# 注册功能
# 1.打开python解释器
# 2.加载05,函数基础.文件
# 3.python解释器会帮我们检测py文件中的语法,但是只会检测python语法,不会执行函数体代码
# def foo(): # print('from foo!') # bar() # print(
# 调用阶段,会执行foo函数体代码
# foo()
# 用函数实现登录功能
#1.让用户输入用户名与密码
2.检验用户名是否存在
3.用户名存在后检验密码是否正确,若正确打印“登录成功”,否则打印“用户或密码错误”,并让用户重新输入
4.用户密码输入错误超过三次则会退出循环
# 先定义
# def register():
# 此函数注册功能
#让用户输入用户名和密码
# user = input('请输入用户名:').strip() # pwd = input("请输入密码").strip() # re_pwd = input("请输入密码").strip() # 判断两次输入的密码是否一致 # if ped == re_pwd:
格式化的三种方式
# user_info = ‘用户名:%s,密码:%s’%(user ,pwd) # user_info = ‘用户名:{},密码:{}’.format(user, pwd) # 字符串前写一个f相当于调用format方法 # user—info=f'用户名:{user},密码:{pwd}' # 把用户信息写入文件中 # with open('user.txt','w',encoding='uft_8') as f: # f.write(user_info) # break # else: # print('两次密码不一致,请重新输入!')
函数调用:函数名+括号 即调用函数
register()
二.作业
源代码
dic={ 'shaoxianwei1':{'password':'123456','count':0}, 'shaoxianwei2':{'password':'123456','count':0}, 'shaoxianwei3':{'password':'123456','count':0}, } while True: name=input('username>>: ') if not name in dic: print('用户不存在') continue if dic[name]['count'] > 2: print('尝试次数过多,锁定') continue password=input('password>>: ') if password == dic[name]['password']: print('登录成功') break else: print('用户名或密码错误') dic[name]['count']+=1
运行结果
转载于:https://www.cnblogs.com/shaoxianwei/p/11013307.html