python6_12

笔记

列表常用方法

  • insert()插入
list1 = ['tian',18,'harbin',80]
list1.insert(2,'old')
print(list1)
  • pop()取出
print(list1.pop())
print(list2.pop(2))
  • remove()移除
list1.remove(2)
print(list1)
  • count()计数
print(list1.count('tian'))
  • index()索引
print(list1.index('harbin'),'--harbin')
  • clear()清除
print(list1.clear())
  • copy()浅拷贝
list2 = list1.copy()
print(list2)
  • append()附加
print(list1)
list3 = list1
list1.append('heilongjiang')
print(list3)
list1 = ['tian',[18,'harbin',80]]
list1[1].append('xin')
print(list1)
  • from copy import deepcopy 深拷贝
from copy import deepcopy
list4 = deepcopy(list1)
print(list4)
list1.append('xin')
print(list4)
  • extend()合并
list1 = [1,2,3]
list2 = [1,3,5,7]
# list1.extend(list2)
print(list1)
  • sort()排序
list1.sort(reverse=True)
print(list1)

字典常用方法

  • get()
dict1 = {'name':'田鑫鑫','age':18,'sex':'female','school':'ahpu'}
# print(dict1['school'])
# print(dict1.get('school'))
print(dict1.get('sal'))
  • len()
print(len(dict1))
  • 成员运算in和not in
# print('name' in dict1)
  • 删除
del dict1['sex']
print(dict1)
  • pop()
name = dict1.pop('name')
print(name)
  • 随机取出字典中某一值
dict1.popitem()
print(dict1)
  • keys,values,items
print(dict1.keys())
print(dict1.items())
  • 循环python
for key in dict1:
    print(key)
  • update()更新
dict2 = {'work':'student'}
dict1.update(dict2)
print(dict1)

数据类型剩余的内置方法

元组类型(在小括号内以逗号隔开共存放多个值)

元组与列表区别,元组是不可变类型,列表是可变类型

  • 按索引取值
tuple1 = (1,2,3)
print(tuple1)
  • 切片
print(tuple1[0:2])
  • 步长
print(tuple1[0:6:2])
  • 长度
print(len(tuple1))
  • 成员运算in和not in
print(1 in tuple1)
  • 循环
for line in tuple1:
    print(line)

集合类型
再打括号内,以逗号隔开,可存放多个值,但集合会默认去重

set1 = set ({1,2,3,4,5,5,6})
set2 = {1,2,3,4,5,5,6}
print(set1)
print(set2)

文件读写基本使用
f 称为句柄
open(参数1:‘文件的绝对路径/文件名字’,参数二:‘操作模式,操作三:‘指定字符编码)

  • 写文件
f = open(
         r'D:\tianxinxinpython\xinxin.txt',
         mode='wt',
         encoding='utf-8')
f.write('tianxinxin')
f.close()
  • 读文件
f = open(
         r'D:\tianxinxinpython\xinxin.txt',
         'rt',
         encoding='utf-8')
res = f.read()
print(res)
f.close()
  • 文件追加模式
f = open(
         r'D:\tianxinxinpython\xinxin.txt',
         'at',
         encoding='utf-8')
f.write('     zhuzhanhao')
f.close()

文件处理 上下文管理

  • List item

with自带close()功能,会在文件处理完以后自动调用close()关闭文件

with open(
        r'D:\tianxinxinpython\xinxin.txt',
        'wt',
        encoding='utf-8') as f:
    f.write('life is short,u need python!')
import requests

res = requests.get('图片url')
  
print(res.content)

# 写图片
with open('klay.jpg','wb') as f:
    f.write(res.content)
# 读取图片
with open('klay.jpg','rb') as f:
    res = f.read()
    print(res)

with open('klay.jpg','rb') as f,open('stephen.jpg','wb') as w:
    res = f.read()
    w.write(res)
  • 读写视频
with open('库里扣篮.mp4','rb') as f,open('stephen.mp4','wb') as w:
    res = f.read()
    w.write(res)

    for line in f:
        w.write(line)

函数基础

先定义再使用

  • 函数的语法

def 函数名(参数1,参数2·······):
“”"
函数声明
“”"
函数体(逻辑代码)

  • 注册功能
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)
            user_info = f'用户名: {user}, 密码: {pwd}'

            with open(f'{user}.txt', 'w', encoding='utf-8') as f:
                f.write(user_info)
            break
        else:
            print('两次密码不一致,请重新输入!')
register()
  • 函数在定义阶段发生的事情

    1.打开python解释器
    2.加载python文件
    3.python解释器会检测python语法,不会执行函数体代码

作业

登陆界面

def register():
    '''
    注册功能
    :return:
    '''
    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)
            user_info = f'用户名:{user}, 密码:{pwd}'

            with open(f'{user}.txt', 'w', encoding='utf-8') as f:
                f.write(user_info)
            break
        else:
            print('两次密码不一致,请重新输入!')
register()
def login():
    '''
    登录功能
    :return:
    '''
    with open(
            r'D:\tianxinxinpython\tianxinxin.txt',
            'rt',
            encoding='utf-8') as f:
        res = f.read()
        res1 = res.split(',')
        list1 = res1[0].split(':')
        list2 = res1[1].split(':')
        list1.pop(0).lstrip()
        list2.pop(0).lstrip()
        # print(list1)
        # print(list2)


        u = input('请输入用户名:').strip()
        if u in list1:
            p = input('请输入密码:').strip()
            if p in list2:
                input('登陆成功!')
            else:
                input('密码不正确!')
        else:
            input('用户不存在!')


login()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值