# 一、集合的使用

• 去重，把一个列表变成集合，就自动去重了
• 关系测试，测试两组数据之前的交集、差集、并集等关系
```list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

print(list_1, type(list_1))

list_2 = set([2, 6, 0, 66, 22, 8, 4])
print(list_1, list_2)

print(list_1.intersection(list_2))             # 求交集
print(list_1.union(list_2))                    # 求并集
print(list_1.difference(list_2))               # 求差集, in list_1 but not in list_2
print(list_2.difference(list_1))               # 求差集, in list_2 but not in list_1
print(list_1.issubset(list_2))                 # 判断list_1是否为list_2子集
print(list_1.issuperset(list_2))               # 判断list_1是否为list_2父集

list_3 = set([1, 3, 7])
print(list_3.issubset(list_1))                 # list_3是list_1子集
print(list_1.issuperset(list_3))               # list_1是list_3父集
print(list_1.symmetric_difference(list_2))     # 对称差集，两个集合都互相没有都即去掉重复值

list_4 = set([5, 6, 8])
print(list_3.isdisjoint(list_4))               # 交集为null返回true

# 用符号表示
print(list_1 & list_2)                         # 交集
print(list_1 | list_2)                         # 并集
print(list_1 - list_2)                         # 差集,in list_1 but not in list_2
print(list_1 ^ list_2)                         # 对称差集

# 基本操作
print(list_1)

list_1.update([888, 777, 555])                 # 添加多个
print(list_1)

list_1.remove(888)                             # 删除指定元素
print(list_1)

print(list_1.pop())                            # 随机删除一个元素，并返回该元素

x = 5
x in list_4                                    # 测试x是否是list_4的成员
x not in list_4                                # 测试x是否不是list_4的成员
list_4.issubset(list_2)                        # 测试是否list_4中的每一个元素都在list_2中
list_4 <= list_2

list_4.issuperset(list_2)                      # 测试是否list_2中每一个元素都在list_4中
list_4 >= list_2```

# 二、文件操作

1.对文件，得到文件句柄并赋值给一个变量

2.通过句柄对文件进行操作

3.关闭文件

```Somehow, it seems the love I knew was always the most destructive kind

Yesterday when I was young

The taste of life was sweet

As rain upon my tongue

I teased at life as if it were a foolish game

The way the evening breeze

May tease the candle flame

The thousand dreams I dreamed

The splendid things I planned

I always built to last on weak and shifting sand

I lived by night and shunned the naked light of day

And only now I see how the time ran away

Yesterday when I was young

So many lovely songs were waiting to be sung

So many wild pleasures lay in store for me

And so much pain my eyes refused to see

I ran so fast that time and youth at last ran out

I never stopped to think what life was all about

And every conversation that I can now recall

Concerned itself with me and nothing else at all

The game of love I played with arrogance and pride

And every flame I lit too quickly, quickly died

The friends I made all somehow seemed to slip away

And only now I'm left alone to end the play, yeah

Oh, yesterday when I was young

So many, many songs were waiting to be sung

So many wild pleasures lay in store for me

And so much pain my eyes refused to see

There are so many songs in me that won't be sung

I feel the bitter taste of tears upon my tongue

The time has come for me to pay for yesterday

When I was young

`data = open("yesterday",encoding="utf-8").read()  `
```f = open("yesterday",'r',encoding="utf-8")  # 文件句柄,r为只读模式
print(data)
print('--------data2----%s---' %data2)      # 打印结果data2为空
f.close()```
```f = open("yesterday2",'w',encoding='utf-8')  # w是创建一个文件去写，会覆盖已有的文件
f.write("我爱北京天安门,\n")
f.write("天安门上太阳升")
f.close()```
```f = open("yesterday2",'a',encoding="utf-8")  # a 追加模式,不能读
f.write("\n我爱北京天安门，\n天安门上太阳升")
f.close()```
```f = open("yesterday","r",encoding="utf-8")   # 读前五行
for i in range(5):
f.close()```
```f = open("yesterday","r",encoding="utf-8")
f.close()```
```f = open("yesterday","r",encoding="utf-8")
for line in f.readlines():                   # 循环读每一行
print(line.strip())
f.close()```

low loop

```f = open("yesterday","r",encoding="utf-8")
for index,line in enumerate(f.readlines()):                   # 不打印第9行
if index == 9:
print("----我是分割线---")
continue
print(line.strip())
f.close()```

high bige,速度最快,f不是列表

```f = open("yesterday","r",encoding="utf-8")
count = 0
for line in f:
if count == 9:
print("----我是分割线----")
count += 1
continue
print(line)
count += 1
f.close()```
```f = open("yesterday", "r", encoding="utf-8")
print(f.tell)                   # 打印字符位置
print(f.tell())
f.seek(0)                       # 回到首位
f.seek(10)                      # 回到位置10
print(f.encoding)               # 打印文件编码
print(f.fileno())               # 打印文件句柄编号
print(f.name)                   # 打印文件名字
print(f.flush())                # 将缓存文件刷新到硬盘
f.close()```

```import sys,time

