Day2
复杂数据类型补充
列表补充
- 插入数据
- 取出数据
- 删除数据
- 统计数据
- 获得索引
- 清除列表
- 合并数据
- 反转列表
- 排序数据
- 相关复制操作
Python中内存的理解
- 小数据池的理解
- 为了重用数据,整形和短字符串在默认条件下,会只保存一个对象,用其赋值其实是该对象的一个引用
- -5~256整形,21位内的字符串会使用小数据池
- 容器的引用
- 简单复制(a=b),会将a引用,b引用对象内存地址。
- 拷贝(a=b.copy()),会先产生一个type(b)类型的对象,a引用该对象
- 深度拷贝,在拷贝容器时,若容器内部含有可变容器,简单的拷贝,并不能改变对内部容器的引用对象,若再对内部容器进行相关对象操作,则会改变被赋对象。此时需要使用深度拷贝。
# DAY2
# demo_03.py
# 2019.06.12
# 列表补充
list_demo = ['zzh', 'boy', 18, 'fine']
# insert
list_demo.insert(2, 'txx')
print(list_demo)
# ['zzh', 'boy', 'txx', 18, 'fine']
# pop
list_demo_copy = list(list_demo)
for i in range(5):
print(list_demo_copy.pop())
print(list_demo_copy)
# fine
# 18
# txx
# boy
# zzh
# []
# remove
list_demo.remove(18)
print(list_demo)
# ['zzh', 'boy', 'txx', 'fine']
# count
list_demo.append('zzh')
print(list_demo.count('zzh'))
# 2
# index
print(list_demo.index('zzh'))
# 0
# clear
list_demo.clear()
print(list_demo)
#
# 浅拷贝
# id()返回的是引用对象的内存地址
# getrefcount(ref) 某对象的引用数+1
# Python中,整数和短字符串为了重复使用,会在赋相同的值时,引用同一个对象
list_demo = ['zzh', 'boy', 18, 'fine']
list_demo_copy = list_demo.copy()
print(list_demo_copy)
print(str(id(list_demo)) + " " + str(id(list_demo_copy)))
list_demo_copy = list_demo
print(str(id(list_demo)) + " " + str(id(list_demo_copy)))
# extend() 合并
list1 = [1, 2, 3]
list2 = [2, 3, 4]
list1.extend(list2)
print(list1)
#[1, 2, 3, 2, 3, 4]
#反转
list1.reverse()
print(list1)
#[4, 3, 2, 3, 2, 1]
#排序
list1.sort()
print(list1)
list1.sort(reverse=True)
print(list1)
# [1, 2, 2, 3, 3, 4]
# [4, 3, 3, 2, 2, 1]
字典
- 索引操作
- 删除数据
- 更新数据
- 取出数据
- 循环操作
# DAY2
# demo_04.py
# 2019.06.12
# 字典操作
dict1 = {'name': 'zzh', 'age': 21, 'sex': 'man', 'school': '安徽工程大学'}
#按键取值
print(dict1['name'])
print(dict1['school'])
# zzh
# 安徽工程大学
#get
print(dict1.get('school','华南理工大学'))
print(dict1.get('scho','华南理工大学'))
# 安徽工程大学
# 华南理工大学
#len
print(len(dict1))
# 4
#包含关系
print('name' in dict1) # True
print('man' in dict1) #False
#del
print(id(dict1))
del dict1['age']
print(id(dict1))
print(dict1)
#pop
print(dict1.pop('sex'))
print(dict1)
print(dict1.popitem())
# keys values items
print(dict1.keys())
print(dict1.values())
print(dict1.items())
# dict_keys(['name'])
# dict_values(['zzh'])
# dict_items([('name', 'zzh')])
for key in dict1:
print(key)
#name
#update()
dict1.update(name='txx',age='18')
print(dict1)
# {'name': 'txx', 'age': '18'}
元组
- 索引操作
- 循环操作
# DAY2
# demo_04.py
# 2019.06.12
# 元组
tuple1 = (1,2,3,4,5,6)
print(tuple1)
#index
print(tuple1[2])
#3
#section
print(tuple1[2:4])
print(tuple1[::2])
#(3, 4)
#(1, 3, 5)
# len
print(len(tuple1))
# 6
# contain
print(1 in tuple1)
print(0 in tuple1)
#True
#False
#circle
for line in tuple1:
print(line)
# 1
# 2
# 3
# 4
# 5
# 6
集合
# DAY2
# demo_06.py
# 2019.06.12
# 集合
set1 = {1,2,3,1,4,5}
print(set1)
print(set1[1])
文件相关操作
- 文件的三种打开模式
- with关键字
- pip3安装第三方库文件
# open(文件路径,操作模式,字符编码)
# r:能够避免转义符
f = open(r'user.txt','r',encoding='utf-8')
print(f.read())
f.close()
#三大类文件打开模式
#r:读模式
#w:写模式
#a:追加模式
#with open
#可以自动close掉文件句柄对象
#将网络图片拷贝到电脑中
import requests
res = requests.get(r'https://www.4k123.com/data/attachment/forum/201707/25/205333a4rabb811vy4mjuk.jpg')
print(res.content)
with open('girl.jpg','wb') as w:
w.write(res.content)
# 按行读取相关文件
with open("video.mp4","rb") as r, open("video_copy.mp4","wb") as w:
for line in r:
w.write(line)
今日练习
课程任务
- 让用户输入用户名与密码
- 检验用户名存在与否
- 验证密码正确性
- 设置密码输入次数
user.txt文件内容
username:zzh password:zzh123
username:txx assword:txx1234
# DAY2
# demo_02.py
# 2019.06.12
# 函数
#
# print('{}{}{}'.format('1','2','3'))
# print(f'{"2"}{"3"}')
# print(f'{{}}')
def register():
'''
实现注册功能
:return: None
'''
while True:
user = input("输入用户名:").strip()
pwd = input("请输入密码:").strip()
re_pwd = input("请在此输入密码:").strip()
if pwd == re_pwd:
user_info = f'username:{user} password:{pwd}\n'
with open('user.txt', 'a', encoding='utf-8') as w:
w.write(user_info)
print("注册成功")
break;
else:
print("两次密码输入不一致")
def sign_in():
'''
实现登录功能
:return: None
'''
user_list = list()
pwd_list = list()
count = 0
with open('user.txt', 'r', encoding='utf-8') as database:
for line in database:
user_info = line.strip().split(" ")
user_list.append(user_info[0][9:])
pwd_list.append(user_info[1][9:])
while count < 3:
user = input("账号:").strip()
pwd = input("密码:").strip()
if len(user) == 0 or len(pwd) == 0:
print("用户名或密码不能为空")
count += 1
continue
else:
if user in user_list:
if (pwd_list[user_list.index(user)] == pwd):
print("账号密码输入正确")
break
else:
print("账号密码不匹配")
count += 1
continue
else:
print("用户名不存在")
count += 1
continue
# register()
sign_in()