Python day_02

今日内容

1.数据类型剩余的内置方法

2.字符编码

3.文件处理

4.函数基础

 

01.列表类型:

# 1.insert() #插入
#第一个参数:索引 第二个参数:插入的值
list1 = ['tank', 18, 'male', 3.0, 9, '广东', 'tank', [1, 2]]
list1.insert(2, 'oldboy')
print(list1)

# 2.pop() #

# 3.remove()#移除

# 4.count()#查看值的个数
# print(list1.count('tank'))

# 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, '添加值前')

#深拷贝()
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() #合并
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()
# print(list3)

#降序
list3.sort(reverse=True)
print(list3)

#tab :往右空四个空格
#shift + tab : 往左减四个空格

 

#02.字典的内置方法(字典是无序的)

#1.按照key取/存值
dict1 = {'name': '张琦', 'age': '18', 'sex': 'male', 'school': '安徽工程大学'}
#根据Key取张琦的学校
# print(dict1['school'])
# print(dict1['sal'])

#get()
#第一个参数是字典的key
#第二个参数是默认值,若key存在则取得key对应的值,否则取默认值none
# print(dict1.get('school', '华南理工大学'))
# print(dict1.get('sal', '15000'))

#2.len
# print(len(dict1))

#3.成员运算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)

#随机取出字典中的某个值
# dict1.popitem()
# print(dict1)

#keys,values,items
#

#循环
#循环字典中所有的key
# for key in dict1:
# print(key)

#7.update()
# print(dict1)
# dict2 = {"work": "student"}
#将dict2加到dict1字典中
# dict1.update(dict2)
# print(dict1)



元组类型(在小括号内,以逗号隔开存放多个值)
#注意: 元组与列表的区别,元组是不可变类型,列表是可变类型

# turlp1 = (1, 2, 3, 4, 5, 6)
#优先掌握
# 1.按索引取值
# print(turlp1[2])

# 2.切片
# print(turlp1[0:6]) # (1,2,3,4,5,6)

#步长
# print(turlp1[0:6:2]) # (1,3,5)

# 3.长度
# print(len(turlp1)) # 6
# 4.成员运算 in 和 not in
# print(1 in turlp1) # True
# print(1 not in turlp1) # False
# 5.循环
# for line in turlp1:
# 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))


#03.文件读写基本使用

#对文本进行操作
#open(参数1:文件的名字,参数2:操作模式,参数3:指定字符编码)
#f: 称之为 句柄
#r: 避免转义符

#打开文件会产生两种资源,一种是python解释器与python文件的资源,程序结束python会自动回收
#另一种是操作系统打开文件的资源,文件打开后,操作系统不会帮我们自动回收,所以需要手动回收资源

#写文件
# f = open(
# r'D:/pyproject/demo1/file/文件的名字.txt',
# mode='wt',
# encoding='utf-8')
# f.write('hello tank!')
#
# f.close()

#读文件
# f = open(
# r'D:/pyproject/demo1/file/文件的名字.txt',
# 'r', #默认为rt
# encoding='utf-8')
#
# res = f.read()
# print(res)
# f.close()

#文件追加模式 a
f = open(r'D:/pyproject/demo1/file/文件的名字.txt',
'a', #默认为at模式
encoding='utf-8'
)
f.write('\n????')
f.close()

 

# 文件处理之上下文管理: with
# with会自带close()功能
# 会在文件处理完以后自动调用close()关闭文件
# 写文件
# with open(r'D:/pyproject/demo1/file/文件的名字.txt', 'w', encoding='utf-8') as f:
# f.write('life is short, u need python')

# 读文件
# with open(r'D:/pyproject/demo1/file/文件的名字.txt', 'r', encoding='utf-8') as f:
# res = f.read()
# print(res)

#文件追加
with open(r'D:/pyproject/demo1/file/文件的名字.txt', 'a', encoding='utf-8') as f:
f.write('\n????')

04.图片视频读写
import requests # pip3 install requests

res = requests.get('http://game-a1.granbluefantasy.jp/assets_en/img/sp/assets/npc/my/3710090000_01.png')
print(res.content)

# 写入图片
with open('大水比.png', 'wb') as f:
f.write(res.content)


# 读取图片
with open('大水比.png', 'rb') as f:
res = f.read()
print(res)


# 文件拷贝操作
# with open('大水比.jpg', 'rb') as f, open('DSWLP.jpg', 'wb') as w:
# res = f.read()
# w.write(res)


# 读写视频
# with open('视频文件.mp4', 'rb') as f, open('视频文件.mp4', 'wb') as w:
# res = f.read()
# print(res)
# w.write(res)


04.函数基础
'''
1.什么是函数?
函数相当于工具,需要事先准备好,在需要用时再使用

2.如何使用函数?
函数必须先定义,后调用。
'''
3.函数语法:

# def 函数名(参数1,参数2...):
# """
# 注释
# 函数声明
# 水杯,用于盛水,喝水
# """
# 函数体代码(逻辑代码)
# return 返回值

'''
def:(defined)用于声明定义函数的关键字
函数名:看其名,知其意
():括号,括号内存放接收外界的参数
注释:用于说明函数的作用
函数体代码:逻辑代码
return:后面跟函数的返回值
'''
'''
# 函数在定义阶段发生的事
1.打开python解释器
2.加载python文件 .py
3.Python解释器会帮我们解释.py语法,
但只会检测语法,不会执行函数体语法
'''
example
# 注册功能
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(f'user.txt', 'w', encoding='utf-8') as f:
                f.write(user_info)
            break
        else:
            print('两次密码不一致,请重新输入!')

# 调用函数  函数名 + 括号 即调用函数
register()

 

practice today
用函数实现登陆功能

1.让用户输入用户名和密码
2.检测用户名是否存在
3.用户名存在后检测密码是否正确,若正确打印'登陆成功',
否则打印'用户名或密码错误'
4.用户密码输入错误超过三次则退出循环
例程
def login_in():
    count = 0
    print('欢迎登陆')
    while True:
        if count != 3:
            user = ''
            pwd = ''
            dict = {}
            users = input('用户名:').strip()
            pwds = input('密码:').strip()
            with open(r'users.txt', 'r', encoding='utf-8') as f:
                for line in f:
                    line = line.split('\n')[0].split(',')
                    for list in line:
                        if '用户名' in list:
                            user = list[4:]
                        else:
                            pwd = list[3]
                        dict[user] = pwd

            if users in dict:
                if pwds == dict[user]:
                    print('登陆成功')
                    break
                else:
                    print('用户名或密码错误')
            else:
                print('用户名或密码错误')
            count = count + 1
        else:
            print('连续错误三次,退出登录界面')
            break

login_in()

 



 

posted on 2019-06-12 22:11  魔弦音奏  阅读(123)  评论(0)    收藏  举报

导航