for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.1)```
```f = open("yesterday2", "a", encoding="utf-8")
f.seek(10)                      # 这个操作失效，不会影响截断位置
f.truncate(10)                  # 截断，从10个字符位置后开始截断```
```f = open("yesterday2", "r+", encoding="utf-8")      # r+读追加模式
print(f.tell())
f.write("--------diao--------")                     # 会追加到文件内容最末尾，并不会在第san
f.close```
```f = open("yesterday2", "w+", encoding="utf-8")      # w+写读模式，先写后读
f.write("------diao------1\n")
f.write("------diao------1\n")
f.write("------diao------1\n")
f.write("------diao------1\n")
print(f.tell())
f.seek(10)
print(f.tell())
f.write("should be at the begining of the second line")     # 从最后位置写
f.close()```
`f = open("yesterday2", "a+", encoding="utf-8")          # 追加读`
```f = open("yesterday2", "rb")              # 二进制格式去读文件,不需要encoding；python3只能用二进制网络传输(python2可以用字符);视频文件;
f.close()```
```f = open("yesterday2", "wb")                # 二进制格式写
f.write("hello binary\n".encode())          # 将二进制转换为程序默认字编码
f.close()```
`f = open("yesterday2", "ab")                  # 二进制格式追加 `

• 打开文件的模式
• r，只读模式（默认）
• w，只写模式。【不可读；不存在则创建；存在则删除内容；】
• a，追加模式。【可读； 不存在则创建；存在则只追加内容；】
• "+" 表示可以同时读写某个文件
• r+，可读写文件。【可读；可写；可追加】
• w+，写读
• a+，可读可追加
• "U"表示在读取时，可以将 \r \n \r\n自动转换成 \n （与 r 或 r+ 模式同使用）
• rU
• r+U
• "b"表示处理二进制文件（如：FTP发送上传ISO镜像文件，linux可忽略，windows处理二进制文件时需标注）
• rb
• wb
• ab

```f = open("yesterday","r",encoding="utf-8")
f_new = open("yesterday2.bak","w",encoding="utf-8")
for line in f:
if "肆意的快乐等我享受" in line:
line=line.replace("肆意的快乐等我享受","肆意的快乐等Alex享受")
f_new.write(line)
f.close()
f_new.close()```

with 语句,不需要每次都close()

```with open("yesterday2","r",encoding="utf-8") as f:
for line in f:
print(line)```

```with open("yesterday2","r",encoding="utf-8") as f,\
open("yesterday","r",encoding="utf-8") as f2:
for line in f:
print(line)```

# 三、字符编码与转码

