Python~Day2

学习内容:

# # 列表类型:
# # 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(reverse=True)
# print(list3)


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



# 一 字典的内置方法(字典是无序的)
# # 1、按照key取/存值
# dict1 = {'name': '张琦', '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、len
# print(len(dict1))  # 4
#
# # 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)
#
# # 5、keys、values、items
# print(dict1.keys())
# print(dict1.values())
# print(dict1.items())
#
# # 6、循环
# # 循环字典中所有的key
# for key in dict1:
#     print(key)
#
# # 7、update()
# print(dict1)
# dict2 = {"work": "student"}
# # 把dict2加到dict1字典中
# dict1.update(dict2)
# print(dict1)


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

# tuple1 = (1, 2, 3, 4, 5, 6)
# print(tuple1)
# # 优先掌握
# # 1.按索引取值
# print(tuple1[2])
#
# # 2.切片(顾头不顾尾)
# print(tuple1[0:6])  # (1, 2, 3, 4, 5, 6)
#
# # 步长
# print(tuple1[0:6:2])  # (1, 3, 5)
#
# # 3.长度
# print(len(tuple1))  # 6
#
# # 4.成员运算 in 和 not in
# print(1 in tuple1)  # True
# print(1 not in tuple1)  # False
#
# # 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(参数1: 文件的绝对路径/文件的名字,参数2:操作模式, 参数3: 指定字符编码)
# f: 称之为 句柄
# r: 避免转义符

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

# 写文件
f = open(
    r'/python相关/python_files/安徽工程/files/文件的名字.txt',
    mode="wt",
    encoding="utf-8")

f.write('hello 安徽工程大学')

f.close()

# 读文件
f = open(
    r'/python相关/python_files/安徽工程/files/文件的名字.txt',
    'r',  # 默认rt
    encoding='utf-8')

res = f.read()
print(res)
f.close()


# 文件追加模式 a
f = open(r'/python相关/python_files/安徽工程/files/文件的名字.txt',
         'a',  # 默认at模式
         encoding='utf-8'
         )

f.write('\nhello jason')

f.close()
# 文件处理之上下文管理: with
# with会自带close()功能,
# 会在文件处理完以后自动调用close()关闭文件

# 写文件
with open(r'file1.txt', 'w', encoding='utf-8') as f:
    f.write('life is short, u need python!')


# 读文件
with open(r'file1.txt', 'r', encoding='utf-8') as f:
    res = f.read()
    print(res)


# 文件追加
with open(r'file1.txt', 'a', encoding='utf-8') as f:
    f.write('\n AAA')
import requests
res = requests.get('https://www.baidu.com/img/bd_logo1.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('百度.png','rb',)as f ,open('baidu.jpg','wb')as w:
#     res=f.read()
#     w.write(res)



# 读写视频
# with open('tianyan_sys.mp4', 'rb') as f, open('tianyan_sys_copy.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', 'wb') as w:

    # 一次打开文件所有内容,若文件的大小超出内存的大小会导致内存溢出
    # f.read()

    # 一行一行读取文件内容,一行一行写入文件中,避免内存溢出
    # for line in f:
    #     w.write(line)

    # res = f.read()
    # print(res)
    # w.write(res)
'''
    1.函数相当于工具,需要事先准备好,在需要时在使用
    2.如何使用函数?
    函数必须先定义,后调用
    3.函数语法
    def 函数名(参数1,参数2,...):
        """
        函数声明
        """
        函数体代码(逻辑代码)
'''

'''
def:(全称define)用来声明定义函数的关键字
函数名:看其名,知其义
():括号,存放的是接受外界的参数
注释:用来说明函数的作用
函数体代码:逻辑代码
return:后面跟函数返回值
'''
'''
函数在定义阶段发生的事情:
1. 先打开python解释器
2. 加载.py文件
3. python会帮助我们检测语法,但是只会检测python语法,不会执行函数体代码
'''

# 注册功能
def register():
    '''
    此函数注册功能
    :return:
    '''
    while True:
        user = input('请输入用户名:').strip()
        pwd = input('请输入密码:').strip()
        re_pwd = input('请输入密码:').strip()   #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('user.txt','w',encoding='utf-8') as f:
            # with open(f'{user}.txt','w',encoding='utf-8') as f:
                f.write(user_info)
            break
        else:
            print("两次密码不一致,请重新输入:")

# 调用函数: 函数名() ,即调用函数
# 调用阶段会执行,函数体代码
register()

作业 :

# 测试用数据
with open('usrdata.txt', 'wt', encoding='UTF-8') as f:
    f.write('用户名:szz,密码:9824\n用户名:szz,密码:9824')



def login():
    '''
    登录函数
    '''

    usr = ''
    pwd = ''
    dict1 = {}
    with open('usrdata.txt', 'rt', encoding='UTF-8') as w:
        for line in w:
            line = line.split('\n')[0].split(',')
            for data in line:
                if '用户名' in data:
                    usr = data[4:]
                else:
                    pwd = data[3:]
                dict1[usr] = pwd
    while True:
        usr1 = input('请输入用户名:').strip()

        if usr1 in dict1:
            i = 1
            while i <= 3:
                pwd1 = input('请输入密码:')
                if pwd1 == dict1[usr1]:
                    print('登陆成功!')
                    break
                else:
                    i += 1
            else:
                print('密码错误超过三次!')
        else:
            print('用户名不存在!')
        break



login()

猜你喜欢

转载自www.cnblogs.com/szzb/p/11013487.html
今日推荐