---------------------------------------------------------------------------ValueError Traceback (most recent call last)Cell In[217], line 1
----> 1 ls.remove(321)ValueError: list.remove(x): x not in list
ls.pop()
'h'
ls
[123, 'test', '2', 'm', 'e', 'c']
ls.pop(10)
---------------------------------------------------------------------------IndexError Traceback (most recent call last)Cell In[220], line 1
----> 1 ls.pop(10)IndexError: pop index out of range
ls.pop(-3)
'm'
ls
[123, 'test', '2', 'e', 'c']
ls[0]=321
ls
[321, 'test', '2', 'e', 'c']
ls[-3:]=[1.1,2.2]
ls
[321, 'test', 1.1, 2.2]
ls.count(1)
0
321in ls
True
1.6 元组 tuple
元素有顺序,可以是任意类型,不可修改
t1 =()
type(t1)
tuple
t1
()
t2 =("qwe")
t2
'qwe'
type(t2)
str
t3 =("12",)
t3
('12',)
type(t3)
tuple
t4=(1,2,True,"ADSAS")
t4
(1, 2, True, 'ADSAS')
t5 =tuple("abd")
t5
('a', 'b', 'd')
t6=tuple([1,2,3,4,5,6])
t6
(1, 2, 3, 4, 5, 6)
# 元素不能修改
t6[3]=0
---------------------------------------------------------------------------TypeError Traceback (most recent call last)Cell In[248], line 21 # 元素不能修改
----> 2 t6[3]=0TypeError: 'tuple' object does not support item assignment
t1 =tuple("abc")
t1
('a', 'b', 'c')
len(t1)
3
t1.__len__()
3
t1[0]
'a'
t1[-1]
'c'
t1[1::-1]
('b', 'a')
# 省略括号和结构赋值
t1 =(1,4.3)
t2 =1,4.3
t1==t2
True
p1,p2 = t2
p1,p2
(1, 4.3)
p1
1
p2
4.3
p1,p2,p3 = t2
---------------------------------------------------------------------------ValueError Traceback (most recent call last)Cell In[265], line 1
----> 1 p1,p2,p3 = t2ValueError: not enough values to unpack (expected 3, got 2)
# 快速交换多个变量的值
A =4
B =5
A,B=B,A
A
5
B
4
ls =(1,2,3,True,1,3,4,2,12)
ls.count(1)
3
ls.count(True)
3
Truein ls
True
Falsein ls
False
# tuple 元素不能修改吗?
t2 =(1,2,3)
t2[1]=3
---------------------------------------------------------------------------TypeError Traceback (most recent call last)Cell In[279], line 1
----> 1 t2[1]=3TypeError: 'tuple' object does not support item assignment
---------------------------------------------------------------------------NameError Traceback (most recent call last)Cell In[364], line 21 # 读取不存在的key会报错
----> 2 d["asd"]NameError: name 'd' is not defined
---------------------------------------------------------------------------NameError Traceback (most recent call last)Cell In[371], line 1
----> 1 d.pop('age1')NameError: name 'd' is not defined
---------------------------------------------------------------------------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 / bZeroDivisionError: division by zero
"""异常处理- 守株待兔的操作- 如果没有发生异常:不做任何额外处理- 如果发生了异常,引导程序做出合理化的处理
"""
a =3
b =0try:result = divide(a,b)print(result)except Exception as e:print(e)else:print("没有发生错误")finally:print("不管是否发生错误,我都会执行")print("这里依然可以执行")
division by zero
不管是否发生错误,我都会执行
这里依然可以执行
# 抛出异常a =2
b =3defdivide(a, b):ifisinstance(a,int)andisinstance(b,int)and b !=0:return a / belse:raise Exception("参数错误")
divide(2,1)
2.0
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 / b9 else:
---> 10 raise Exception("参数错误")Exception: 参数错误
4. 包和模块、随机数
4.1 包和模块
包和模块是代码组织的一种方式,包就是一个文件夹,模块就是一个源码文件
避免重复造轮子,利用前人写好的包和模块
托管平台:pip 和 conda 管理工具
import numpy as np
np.__version__
'1.23.5'
np.e
2.718281828459045
import os
os.path.exists("./")
True
from matplotlib import pyplot as plt
定义自己的模块
在项目文件夹中新建一个文件夹名为 utils ,在里面新建一个 math.py文件,然后编辑函数
from utils import math
math.add(3,23)
26
math.sub(43,1)
42
4.2 随机数
- 概率论中随机试验产生的结果
- 数据科学中随机数很重要
import random
# 均匀分布# 按照均匀分布生成一个随机数
random.uniform(0,100)
4.646385375119754
random.randint(0,100)
67
# 高斯分布
random.gauss(mu=0,sigma=1)
-2.9510713969400872
# 洗牌操作
x =[1,2,3,4,5,6,7,8,9,0]print(x)
random.shuffle(x)print(x)
Nginx七层(应用层)反向代理 SCGI代理scgi_pass篇 -
文章信息 -
Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite:http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this art…
文章目录 修改运行发现数据库数据没有添加进去 修改 int u session.update(s, new User(1,"xiaoxiao",18));运行发现数据库数据没有添加进去
运行发现数据库没有添加进去数据,原因是默认是手动添加的,需要修改。
有两种方法然后再重新运行&a…