一、集合
集合是一个无序的,不重复的数据组合,它的主要作用如下:
- 去重,把一个列表变成集合,就自动去重了
- 关系测试,测试两组数据之前的交集、差集、并集等关系

list_1=[1,2,3,3,3,34]
list_1=set(list_1)
print(list_1,type(list_1))#输出:{1, 2, 3, 34} <class 'set'>
#求交集
list_2=set([1,3,4,4,4,4])
print(list_1.intersection(list_2))#输出:{1, 3}
print(list_1&list_2)#输出:{1, 3}
#并集
print(list_1.union(list_2))
print(list_1|list_2)#输出:{1, 2, 3, 34, 4}
#差集
#在list_1中不在list_2中
print(list_1.difference(list_2))
print(list_1-list_2)#输出:{2, 34}
#在list_2中不在list_1中
print(list_2.difference(list_1))
print(list_2-list_1)#输出:{4}
#对称差集(并集减去交集)
print(list_1.symmetric_difference(list_2))
print(list_1^list_2)#输出:{2, 34, 4}
#判断是否为子集/父集
list_3=set([1,2])
print(list_1.issubset(list_2))#输出:False
print(list_3.issubset(list_1))#输出:True
print(list_1.issuperset(list_3))#输出:True
#添加元素
list_1.add(999)
print(list_1)#输出:{1, 2, 3, 34, 999}
list_1.update([888,666])
print(list_1)#输出:{1, 2, 3, 34, 999, 888, 666}
#删除元素
list_1.remove(999)
print(list_1)#输出:{1, 2, 3, 34, 888, 666}
二、文件的读与写
对文件操作流程
- 打开文件,得到文件句柄并赋值给一个变量
- 通过句柄对文件进行操作
- 关闭文件

f=open("yesterday",'r')#文件句柄,读模式
f=open("yesterday2",'w')#新建一个文件,并写进去 注意!!如果文件原来存在的话,文件会被格式化
f=open("yesterday2",'a')#文件内容添加(append),不覆盖原来的文件
f=open("yesterday2",'r+')#读写模式

f=open("yesterday",'r')#文件句柄,读模式
date=f.read()
print(date)#读文件
f=open("yesterday2",'w')#新建一个文件,并写进去 注意!!如果文件原来存在的话,文件会被格式化
f.write("我爱北京天安门,\n")
f.write("天安门上太阳升")
f=open("yesterday2",'a')#文件内容添加(append),不覆盖原来的文件
f.write("\n我爱北京天安门,,,,,\n")
f.write("天安门上太阳升")
f=open("yesterday",'r')
print(f.readline())#读第一行
for i in range(5):
print(f.readline())#读五行
f=open("yesterday",'r')
#第十行输出分割线
for index,line in enumerate(f.readlines()):
if index==9:
print('---我是分割线---')
continue
print(line.strip())#strip把换行符去了
f=open("yesterday",'r')
#第十行输出分割线、效率高
count=0
for line in f:
if count==9:
print('---我是分割线---')
count+=1
continue
print(line.strip())
count+=1
f=open("yesterday",'r')
print(f.tell())#查看光标所在位置。输出:0
print(f.read(5))#读五个字符,输出:Someh
print(f.tell())#查看光标所在位置。输出:5
f.seek(0)#光标回到第一个位置
print(f.flush())#强制刷新
f=open("yesterday2",'a')
f.seek(10)
f.truncate(20)#截断

f=open("yesterday2","r")
f_new=open("yesterday2.bak","w")
for line in f:
if "肆意的快乐等我享受" in line:
line=line.replace("肆意的快乐等我享受","肆意的快乐等Alex享受")
f_new.write(line)
f.close()
f_new.close()
三、with语句
当with代码块执行完毕时,内部会自动关闭并释放文件资源

with open("yesterday2","r") as f:#自动关闭文件,
print(f.readline())
for line in f:
print(line)
with open("yesterday2","r") as f,\
open("yesterday2","r") as f2:#可以同时打开多个文件
四、函数、过程、函数式编码
1、面向对象》》类》》class
2、面向过程》》过程》》def
3、函数式编程》》函数》》def

#author = YanQi Ye
# coding=gbk
# -*- coding: utf-8 -*
def func1():#函数
"""testing1"""
print('in the func1')
return 0
def func2():#过程
"""testing2"""
print('in the func2')
#调用,过程可以说是没用返回值的函数
x=func1()
y=func2()

#author = YanQi Ye
# coding=gbk
# -*- coding: utf-8 -*
import time
def logger():
time_format = '%Y-%m-%d %X'
time_current = time.strftime(time_format)
with open('a','a+')as f:
f.write('%s end action\n' %time_current)
def test1():
print('test1 starting action')
logger()
def test2():
print('test2 starting action')
logger()
def test3():
print('test3 starting action')
logger()
test1()
test2()
test3()

#author = YanQi Ye
# coding=gbk
# -*- coding: utf-8 -*
#有参数,形参和实参应一一对应
def test1(x,y):
print(x,y)
test1(1,2)#位置参数调用
test1(y=2,x=1)#关键字参数调用
#默认参数
def test1(x,y=2):
print(x,y)
test1(1)#输出:1 2
test1(1,3)#输出:1 3
#参数组(实参个数不固定)
#把n个关键字参数转换为数组方式
def test1(*args):
print(args)
test1(*[1,2,3,3,3,1])
#把n个关键字参数转换为字典方式
def test1(**kwargs):
print(kwargs)
print(kwargs['name'])#单独取值
test1(name='alex',age=8,sex='F')
test1(**{'name':'alex','age':8,'sex':'F'})
五、局部变量与全局变量

#author = YanQi Ye
# coding=gbk
# -*- coding: utf-8 -*
#school全局变量
school="123"
def change_name(name):
global age
age='23'#age变为全局变量,慎用!
print("before change ",name)
name="Alex Li"#name是局部变量,这个函数就是这个变量的作用域
print("after change ",name)
name='Alex'
change_name(name)
print(name)
六、简单的递归
def calc(n):
print(n)
if int(n/2)>0:
return calc(int(n/2))
calc(10)
七、高阶函数与进度条
高阶函数就是函数里调用其他函数

def add(x,y,f):
return f(x)+f(y)
res = add(3,-6,abs)
print(res)
进度条忘记是哪个模块教的了。。。就先放这儿

import sys,time
for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()#强制刷新
time.sleep(0.1)
来源:https://www.cnblogs.com/yanqiyyy/p/10805539.html
