练习:login功能

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()

列表

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': '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', ''))

2.长度

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)

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.切片,顾头不顾尾

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文件中的语法错误,不具体执行代码

Python Learning Day2的更多相关文章

  1. python s12 day2

    python s12 day2   入门知识拾遗 http://www.cnblogs.com/wupeiqi/articles/4906230.html 基本数据类型 注:查看对象相关成员 var, ...

  2. python learning Exception & Debug.py

    ''' 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数open(),成功时返 ...

  3. (转)Python作业day2购物车

    Python作业day2购物车 原文:https://www.cnblogs.com/spykids/p/5163108.html 流程图: 实现情况: 可自主注册, 登陆系统可购物,充值(暂未实现) ...

  4. Python Learning Paths

    Python Learning Paths Python Expert Python in Action Syntax Python objects Scalar types Operators St ...

  5. Python基础-day2

    1.Python模块python 中导入模块使用import语法格式:import module_name示例1: 导入os模块system('dir')列出当前目录下的所有文件 # _*_ codi ...

  6. Python Learning

    这是自己之前整理的学习Python的资料,分享出来,希望能给别人一点帮助. Learning Plan Python是什么?- 对Python有基本的认识 版本区别 下载 安装 IDE 文件构造 Py ...

  7. python学习day2

    一.模块初识 python模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个 模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用 ...

  8. 【Python Learning第一篇】Linux命令学习及Vim命令的使用

    学了两天,终于把基本命令学完了,掌握以后可以当半个程序员了♪(^∇^*) 此文是一篇备忘录或者查询笔记,如果哪位大佬看上了并且非常嫌弃的话,还请大佬不吝赐教,多多包涵 以下是我上课做的一些笔记,非常的 ...

  9. Python自学day-2

    一.模块     模块分两种:标准库和第三方库,标准库是不需要安装就可以使用的库.     import [模块名]:导入一个库,优先是在项目路径中寻找.自定义模块名不要和标准库模块名相同.   sy ...

随机推荐

  1. Java日志相关概述

    日志是代码调试.生产运维必备工具,基本所有软件都会有日志记录. 1.常用日志框架介绍 1.Logging jdk1.5自带日志工具类,位于java.util.logging; 2.Log4j 市场占有 ...

  2. 008、Java中变量与常量的区别

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  3. Linux每日练习-awk命令之内部自定义函数 20200224

  4. dede:list 与 dede:arclist 的区别

    1.{dede:list}是用于列表页的文章列表调用,通常是用于list_article.htm页面,这个文章列表是可以分页的. 功能说明:表示列表模板里的分页内容列表适用范围:仅列表模板 list_ ...

  5. React 学习笔记(2) 路由和UI组件使用

    安装依赖 cnpm install react-router-dom -S // 或 yarn add react-router-dom 导入 // index.js import React fro ...

  6. Ubuntu 14.04 安装 Dash to Dock

    每次打开或选择一个已经打开的应用都要把鼠标指到左上角,相当费事. Ubuntu 14.04 GNOME自带 Tweaks (系统中名为:优化工具),可以使界面如Windows般(最小化.最大化.底部任 ...

  7. [题解] LuoguP6075 [JSOI2015]子集选取

    传送门 ps: 下面\(n\)和\(k\)好像和题目里的写反了...将就着看吧\(qwq\) 暴力打个表答案就出来了? 先写个结论,答案就是\(2^{nk}\). 为啥呢? 首先你需要知道,因为一个集 ...

  8. mysql第五篇 : MySQL 之 视图、触发器、存储过程、函数、事物与数据库锁

    第五篇 : MySQL 之 视图.触发器.存储过程.函数.事物与数据库锁 一.视图 视图是一个虚拟表(非真实存在的),其本质是‘根据SQL语句获取动态的数据集,并为其命名‘ ,用户使用时只需使用“名称 ...

  9. Linux重要命令练习之bc

  10. SOA--基于银行系统实例分析

    阅读以下关于 Web 系统设计的叙述 [说明] 某银行拟将以分行为主体的银行信息系统,全面整合为由总行统一管理维护的银行信息系统,实现统一的用户账户管理.转账汇款.自助缴费.理财投资.贷款管理.网上支 ...