1.在python2默认编码是ASCII, python3里默认是unicode 2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节)，utf-8(占1-4个字节)， so utf-16就是现在最常用的unicode版本， 不过在文件里存的还是utf-8，因为utf8省空间 3.在py3中encode,在转码的同时还会把string 变成bytes类型，decode在解码的同时还会把bytes变回string

python2中的编码转换

```#-*- coding:utf8 -*-
import sys

print(sys.getdefaultencoding())  # 打印系统默认编码
s="你好"
print(s)

# utf-8要先转换为unicode
s_to_unicode=s.decode("utf-8")
print(s_to_unicode,type(s_to_unicode))
# unicode转换为gbk
s_to_gbk=s_to_unicode.encode("gbk")
print(s_to_gbk)

# gbk转换为utf-8，需要先将gbk转换为Unicode，再转换为utf-8
gbk_to_utf8=s_to_gbk.decode("gbk").encode("utf-8")
print(gbk_to_utf8)

t=u"你好"               # u默认代表Unicode格式,不需要再将t转换为Unicode
t_to_gbk= t.encode("gbk")
print(t_to_gbk)```

Python3默认编码是utf-8

```import sys
print(sys.getdefaultencoding())

s = "你哈"
s_gbk=s.encode("gbk")

print(s_gbk)
print(s.encode())

gbk_to_utf8= s_gbk.decode("gbk").encode("utf-8")
print("utf8",gbk_to_utf8)```

```#!/usr/bin/env python3
# -*- coding:gbk -*-
# Author: Erick Zhang

import sys
print(sys.getdefaultencoding())

s="你哈"
print(s.encode("gbk"))
print(s.encode("utf-8"))
print(s.encode("utf-8").decode("utf-8").encode("gb2312").decode("gb2312"))```

# 四、函数

python中的函数定义方法：

```def test(x):
"""the function definitions"""
x+=1
return x

def：定义函数的关键字
test：函数名
()：内可定义形参
""" """：文档描述(非必要，但是强烈建议为你的函数添加描述信息)
x+=1：泛指代码块或程序处理逻辑
return：定义返回值```

python中过程返回值为None

```# 函数
def func1():
"""testing"""
print('in the func1')
return 0

# 过程
def func2():
"""testing2"""
print('in the func2')

x = func1()
y = func2()
print('from func1 return is %s' % x)
print('from func2 return is %s' % y)

in the func1
in the func2
from func1 return is 0
from func2 return is None```

• 减少重复代码
• 使程序变的可扩展
• 使程序变得易维护
```import time

def logger():
time_format = '%Y-%m-%d %X'
time_current = time.strftime(time_format)
with open('a.txt','a+') as f:
f.write('%s end action\n' % time_current)

def test1():
print('in the test1')
logger()

def test2():
print('in the test2')
logger()

def test3():
print('in the test3')
logger()

test1()
test2()
test3()```

return返回值

```def test():
print('in the test1')
return 0

x=test()
print(x)```

```def test1():
print('in the test1')

def test2():
print('in the test2')
return 0

def test3():
print('in the test3')
return 1,'hello',['alex','wupeiqi'],{'name':'alex'}

x=test1()
y=test2()
z=test3()
print(x)
print(y)
print(z)

None
0
(1, 'hello', ['alex', 'wupeiqi'], {'name': 'alex'})```

• 返回值数=0，返回None
• 返回值数=1，返回object
• 返回值数>1，返回tuple

1.位置参数

```# x,y为形参，1,2为实参
def test(x,y):
print(x)
print(y)

test(1,2)```

2.关键字调用

```def test(x,y):
print(x)
print(y)

test(y=2,x=3)
test(3,y=2)
test(3,x=2) 报错
test(x=2,3) 报错```

3.默认参数

```def test(x,y=2):
print(x)
print(y)

test(1)
test(1,3)
test(1,y=1)```

4.可变长参数

