Python 核心编程

Python 核心编程

  • [1. 数据类型](#1. 数据类型)
    • [1.1 整型 int](#1.1 整型 int)
    • [1.2 浮点数 float](#1.2 浮点数 float)
    • [1.3 布尔类型 bool](#1.3 布尔类型 bool)
    • [1.4 字符串 str](#1.4 字符串 str)
    • [1.5 列表 list](#1.5 列表 list)
    • [1.6 元组 tuple](#1.6 元组 tuple)
    • [1.7 集合 set](#1.7 集合 set)
    • [1.8 字典 dict](#1.8 字典 dict)
  • [2. 逻辑结构、文件操作](#2. 逻辑结构、文件操作)
    • [2.1 分支结构和三元表达](#2.1 分支结构和三元表达)
    • [2.2 循环和遍历](#2.2 循环和遍历)
    • [2.3 目录和路径](#2.3 目录和路径)
    • [2.4 文件操作](#2.4 文件操作)
  • [3. 函数、类、异常处理](#3. 函数、类、异常处理)
    • [3.1 函数](#3.1 函数)
    • [3.2 类](#3.2 类)
    • [3.3 异常处理](#3.3 异常处理)
  • [4. 包和模块、随机数](#4. 包和模块、随机数)
    • [4.1 包和模块](#4.1 包和模块)
    • [4.2 随机数](#4.2 随机数)
  • [5. 生成器、高阶函数](#5. 生成器、高阶函数)

1. 数据类型

1.1 整型 int

  • 没有长度限制
python 复制代码
score = 95
python 复制代码
type(score)
int
python 复制代码
num = 888888888888888888888888888888888888888888888888888888888888888888888888888888
python 复制代码
type(num)
int
python 复制代码
num
888888888888888888888888888888888888888888888888888888888888888888888888888888
python 复制代码
a = 4
b = 3
python 复制代码
a + b
7
python 复制代码
a - b
1
python 复制代码
a * b
12
python 复制代码
a / b
1.3333333333333333
python 复制代码
a ** b
64
python 复制代码
a ** (1 / b)
1.5874010519681994
python 复制代码
import math
python 复制代码
math.exp(3)
20.085536923187668
python 复制代码
math.log(a)
1.3862943611198906
python 复制代码
str(a)
'4'
python 复制代码
c = int('5')
python 复制代码
c
5
python 复制代码
a > b
True
python 复制代码
b > a
False
python 复制代码
 a == b
False
python 复制代码
a >= b
True
python 复制代码
a <= b
False
python 复制代码
a != b
True

1.2 浮点数 float

  • 浮点数是不精准存储
python 复制代码
score = 82.11
python 复制代码
type(score)
float
python 复制代码
price = 5.55555555555555555555555555555555555555555555555555555555555
python 复制代码
price
5.555555555555555
python 复制代码
type(price)
float
python 复制代码
distance = 1.5e-4
python 复制代码
distance
0.00015
python 复制代码
a = 1.5
b = 2.7
python 复制代码
a + b
4.2
python 复制代码
a - b
-1.2000000000000002
python 复制代码
a * b
4.050000000000001
python 复制代码
a / b
0.5555555555555555
python 复制代码
import math
python 复制代码
a = 3.5
python 复制代码
a ** 2
12.25
python 复制代码
a ** (1/2)
1.8708286933869707
python 复制代码
a = 5.6
python 复制代码
math.exp(a)
270.42640742615254
python 复制代码
math.log(a)
1.7227665977411035
python 复制代码
math.log2(a)
2.4854268271702415
python 复制代码
str(a)
'5.6'
python 复制代码
c = float('2.3132')
python 复制代码
c
2.3132
python 复制代码
a = 3.23452
python 复制代码
math.ceil(a)
4
python 复制代码
math.floor(a)
3
python 复制代码
int(a)
3
python 复制代码
round(a,2)
3.23
python 复制代码
a = 1-0.55
python 复制代码
a
0.44999999999999996
python 复制代码
a = 3.5
b = 6.43
python 复制代码
a < b
True
python 复制代码
a = 1 - 0.55
python 复制代码
a == 0.45
False
python 复制代码
# 浮点数的相等不能直接比较
math.fabs(a - 0.45) < 1e-6
True

1.3 布尔类型 bool

  • True 就是1 , False 就是0
python 复制代码
True
True
python 复制代码
False
False
python 复制代码
result = True
python 复制代码
3>2
True
python 复制代码
1==2
False
python 复制代码
True+1
2
python 复制代码
2 ** True
2
python 复制代码
1 / False
---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

Cell In[85], line 1
----> 1 1 / False


ZeroDivisionError: division by zero
python 复制代码
True and True
True
python 复制代码
True and False
False
python 复制代码
True or False
True
python 复制代码
not False
True
python 复制代码
not True
False

1.4 字符串 str

  • 单引号/双引号/三单引号/三双引号
python 复制代码
s1 = '人工智能'
s2 = "人工智能"
python 复制代码
type(s1),type(s2)
(str, str)
python 复制代码
s3='''这是一个多行字符串
我可以自由换行
'''
python 复制代码
s3
'这是一个多行字符串\n我可以自由换行\n'
python 复制代码
s4="""三双引号
一样可以灵活换行"""
python 复制代码
s4
'三双引号\n一样可以灵活换行'
python 复制代码
len(s4)
13
python 复制代码
s4.__len__()
13
python 复制代码
s="qwertyuiopezxcfvgbnhmpdsfkpisjfoid"
python 复制代码
s[0]
'q'
python 复制代码
s[10]
'e'
python 复制代码
s[-1]
'd'
python 复制代码
s[-2]
'i'
python 复制代码
s[:3]
'qwe'
python 复制代码
s[3:5]
'rt'
python 复制代码
s[1:-1]
'wertyuiopezxcfvgbnhmpdsfkpisjfoi'
python 复制代码
len(s)
34
python 复制代码
#[start:stop:step]
s[1:-4:3]
'wtiecghdks'
python 复制代码
for ele in s[5:-6:4]:
    print(ele)
y
p
c
b
p
k
python 复制代码
s = "            erwreqw"
python 复制代码
s.strip()
'erwreqw'
python 复制代码
s = "   \t\n AI \t \n"
python 复制代码
s
'   \t\n AI \t \n'
python 复制代码
s.strip()
'AI'
python 复制代码
s = "人工  \t智能"
python 复制代码
s.strip()
'人工  \t智能'
python 复制代码
s.replace("\t","").replace(" ","")
'人工智能'
python 复制代码
s = "1,2,3,4,5,6  "
python 复制代码
ls = s.strip().split(",")
python 复制代码
ls
['1', '2', '3', '4', '5', '6']
python 复制代码
s = ",".join(ls)
python 复制代码
s
'1,2,3,4,5,6'
python 复制代码
s = "Hello World"
python 复制代码
s.lower()
'hello world'
python 复制代码
s.upper()
'HELLO WORLD'
python 复制代码
s1 = "AI "
s2 = "人工智能 "
python 复制代码
s1+s2
'AI 人工智能 '
python 复制代码
s = 3*s1+ 5* s2
python 复制代码
s
'AI AI AI 人工智能 人工智能 人工智能 人工智能 人工智能 '
python 复制代码
s.count("AI")
3
python 复制代码
'人工' in s
True
python 复制代码
'智能' not in s
False

1.5 列表 list

  • 元素有顺序,元素可以是任意类型,元素可以重复
python 复制代码
ls = [1,2,3,4]
python 复制代码
type(ls)
list
python 复制代码
ls = list("abc")
python 复制代码
ls
['a', 'b', 'c']
python 复制代码
[1,2] == [2,1]
False
python 复制代码
ls = [1,True,"asd",[1,2,3,4]]
python 复制代码
ls
[1, True, 'asd', [1, 2, 3, 4]]
python 复制代码
len(ls)
4
python 复制代码
ls.__len__()
4
python 复制代码
ls[0]
1
python 复制代码
ls[:2]
[1, True]
python 复制代码
ls[::2]
[1, 'asd']
python 复制代码
ls[::-1]
[[1, 2, 3, 4], 'asd', True, 1]
python 复制代码
for ele in ls:
    print(ele)
1
True
asd
[1, 2, 3, 4]
python 复制代码
ls=[]
python 复制代码
len(ls)
0
python 复制代码
ls.insert(0,"test")
python 复制代码
ls
['test']
python 复制代码
ls.insert(0,"1")
python 复制代码
ls
['1', 'test']
python 复制代码
ls.insert(100,"2")
python 复制代码
ls
['1', 'test', '2']
python 复制代码
ls.insert(-100,123)
python 复制代码
ls
[123, '1', 'test', '2']
python 复制代码
ls.append([1,2,3])
python 复制代码
ls
[123, '1', 'test', '2', [1, 2, 3]]
python 复制代码
ls.extend("mech")
python 复制代码
ls
[123, '1', 'test', '2', [1, 2, 3], 'm', 'e', 'c', 'h']
python 复制代码
ls.remove("1")
python 复制代码
ls.remove([1,2,3])
python 复制代码
ls
[123, 'test', '2', 'm', 'e', 'c', 'h']
python 复制代码
ls.remove(321)
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

Cell In[217], line 1
----> 1 ls.remove(321)


ValueError: list.remove(x): x not in list
python 复制代码
ls.pop()
'h'
python 复制代码
ls
[123, 'test', '2', 'm', 'e', 'c']
python 复制代码
ls.pop(10)
---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

Cell In[220], line 1
----> 1 ls.pop(10)


IndexError: pop index out of range
python 复制代码
ls.pop(-3)
'm'
python 复制代码
ls
[123, 'test', '2', 'e', 'c']
python 复制代码
ls[0] = 321
python 复制代码
ls
[321, 'test', '2', 'e', 'c']
python 复制代码
ls[-3:] = [1.1,2.2]
python 复制代码
ls
[321, 'test', 1.1, 2.2]
python 复制代码
ls.count(1)
0
python 复制代码
321 in ls
True

1.6 元组 tuple

  • 元素有顺序,可以是任意类型,不可修改
python 复制代码
t1 = ()
python 复制代码
type(t1)
tuple
python 复制代码
t1
()
python 复制代码
t2 = ("qwe")
python 复制代码
t2
'qwe'
python 复制代码
type(t2)
str
python 复制代码
t3 = ("12",)
python 复制代码
t3
('12',)
python 复制代码
type(t3)
tuple
python 复制代码
t4=(1,2,True,"ADSAS")
python 复制代码
t4
(1, 2, True, 'ADSAS')
python 复制代码
t5 = tuple("abd")
python 复制代码
t5
('a', 'b', 'd')
python 复制代码
t6=tuple([1,2,3,4,5,6])
python 复制代码
t6
(1, 2, 3, 4, 5, 6)
python 复制代码
# 元素不能修改
t6[3]=0
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[248], line 2
      1 # 元素不能修改
----> 2 t6[3]=0


TypeError: 'tuple' object does not support item assignment
python 复制代码
t1 = tuple("abc")
python 复制代码
t1
('a', 'b', 'c')
python 复制代码
len(t1)
3
python 复制代码
t1.__len__()
3
python 复制代码
t1[0]
'a'
python 复制代码
t1[-1]
'c'
python 复制代码
t1[1::-1]
('b', 'a')
python 复制代码
# 省略括号和结构赋值
t1 = (1,4.3)
python 复制代码
t2 = 1,4.3
python 复制代码
t1==t2
True
python 复制代码
p1,p2 = t2
python 复制代码
p1,p2
(1, 4.3)
python 复制代码
p1
1
python 复制代码
p2
4.3
python 复制代码
p1,p2,p3 = t2
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

Cell In[265], line 1
----> 1 p1,p2,p3 = t2


ValueError: not enough values to unpack (expected 3, got 2)
python 复制代码
# 快速交换多个变量的值
A = 4
B = 5
python 复制代码
A,B=B,A
python 复制代码
A
5
python 复制代码
B
4
python 复制代码
ls = (1,2,3,True,1,3,4,2,12)
python 复制代码
ls.count(1)
3
python 复制代码
ls.count(True)
3
python 复制代码
True in ls
True
python 复制代码
False in ls
False
python 复制代码
# tuple 元素不能修改吗?
t2 = (1,2,3)
python 复制代码
t2[1]=3
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[279], line 1
----> 1 t2[1]=3


TypeError: 'tuple' object does not support item assignment
python 复制代码
t3=(1,2,[4,5,6])
python 复制代码
# 列表是可变元素,值变了,但地址没变
t3[-1].append("ABD")
python 复制代码
t3
(1, 2, [4, 5, 6, 'ABD'])

1.7 集合 set

  • 元素的无序性/确定性/唯一性
python 复制代码
s1 = {}
python 复制代码
s1
{}
python 复制代码
type(s1)
dict
python 复制代码
t2 = set()
python 复制代码
t2
set()
python 复制代码
type(t2)
set
python 复制代码
t3={1}
python 复制代码
t3
{1}
python 复制代码
t4={1,2,3,4,5,6,7}
python 复制代码
t4
{1, 2, 3, 4, 5, 6, 7}
python 复制代码
t5=set("abscs")
python 复制代码
t5
{'a', 'b', 'c', 's'}
python 复制代码
t6=set([1,2,3,4])
python 复制代码
t6
{1, 2, 3, 4}
python 复制代码
t7 = {1,1,1,1,2,3,4}
python 复制代码
t7
{1, 2, 3, 4}
python 复制代码
# 列表是可变的,不能作为集合的元素
t8 = {[1,2,3],True,"absd"}
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[301], line 2
      1 # 列表是可变的,不能作为集合的元素
----> 2 t8 = {[1,2,3],True,"absd"}


TypeError: unhashable type: 'list'
python 复制代码
s = set("asdfdwsf")
python 复制代码
len(s)
5
python 复制代码
s.__len__()
5
python 复制代码
s.add(True)
python 复制代码
s
{True, 'a', 'd', 'f', 's', 'w'}
python 复制代码
s.add('s')
python 复制代码
s
{True, 'a', 'd', 'f', 's', 'w'}
python 复制代码
s.remove('a')
python 复制代码
s
{True, 'd', 'f', 's', 'w'}
python 复制代码
s.remove('ffff')
---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

Cell In[313], line 1
----> 1 s.remove('ffff')


KeyError: 'ffff'
python 复制代码
# 随机删除
s.pop()
'd'
python 复制代码
s
{'s', 'w'}
python 复制代码
a = {1,2,3,4,5,6,7}
b = {4,5,6,7,8,9}
python 复制代码
a.intersection(b)
{4, 5, 6, 7}
python 复制代码
b.intersection(a)
{4, 5, 6, 7}
python 复制代码
a.union(b)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
python 复制代码
b.union(a)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
python 复制代码
1 in a
True
python 复制代码
10 in b
False

1.8 字典 dict

  • 元素成对出现,元素五顺序,key不可变,不重复,value无要求
python 复制代码
d1 = {}
python 复制代码
d1
{}
python 复制代码
type(d1)
dict
python 复制代码
len(d1)
0
python 复制代码
d2 = {"name":"Tom"}
python 复制代码
d2
{'name': 'Tom'}
python 复制代码
d3 = {'name': 'Tom','age':12}
python 复制代码
d3
{'name': 'Tom', 'age': 12}
python 复制代码
len(d3)
2
python 复制代码
# 可变类型不可以做key
# 可变类型: list/set/dict
# 不可变类型: int/float/bool/str/tuple*
d4 = {[1,2,3]:12}
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[339], line 2
      1 # 可变类型不可以做key
----> 2 d4 = {[1,2,3]:12}


TypeError: unhashable type: 'list'
python 复制代码
d5 = dict(a=1,b=2,c=3)
python 复制代码
d5
{'a': 1, 'b': 2, 'c': 3}
python 复制代码
len(d5)
3
python 复制代码
d5.__len__()
3
python 复制代码
d6 = dict(name='Tom',age=22,school='北京大学')
python 复制代码
d6
{'name': 'Tom', 'age': 22, 'school': '北京大学'}
python 复制代码
d6["score"]=87
python 复制代码
d6
{'name': 'Tom', 'age': 22, 'school': '北京大学', 'score': 87}
python 复制代码
# 有则改之,无则追加
d2["score"] = 98
python 复制代码
d2
{'name': 'Tom', 'score': 98}
python 复制代码
d2["score"]
98
python 复制代码
# 读取不存在的key会报错
d["asd"]
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Cell In[364], line 2
      1 # 读取不存在的key会报错
----> 2 d["asd"]


NameError: name 'd' is not defined
python 复制代码
# 使用get读取更加安全
d6.get("abc",0)
0
python 复制代码
d6
{'name': 'Tom', 'age': 22, 'school': '北京大学', 'score': 87}
python 复制代码
d6.pop('name')
'Tom'
python 复制代码
d6
{'age': 22, 'school': '北京大学', 'score': 87}
python 复制代码
d.pop('age1')
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Cell In[371], line 1
----> 1 d.pop('age1')


NameError: name 'd' is not defined
python 复制代码
d6.keys()
dict_keys(['age', 'school', 'score'])
python 复制代码
d6.values()
dict_values([22, '北京大学', 87])
python 复制代码
d6.items()
dict_items([('age', 22), ('school', '北京大学'), ('score', 87)])
python 复制代码
# 默认遍历key
for ele in d2:
    print(ele)
name
score
python 复制代码
# 遍历key
for key in d6.keys():
    print(key)
age
school
score
python 复制代码
# 遍历value
for value in d6.values():
    print(value)
22
北京大学
87
python 复制代码
# 遍历键值对
for key,value in d6.items():
    print(key,value)
age 22
school 北京大学
score 87
python 复制代码
'age' in d6
True
python 复制代码
'age1' in d6
False

2. 逻辑结构、文件操作

2.1 分支结构和三元表达

python 复制代码
if(1<2):
    print("1<2")
1<2
python 复制代码
age = 25
if age >= 18:
    print("成年人")
else:
    print("未成年")
成年人
python 复制代码
score= 89

if score >=90:
    print("优秀")
elif score >= 75:
    print("良好")
elif score >= 60:
    print("及格")
else:
    print("不及格")
良好
python 复制代码
# 三元表达

age = 3

True if age >= 18 else False
False
python 复制代码
"成年人" if age >=18 else "未成年"
'未成年'
python 复制代码
"成年人" if age >=18 else age
3

2.2 循环和遍历

python 复制代码
num = 1
while True:
    if num < 10:
        num+=1
        print(num)
    else:
        break
2
3
4
5
6
7
8
9
10
python 复制代码
for _ in range(10):
    print("Hello,Python!!!")
Hello,Python!!!
Hello,Python!!!
Hello,Python!!!
Hello,Python!!!
Hello,Python!!!
Hello,Python!!!
Hello,Python!!!
Hello,Python!!!
Hello,Python!!!
Hello,Python!!!
python 复制代码
ls = [1,2,3,4,5,"Hello",False]
for ele in ls:
    print(ele)
1
2
3
4
5
Hello
False
python 复制代码
s = "asdfg"
for ele in s:
    print(ele)
a
s
d
f
g

2.3 目录和路径

python 复制代码
import os
python 复制代码
# 路径是否存在(文件夹或文件)
os.path.exists(path="./abc")
True
python 复制代码
# 路径拼接
os.path.join(".","abc","ddd")
'.\\abc\\ddd'
python 复制代码
# 创建目录
save_path = "./abc/saved_weight"

# 如果不存在就创建
if not os.path.exists(path=save_path):
    os.makedirs(save_path)
python 复制代码
# 删除文件
if os.path.exists(path="ddd.ddd"):
    os.remove()
python 复制代码
# 删除目录
save_path = "./abc/saves_weights"
if os.path.exists(path=save_path):
    print("存在")
    os.removedirs(name=save_path)
python 复制代码
# 遍历一个目录
root = "./"
for ele in os.listdir(root):
    if os.path.isfile(ele):
        print(ele,"文件")
    elif os.path.isdir(ele):
        print(ele,"目录")
.ipynb_checkpoints 目录
12.csv 文件
1234.txt 文件
abc 目录
P1_数据类型.ipynb 文件
P2_逻辑结构及文件操作.ipynb 文件
poem.txt 文件
saved_weight 目录

2.4 文件操作

python 复制代码
# 打开、增删改查、关闭
python 复制代码
f = open(file="./1234.txt",mode="r",encoding="utf8")
python 复制代码
f.read()
'锄禾日当午\n汗滴禾下土'
python 复制代码
f.read()
''
python 复制代码
f.close()
python 复制代码
# 更加优雅的写法
with open(file="./1234.txt",mode="r",encoding="utf8") as f:
    while True:
        line = f.readline().strip()
        if line:
            print(line)
        else:
            break
锄禾日当午
汗滴禾下土
python 复制代码
# 写入:如果文件不存在则新建,如果存在则清空文件内容
with open(file="poem.txt",mode="w",encoding="utf8") as f:
        f.write("举头望明月\n低头思故乡\n")
python 复制代码
# 追加内容
with open(file="poem.txt",mode="a",encoding="utf8") as f:
        f.write("举头望明月\n低头思故乡\n")

3. 函数、类、异常处理

3.1 函数

  • 位置参数
  • 默认参数
  • 可变参数
  • 匿名函数
python 复制代码
# 定义参数

def fun():
    pass
python 复制代码
# 调用
fun()
python 复制代码
callable(fun)
True
python 复制代码
# 位置参数

def add(a,b):
    return a+b
python 复制代码
add(1,2)
3
python 复制代码
# 默认参数

def area(r,pi=3.1415926):
    return r*r*pi
python 复制代码
area(r=3,pi=3.14)
28.26
python 复制代码
# 可变参数

def add_num(*args):
    s = 0
    for ele in args:
        s += ele
    return s
python 复制代码
add_num()
0
python 复制代码
add_num(1)
1
python 复制代码
add_num(1,2,3,4,5)
15
python 复制代码
# 匿名函数
fn = lambda x: x ** 2
python 复制代码
fn(3)
9
python 复制代码
(lambda x,y:x+y)(1,2)
3

3.2 类

  • 封装
  • 继承
  • 多态
python 复制代码
class Car(object):
    def __init__(self,color="Black",brand="BYD",price=50):
        '''
            自定属性
        '''
        self.color = color
        self.brand = brand
        self.price = price
    def info(self):
        '''
            自定义方法
        '''
        print(self.color,self.brand,self.price)
    def __repr__(self):
        '''
            重载父类的方法
        '''
        return 'Car'
python 复制代码
car1 = Car(color="Blue")
python 复制代码
car1
Car
python 复制代码
print(car1.color)
Blue
python 复制代码
car1.info()
Blue BYD 50
python 复制代码
# 继承
car1.__repr__()
'Car'
python 复制代码
o = object()
python 复制代码
o.__repr__()
'<object object at 0x0000020EBEBE4ED0>'
python 复制代码
# 多态
car1.__repr__()
'Car'

3.3 异常处理

  • 异常基类 Exception
  • 接收异常 try except else finally
  • 抛出异常 raise
python 复制代码
def divide(a, b):
    return a / b
python 复制代码
divide(1,2)
0.5
python 复制代码
divide(2,0)
---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

Cell In[13], line 1
----> 1 divide(2,0)


Cell In[11], line 2, in divide(a, b)
      1 def divide(a, b):
----> 2     return a / b


ZeroDivisionError: division by zero
python 复制代码
"""
    异常处理
        - 守株待兔的操作
        - 如果没有发生异常:不做任何额外处理
        - 如果发生了异常,引导程序做出合理化的处理
"""
a = 3
b = 0
try:
    result = divide(a,b)
    print(result)
except Exception as e:
    print(e)
else:
    print("没有发生错误")
finally:
    print("不管是否发生错误,我都会执行")

print("这里依然可以执行")
division by zero
不管是否发生错误,我都会执行
这里依然可以执行
python 复制代码
# 抛出异常

a = 2
b = 3

def divide(a, b):
    if isinstance(a, int) and isinstance(b, int) and b != 0:
        return a / b
    else:
        raise Exception("参数错误")
python 复制代码
divide(2,1)
2.0
python 复制代码
divide(1,0.3)
---------------------------------------------------------------------------

Exception                                 Traceback (most recent call last)

Cell In[23], line 1
----> 1 divide(1,0.3)


Cell In[21], line 10, in divide(a, b)
      8     return a / b
      9 else:
---> 10     raise Exception("参数错误")


Exception: 参数错误

4. 包和模块、随机数

4.1 包和模块

  • 包和模块是代码组织的一种方式,包就是一个文件夹,模块就是一个源码文件
  • 避免重复造轮子,利用前人写好的包和模块
  • 托管平台:pip 和 conda 管理工具
python 复制代码
import numpy as np
python 复制代码
np.__version__
'1.23.5'
python 复制代码
np.e
2.718281828459045
python 复制代码
import os
python 复制代码
os.path.exists("./")
True
python 复制代码
from matplotlib import pyplot as plt
  • 定义自己的模块
  • 在项目文件夹中新建一个文件夹名为 utils ,在里面新建一个 math.py文件,然后编辑函数
python 复制代码
from utils import math
python 复制代码
math.add(3,23)
26
python 复制代码
math.sub(43,1)
42

4.2 随机数

- 概率论中随机试验产生的结果
- 数据科学中随机数很重要
python 复制代码
import random
python 复制代码
# 均匀分布
# 按照均匀分布生成一个随机数
random.uniform(0,100)
4.646385375119754
python 复制代码
random.randint(0,100)
67
python 复制代码
# 高斯分布
random.gauss(mu=0,sigma=1)
-2.9510713969400872
python 复制代码
# 洗牌操作
x = [1,2,3,4,5,6,7,8,9,0]
print(x)
random.shuffle(x)
print(x)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[0, 6, 1, 4, 5, 7, 3, 9, 8, 2]
python 复制代码
# 随机抽取
x = [1,2,3,4,5,6,7,8,9,0]
result = random.choice(x)
print(result)
9
python 复制代码
ls = random.sample(x,3)
print(ls)
[5, 9, 3]
python 复制代码
# 固定随机数,方便复现:种子固定了,随机规则就产生了,
random.seed(0)
x = [1,2,3,4,5,6,7,8,9,0]
result = random.sample(x,2)
print(result)
[7, 0]

5. 生成器、高阶函数

生成器

- 当数据集很大时,我们很难一次性将所有数据加载到内存中,而是按需加载,这时候就要用到生成器
- 模型训练时,数据经常会打包成生成器
- yield 和 return
- 列表生成器
- 打包数据集
python 复制代码
# return 返回并跳出函数
def get_data():
    ls = [0,1,2,3,4,5,6,7,8,9]
    for ele in ls:
        return ele
python 复制代码
get_data()
0
python 复制代码
# yield 构建生成器
def get_data():
    ls = [0,1,2,3,4,5,6,7,8,9]
    for ele in ls:
        yield ele
python 复制代码
gen = get_data()
python 复制代码
for ele in gen:
    print(ele)
0
1
2
3
4
5
6
7
8
9
python 复制代码
ls = list(range(10))
python 复制代码
ls
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
python 复制代码
# 列表解析式 : for循环 + if判断
ls1 = [ele for ele in ls if ele % 2 == 1]
python 复制代码
ls1
[1, 3, 5, 7, 9]
python 复制代码
# 这样也可以构建生成器
gen1 = (ele for ele in ls if ele % 2 == 1)
python 复制代码
for ele in gen1:
    print (ele)
1
3
5
7
9
python 复制代码
# 通过生成器来读取数据集

def get_dataset():
    with open(file="dataset.csv",mode="r",encoding="utf8") as f:
        line = f.readline()
        while True:
            line = f.readline()
            if line:
                yield line
            else:
                break
python 复制代码
gen = get_dataset()
python 复制代码
# 生成器是一次性的,从头读到尾之后,再想重新读就又要新的生成器了
next(gen)
'1.70,70,23,89\n'
python 复制代码
for ele in gen:
    print(ele)
1.67,71,23,79

1.75,72,22,84

1.74,73,23,86

1.79,74,21,56

高阶函数

- 把函数当做参数,自动化的实现底层遍历
- 数据科学中高阶函数很有用,可以极大提升效率
- map 映射
- reduce 聚合
- filter 过滤
- sorted 排序
  • map
python 复制代码
ls = [0,1,2,3,4,5,6,7,8,9]

def add(ele):
    return ele + 0.5
python 复制代码
ls1 = list(map(add,ls))
python 复制代码
ls1
[0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]
  • reduce
python 复制代码
from functools import reduce
python 复制代码
sum(ls)
45
python 复制代码
def add(a,b):
    return a+b
python 复制代码
reduce(add,ls)
45
python 复制代码
reduce(lambda x, y: x * y, ls)
0
  • filter
python 复制代码
list(filter(lambda x : True if x % 2 == 0 else False, ls))
[0, 2, 4, 6, 8]
  • sorted
python 复制代码
sorted(ls, reverse = True)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
python 复制代码
ls = [0,1,2,3,4,5,6,7,8,9]
python 复制代码
ls.sort(reverse = True)
python 复制代码
ls
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
python 复制代码
ls2 = [(2,4),(1,6),(5,1),(3,8)]
python 复制代码
sorted(ls2, key=lambda x:x[0], reverse=True)
[(5, 1), (3, 8), (2, 4), (1, 6)]
python 复制代码
sorted(ls2, key=lambda x:x[1],reverse=False)
[(5, 1), (2, 4), (1, 6), (3, 8)]
相关推荐
杨半仙儿还未成仙儿几秒前
java注解
java·开发语言
编程零零七6 分钟前
python数据分析知识点大全
开发语言·python·python基础·python教程·python数据分析·数据分析知识点大全·python数据分析知识点
我是Superman丶7 分钟前
【工具】Java Excel转图片
java·python·excel
wxin_VXbishe11 分钟前
springboot瑜伽课约课小程序-计算机毕业设计源码87936
java·c++·spring boot·python·spring·servlet·php
A 八方15 分钟前
Python JSON
开发语言·python·json
超雄代码狂29 分钟前
JavaScript web API完结篇---多案例
开发语言·前端·javascript
1.01^100033 分钟前
[000-01-008].第05节:OpenFeign特性-重试机制
java·开发语言
天飓40 分钟前
树莓派智能语音助手实现音乐播放
人工智能·python·语音识别·树莓派·rasa·sounddevice
Adolf_199344 分钟前
Flask-Migrate的使用
后端·python·flask
suiyi_freely1 小时前
c++二叉搜索树
开发语言·c++