练习:login功能python
def login(): with open(r'C:\Users\liubin\desktop\user.txt','r') as f: res=f.read() flag=1 list=res.split(',') while flag: user = input('请输入用户名:').strip() for i in range(len(list)): if '用户名' in list[i]: if(user==list[i][4:]): r_pwd=list[i+1][3:] flag=0 break else: print('用户名未注册!') print('从新输入!') for i in range(4): pwd = input('请输入密码:').strip() if pwd==r_pwd: print('登陆成功!') break else: print('用户名或密码错误!从新输入!') login()
列表app
1.insert()函数
# 第一个参数: 索引 第二个参数: 插入的值 list1 = ['tank', 18, 'male', 3.0, 9, '广东', 'tank', [1, 2]]
list1.insert(2, 'oldboy')
print(list1)
2.pop()编码
3.remove()spa
4.count()指针
print(list1.count('tank'))
5.index()code
print(list1.index('广东'), '---广东')
6.clear()orm
list1.clear() print(list1)
7.copy()视频
# 将list1的内存地址浅拷贝赋值给list2 list2 = list1.copy() print(list2, '添加值前') # 将list1的原地址直接赋值给了list3 list3 = list1 print(list3, '添加值前') # 深拷贝() from copy import deepcopy # 将list1的值深拷贝赋值给list4 list4 = deepcopy(list1) # 追加jason到list1中国 list1.append('jason') print(list2, '添加值后') print(list3, '添加值后') # 给list1中的可变列表进行追加值 list1[8].append('tank') # 打印直接赋值、深、浅拷贝的结果 # 浅拷贝: list1的列表中外层值改变对其不影响 # 但对list1中的可变类型进行修改则会随之改变值 print(list2) print(list3) # 深拷贝: 把list1中的全部值彻底拷贝到一个新的地址中 # 进而与list1彻底隔离开 print(list4)
8.extend() blog
list1 = [1, 2, 3] list2 = [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=True) print(list3)
tab : 往右空四个空格
shift + tab : 往左减四个空格
字典
一、按照key取/存值
dict1 = {'name': 'ABC', 'age': 18, 'sex': 'male', 'school': '安工程'} # 根据key提取学校 print(dict1['school']) print(dict1['sal']) # get() # 第一个参数是字典的key # 第二个参数是默认值,若key存在则取key对应的值,不然取默认值 print(dict1.get('school', '华南理工')) print(dict1.get('sal', '15000'))
2.长度
print(len(dict1))
三、成员运算in和not in
print('name' in dict1) # True print('sal' in dict1) # False print('sal' not in dict1) # True
4.删除
del dict1["name"] print(dict1) # pop() # 根据字典中的key取出对应的值赋值给变量name name = dict1.pop('name') print(dict1) print(name)
5.keys.values.items
print(dict1.keys()) print(dict1.values()) print(dict1.items())
6.循环
for key in dict1: print(key)
7.update()
print(dict1) dict2={‘work':'student'} #dict2加入dict1字典中 dict1.update(dict2) print(dict1)
元组类型(在小括号内,以逗号隔开存放多个值)
tuple1=(1,2,3) print(tuple1)
1.按照索引值
print(tuple1[2])
2.切片,顾头不顾尾
print(tuple1[0:6]) print(tuple1[0:6:2])
3.长度
print(len(tuple1))
4.成员运算 in 和 not in
print(1 in tuple1) print(1 not in tuple1)
5.循环
for line in tuple1: print(line)
集合类型
在{ }内,以逗号隔开,可存放多个值,但集合默认去重功能
set1={1,2,3,4,1,2,3,4} print(set1)
集合是无序的
set1=set() set2={} print(set1) print(set2) set2['name']='tank' print(type(set2))
文件读写基本使用
open(参数1:绝对路径 文件名字,参数2:模式,参数3:指定字符编码)
f : 称之为 句柄
写文件 文件开始位置指针覆盖写入
f=open('C:\\Users\\liubin\\Desktop\\文件名字.txt', mode='r+', encoding="utf-8") f.write('hello world') f.close()
读文件
f=open('C:\\Users\\liubin\\Desktop\\文件名字.txt', 'r', encoding="utf-8") print(f.read()) f.close()
文件追加模式 文件末尾指针追加
f=open('C:\\Users\\liubin\\Desktop\\文件名字.txt', 'a', encoding="utf-8") f.write('asdf') f.close()
文件处理上下文管理:with
with自带close()
写文件
with open('C:\\Users\\liubin\\Desktop\\文件名字.txt', mode='w', encoding="utf-8") as f: f.write('life is fantastic')
读文件
with open('C:\\Users\\liubin\\Desktop\\文件名字.txt', mode='r', encoding="utf-8") as f: print(f.read())
追加
with open('C:\\Users\\liubin\\Desktop\\文件名字.txt', 'a', encoding="utf-8") as f: f.write('asdf')
图片操做
写入图片
import requests res=requests.get('http://pic15.nipic.com/20110628/1369025_192645024000_2.jpg') with open('C:\\Users\\liubin\\Desktop\\2.jpg', 'wb') as f: f.write(res.content)
读取图片
with open('C:\\Users\\liubin\\Desktop\\2.jpg', 'rb') as f: res=f.read() print(res)
文件拷贝
with open('C:\\Users\\liubin\\Desktop\\2.jpg','rb') as f,open('C:\\Users\\liubin\\Desktop\\3.jpg','wb') as w: res=f.read() w.write(res)
读写视频
with open('视频文件.mp4') as f,open('视频文件.mp4','wb') as w: res = w.read() print(res) w.write(res)
一行一行读文件,一次打开全部内容致使内存溢出,一行行写入能够避免
with open(r'C:\\Users\\liubin\\desktop\\01.mp4','rb') as f,open('C:\\Users\\liubin\\desktop\\03.mp4','wb') as w: for line in f: w.write(line)
函数#注册功能
def register(): user = input('请输入用户名:').strip() pwd = input('请输入密码:').strip() re_pwd = input('请再输入密码:').strip() while True: if pwd == re_pwd: # user_info = '用户名:%s,密码:%s'%(user,pwd) # user_info = '用户名:{},密码:{}'.format(user,pwd) user_info = f'用户名:{user},密码:{pwd}' # 把用户信息写入文件 with open(r'C:\Users\liubin\desktop\user.txt','w') as f: f.write(user_info) break else: print('两次密码不一致,从新输入!') register()
函数在定义阶段发生如下事件:
发开python解释器
加载py文件
检测py文件中的语法错误,不具体执行代码