pthon总结
-
- 1、python 三种基本数据类型
- 2、python 字典
- 3、python 列表
- 4、python 集合
- 5、python 字符串
- 6、python 元组
- 7、python 运算符(成员、比较、算术、赋值、身份、逻辑)
-
- 7.1.成员运算符
- 7.2.比较运算符
- 7.三、算术运算符
- 7.4.身份运算符
- 7.5.逻辑操作符
- 8、python 逻辑控制
-
- 8.1、if
- 8.2、for
- 8.3、while
- 8.4.循环跳出和冒牌排名
- 9、python 多个参数和多个返回值函数
-
- 9.1、多个参数
- 9.2.多个返回值
- 10、python 匿名函数
- 11、python 变量链赋值和作用域
-
- 11.1、链式赋值
- 11.2、作用域
- 12、python 面向对象-基本用法
- 13、python 面向对象-包装
- 14、python 面向对象-抽象
- 15、python 面向对象-特殊属性的静态方法
- 16、python 对象-继承
- 17、python 面向对象-面向对象的组合用法
- 18、python 面向对象-面向对象的组合用法
- 19、python 列表,字典推导
- 20、python 解包列表、元组、字典
- 21、python 变量类型提前说明
- 22、python 转换字典、列表、字符串类型
- 23、python 把浏览器headers直接复制使用
- 24、python @contextmanager上下文
- 25、python args和kwargs
- 26、python 异常(手动抛出、自定义)
- 27、jsonpath(精确、模糊、过滤)
- 28、定时器
1、python 三种基本数据类型
'''使用python3.超过6个版本的数据类型定义 ''' import math #数学函数 i:int=1 f:float=1.22 s:str='字符串' print(f'{
i,f,s}') '''int和float转化''' #整数转小数 print(float(i)) #四舍五入保留2位小数 print(round(3.526,2)) #向下取整 print(math.floor(3.526)) #向上取整 print(math.ceil(3.526)) #打印输出整数和小数,格式元组 print(math.modf(3.526)) 数字类型和str''' #只有字符串里面的数据类型是数字类型或者浮点型类型就可以用对应的方法转化
print(int('12'))
print(float('12.1'))
2、python 字典
# # 定义
dict1 = {
'key': 'value'}
d = dict(name='Bob', age=20, score=88)
print(dict1)
# 取值
print(dict1['key'])
# 增加
dict1['cc'] = 22
# 修改
dict1['cc'] = 33
# 删除
del dict1['cc']
# 清空字典的值
dict1.clear()
print(dict1)
# update方法 存在就修改,不存在则删除
dict1.update({
1: 1, 2: 2})
dict1.update({
1: 1.0, 3: 3})
print(dict1)
# pop方法:删除指定的键值对并返回值
print(dict1.pop(2))
print(dict1)
# 遍历键值对
dict2 = {
1: 1, 2: 2}
for k, v in dict2.items():
print(k, v)
if 'kk' not in d:
print('kk不在字典d中')
d.update({
'kk': 99})
if d.pop('kk') == 99:
print('kk被新增了,但是又被我干掉了')
3、python 列表
# 定义
list1 = [1, "2", "进阶"]
print(list1)
# 嵌套
list2 = [1, "2", "进阶", [1, 2]]
print(list2)
# 取值(细节)
list3 = ['c', 'a', 'd', 'g']
print(list3[1]) # 取索引为1的值,结果为字符串 'a'
print(list3[1:2]) # 取索引为1的值,结果为列表 ['a']
# 增删改查拼乘
list4 = ['c+'] + ['+']
print(list4)
list5 = ['2', '1', 2, 4, "dd"]
print(list5 * 3)
# 新增 末尾新增
list5.append(1)
print(list5)
# insert(index, x)在列表lst指定位置index处添加元素x,该位置后面的所有元素后移一个位置
list5.insert(1, 'xl')
# pop方法 删除并返回列表lst中下标为index(默认为-1)的元素
list5.pop()
list5.pop(0)
print(list5) # ['1']
# index()方法 返回列表lst中第一个值为x的元素的下标,若不存在值为x的元素则抛出异常
print(list5.index(2))
# count(x) 统计元素出现的次数
print(list5.count(1))
print(list5)
# reverse() 对列表lst所有元素进行逆序
list5.reverse() # 这个运行结果没有返回值
print(list5)
print(list5[::-1]) # 这个是有返回值,其实是取值
'''写个方法 校验列表是否有相同的元素'''
def chan(l):
for i in l:
if l.count(i)>1:
return i
4、python 集合
# 定义
''' 集合: 无序,没有索引 不支持数据重复 '''
set1 = {
1, 2, 3, 4, 1, "5"}
print(set1) # {1, 2, 3, 4, '5'} 重复之会出现一个
# 集合特殊操作
# 去除指定的元素
print({
1, 2, 3, 4, 5} - {
1, 2})
# 找到指定元素相同的元素
print({
1, 2, 3, 4, 5} & {
1, 2})
# 合并去重
print(({
1, 2, 3, 4, 5} | {
3, 5, 6, 7}))
# 定义一个空的集合
print(type(set())) # <class 'set'>
print(type({
})) # <class 'dict'>
5、python 字符串
# 定义
str1 = '1jaskj'
# 切片操作 结果
''' 字符串[开始索引:结束索引:步长] 切取字符串为开始索引到结束索引-1内的字符串(取头不取尾) 步长不指定时步长为1 字符串[开始索引:结束索引] '''
print(str1[0]) # 1
print(str1[-1]) # j
print(str1[1:3]) # ja
# 翻转
print(str1[::-1]) # jksaj1
a='a\a\a\a'
#原生字符串,不会被里面的转义所影响
b=r'a\a\a\a'
6、python 元组
# 定义
tuple1 = (1, "2", "进阶")
print(tuple1)
# 嵌套
tuple2 = (1, "2", "进阶", (1, 2))
print(tuple2)
# 取值(细节)
tuple3 = ('c', 'a', 'd', 'g')
print(tuple3[1]) # 取索引为1的值,结果为字符串 'a'
print(tuple3[1:2]) # 取索引为1的值,结果为列表 ('a',),会有个逗号
print(tuple3[1:3]) # 取索引为1和2的值,结果为列表 ('a', 'd')
# 定义只有一个元素的元组
print((1)) # 这是'1'
print((1,)) # 这是元组(1,)
'''神奇'''
a = {
1: 1},
print(type(a)) # <class 'tuple'> a也是元组
b = [1, 2],
print(type(b)) # <class 'tuple'> b也是元组
7、python 运算符(成员、比较、算术、赋值、身份、逻辑)
7.1、成员运算符
print(1 in[1,'2m']) #True
print(1 not in[1,'2m']) #False
#这样只能判断字典的key 是不是存在
print('c' in {
'c':1}) #True
print(1 in {
'c':1}) # False
7.2、比较运算符
# 比较运算符返回布尔值
print(1 == 1) # True
print(1 >= 1) # True
print(1 <= 1) # True
print(1 > 1) # False
print(1 < 1) # False
print(1 != 1) # False
b = 1
b += b >= 1
print(b>=1) #True
print(int(True)) #结果为1
print(b) #结果为2
7.3、算术运算符
# 加减乘除
print(1 + 1)
print(1 - 1)
print(1 * 1)
print(4 / 2) # 2.0
# 取余
print(7 % 2) # 1
# 多次方
print(2 ** 2) # 4
print(2 ** 5) # 32
7.4、身份运算符
''' 总结: 对象的三个特征 id value type 只有三个特征都相等时 使用 is 的结果才为True 而使用==时只需要value的结果就是True '''
# 基本使用
print(1 is 1) # True
print(1 is not 2) # True
print('1' is 1) # False
# ==和is区别
print(1 is 1.0) # False
print(1 == 1.0) # True
print(1 == True) # True
print(1 is True) # False
a = {
1, 2, 3}
print(id(a)) # 2257202349000
b = {
1, 3, 2}
print(id(b)) # 2257200217896
print(a == b) # True
print(a is b) # False
# 判断数据的类型
print(type(1) == int) # True
print(isinstance(1, int)) # True
print(isinstance("2", str)) # True
print(isinstance(1, (int, str, float))) # True
print(isinstance({
1: 1}, (int, str, float))) # False
7.5、逻辑运算符
print(True and True)
print(False and True)
print(False and False)
print(True or True)
print(False or True)
print(False or False)
8、python 逻辑控制
8.1、if
''' 非0和非空(null)值为true,0 或者 null为false。 '''
if 0: # if false 不满足无法打印
print('0为false')
if None: # if false 不满足无法打印
print('表达式为None')
if 1: # if true 满足打印
print('非0为true')
if '1': # if true 满足打印
print('表达式为非空')
a=3
if a==1:
print('a=1')
elif a==2:
print('a=2')
else:
print('a==0')
a=2
print(a)
8.2、for
'''几次常规用法'''
for i in range(1,4):#从1开始 1 2 3
print(i)
for i in range(4): #从0开始 0 1 2 3
print(i)
a=[1,2,3,'ll','24']
for i in a: #遍历每个列表元素
print(i)
b={
'k1':'v1','k2':'v2','k3':'v3'}
for i in b.keys(): #遍历字典键
print(i)
for i in b.values(): #遍历字典值
print(i)
for k,v in b.items(): #键值对一起遍历
print(k,v)
8.3、while
import random
a=1
while a<3:
print('进来时a={}'.format(a))
a+=1
print('出去时a={}'.format(a))
print('循环运行完的a={}'.format(a))
count=0
while a<=7:
count+=1
a+=random.randint(2,9)
else:
print('a运行了{}次就大于7了,a={}'.format(count,a))
8.4、循环跳出和冒牌排序
''' break是直接结束当前循环 continue是结束当前循环的剩余语句进行下一论的循环 continue 语句是一个删除的效果,他的存在是为了删除满足循环条件下的某些不需要的成分 '''
for i in 'py1thon':
if i == '1':
break
print(i)
print('-------------------------------------')
for i in 'py1thon':
if i == '1':
continue
print(i)
print('-------------------------------------')
# 冒泡排序 :左右比较并互换位置
a = [2, 1, 4, 6, 2, 3, 5, 9, 7]
# print(len(a)) # 9
for i in range(len(a)-1):
for j in range(len(a)-1):
if a[j] >= a[j + 1]:
#互换之术
a[j],a[j+1]=a[j+1],a[j]
9、python 函数的多个参数和多个返回值
9.1、多个参数
''' 默认参数: 它是如何工作的 名为 func 的函数有一个没有默认参数值的参数,后跟两个各自带有默认参数值的参数。 在第一次调用python函数时,func(3, 7),参数 a 获得了值 3,参数 b 获得了值 7,而 c 获得了默认参数值 10。 在第二次调用函数时,func(25, c=24),由于其所处的位置,变量 a 首先获得了值 25。然后,由于命名——即关键字参数——指定,变量 c 获得了值 24。变量 b 获得默认参数值 5。 在第三次调用函数时,func(c=50, a=100),我们全部使用关键字参数来指定值。在这里要注意到,尽管 a 在 c 之前定义,但我们还是在变量 a 之前指定了变量 c。 ''' def sum(a, b=1): return a + b print(sum(1)) def func(a, b=5, c=10): print(a, b, c) func(3, 7) func(25, c=24) func(c=50, a=100) def t(a,