```def test(*args):
print(args)

test(1,2,3,4,5,5)
test(*[1,2,3,4,5,5])    # args=tuple([1,2,3,4,5,5])

def test1(x,*args):
print(x)
print(args)

test1(1,2,4,5,6,7)

def test2(**kwargs):
print(kwargs)

test2(name='alex',age=8,sex='F')
test2(**{'name':'alex','age':8})

def test3(name,**kwargs):
print(name)
print(kwargs)

test3('alex',age=18,sex='m')

def test4(name,age=18,**kwargs):
print(name)
print(age)
print(kwargs)

test4('alex',sex='m',hobby='tesla',age=3)

def test5(name,age=18,*args,**kwargs):
print(name)
print(age)
print(args)
print(kwargs)

test5('alex','a',age=34,sex=18,hobby='tesla')```

5 全局变量与局部变量

```def change_name(name):
print("before change",name)
name = "Alex li"
age = 23
print("after change", name)

name = "alex"
change_name(name)
print("age", age)

before change alex
after change Alex li
Traceback (most recent call last):
File "/Users/erick/PycharmProjects/oldboy_python/day2/局部变量.py", line 14, in <module>
print("age", age)
NameError: name 'age' is not defined```

```school = "Oldboy edu"

def change_name(name):
school = "Mage Linux"
print("before change",name,school)
name = "Alex li"
print("after change", name)

name = "alex"
change_name(name)
print(school)

before change alex Mage Linux
after change Alex li
Oldboy edu```

```school = "Oldboy edu"

def change_name(name):
global school
school = "Mage Linux"
print("before change",name,school)
name = "Alex li"
print("after change", name)

name = "alex"
change_name(name)
print("school",school)

before change alex Mage Linux
after change Alex li
school Mage Linux```

0 条评论

• ### python一些常用小技巧

这个方法可以将布尔型的值去掉，例如（False，None，0，“”），它使用 filter() 函数。

• ### 如何在Windows上使用Python，看看微软的官方教程

随着Python持续火热状态，微软开始重视Python在Windows系统上的运行，于是它出品了一个官方教程。这个教程包含了入门，web开发，自动化和脚本，数据...

• ### 使用Python『秒开』100GB+数据！

第二种使用分布式计算：虽然在某些情况下这是一种有效的方法，但是它带来了管理和维护集群的巨大开销。想象一下，必须为一个刚好超出RAM范围的数据集设置一个集群，比如...

• ### 当Excel不够用的时候如何用Python救场？

Excel是很多公司非常流行的工具，数据分析师和数据科学家经常发现他们把它作为数据分析和可视化工具的一部分，但这并不总是最好的选择。

• ### Python之前准备工作，轻松安装好这些软件就可以开始入门了

Python作为一个目前最火的编程语言之一，已经渗透到了各行各业，笔者所在的人工智能科研领域就以Python为主要编程语言，尤其是神经网络几乎已经离不开Pyth...

• ### 常用Hive函数的学习和总结

今天来小结一下工作中经常会使用到的一些Hive函数。关于Hive函数的总结，网上早有十分全面的版本。参考：https://blog.csdn.net/dovey...

• ### Python进阶之Matplotlib入门(九)

Matplotlib是Python的画图领域使用最广泛的绘图库，它能让使用者很轻松地将数据图形化以及利用它可以画出许多高质量的图像，是用Python画图的必备技...

• ### 许嵩粉丝地域分布热力图，看看你的爱豆受到哪些地方的人喜欢

这次我们想绘制一个歌手许嵩的粉丝地域分布的热力图。我们通过对网易云音乐网站爬虫，获取了所有粉丝的信息，包括所在地区。然后将这些地区按照关注人数通过热力图的形式，...

• ### PHP 到底是不是宇宙第一？TIOBE 排行榜来证明！

做为一名程序员，都比较关注其使用编程语言的热度，一方面编程语言的热度决定了它拥有多大的市场，另一方面也关系到行业内程序员选择机会有多大。