变量的命名规则
可以用数字、下划线、字母命名。数字不能开头 不能用关键字
常见运算符
取余 %
取模 /
除 //
判断是 is
判断不是 is not
成员属于 in
成员不属于 not in
与 and
或 or
数据类型
整型 int
浮点型 float
布尔 bool --Tuer 、 False
字符串 string
复数 complex
列表 list
元组 tuple
集合 set
字典 dictionary
自己创建的类
if、for、while
循环中常见的关键词:
break:结束循环
continue:跳过本次循环
ealse:当循环结束后才执行
字符串”“
运算:
+ 拼接
* 重复
特点:一旦创建不可变,可以索引
查找:
index()-根据元素查找下标,找到第一个停止,找不到报错
find() -根据元素查找下标,找到第一个停止,找不到返回 -1
conunt() - 查找一个元素出现的次数
len() - 返回字符串长度
截取:
[x:y] 根据所以截取字符串,从下标为x开始,下标为y-1结束
[x::-y] 根据下标倒着截取,x为开始下标,y为步长 从下标x开始截取,步为
y,例如[2::-1] 从下标2开始倒着截取,步长为1 [-2::-2] 从下标为-2开
始截取,每隔一个截取一次
替换:
replace(old,new,n) 参数1为老字符,参数2为新字符 n为替换的最大
次数
拆分:
split() 会返回一个列表
拼接:
"".join() 会将列表中的数据拼接为字符串
转换:
upper() 全部转换为大写
lower() 全部转化为小写
title() 每个单词首字母大写,其余小写,以空格区分每个单词
capitalize()将整个字符串首字母大写,其余小写
swapcase() 大写转小写,小写转大写
判断:
isupper()是否全是大写
islower()是否全是小写
istitle() 是否字符串首字母大写,其余小写
isalpha() 是否全是字母
isalnum() 是否全是数字或字母
isdigit() 是否全是数字
isspace() 是否全是空格
starswith()是否以指定的字符开头
endswith() 是否以指定的字符结尾
格式化:
format()
%
列表[]
特点:有序,有下标,可变,可重复,可存储不同过类型的数据
添加:
append() 在末尾添加一个
insert(x,y) 在x位置插入y
extend() 在末尾添加多个 参数以列表形式[]
删除:
remove() 移除指定元素
pop() 移除指定元素,并返回该值
clear() 清空列表
修改:
[] 根据索引修改
查找:
index() 返回某一个元素的下标,找不到报错
count() 查找元素出现的次数
截取:
同字符串
排序:
sort() 默认升序
反转:
reverse() 反转
复制:
copy() 浅层拷贝,嵌套的只复制一层数据
deepcopy() 深层复制,复制的是数据,嵌套的也能拷贝到
元组()
特点:有序,不可变,有下标,有重复
查找:
index()
count()
复制:
元组本身没有copy函数, = deep copy()
字典{}
特点:
键值对形式存在,键是唯一的,不可变,值可变。可通过键来索引值
添加:
[] 通过增加一个索引来产生新的键值对
setdefault() :查找键,没有的话增加新键,值默认为空
update() 一次增加多个 必须以字典形式增加
删除:
pop() 根据键删除某个元素
clear() 清空字典
修改:
[] 根据键修改值
查找:
[] 根据键查找,找不到报错
get() 根据键查找,找不到返回None
遍历:
keys() 得到所有键,返回一个列表
values() 得到所有的值,返回所有列表
iteams() 得到所有的键值对,返回一个列表
复制:
copy()
deepcopy()
集合{}
特点:
无序,无重复,可变
添加:
add()
update()
删除:
remove() 移除
pop() 删除查找到的第一个
clear() 清空
函数
函数标志: 名字()
参数:定义变量的时候为形参,在函数调用给变量赋值的是实参
返回值:
return 不加return默认None
不定长参数:
*
局部变量:
函数内部变量,只有在函数内部有效
全局变量:
要在函数内部修改全局变量,必须以 globl 引用
模块
form *** import
面向对象:
类和对象
封装
继承 super(). 或者父亲名字.
多态
访问限制
构造函数 创建对象时自动执行
类变量 和 实例变量
动态属性
装饰器
def outset(f)
def inner(*x)
print("****") 修饰内容
f(*x) 被修饰函数
return inner
@outset
set()
x = 1
y = 1
print(x + y)
文件处理
os模块
异常处理
try:
主程序
excpet 警告类型 as e
执行语句
else: 没有错误执行
finaely : 无论有没有错都会执行
正则表达式
re模块
import re
r'''
re.match 函数
参数:
原型 :match(pattern,string,flags)
pattern :匹配的正则表达式
string: 要匹配的字符
flags : 标志位, 用于控制正则表达式的匹配方式
re.I 忽略大小写
re.L 做本地化识别
re.M 多行匹配 ,影响^和$
re.S 是. 匹配包括换行符在内的任意字符
re.U 根据Unicode 字符集解析字符 ,影响\w \W \b \B
re.X 使我们更灵活的理解正则表达式
功能: 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配
成功的话返回None 就是之匹配开头
'''
print(re.match("www", "www.baidu.com"))
print(re.match("wWw", "www.baidu.com"))
print(re.match("wWw", "wwW.baidu.com", flags=re.I))
print(re.match("www", "wwbaidu.www.com"))
print("re.match只是匹配开头否则返回None")
r'''
re.search 函数
参数:
原型 :match(pattern,string,flags)
pattern :匹配的正则表达式
string: 要匹配的字符
flags : 标志位, 用于控制正则表达式的匹配方式
re.I 忽略大小写
re.L 做本地化识别
re.M 多行匹配 ,影响^和$
re.S 是. 匹配包括换行符在内的任意字符
re.U 根据Unicode 字符集解析字符 ,影响\w \W \b \B
re.X 使我们更灵活的理解正则表达式
功能: 扫描整个字符串,并返回第一个成功匹配的
'''
print(re.search("jiuzhu","jiuzhu is a good man jiuzhu is a good man"))
r'''
re.findall()
参数:
原型 :match(pattern,string,flags)
pattern :匹配的正则表达式
string: 要匹配的字符
flags : 标志位, 用于控制正则表达式的匹配方式
re.I 忽略大小写
re.L 做本地化识别
re.M 多行匹配 ,影响^和$
re.S 是. 匹配包括换行符在内的任意字符
re.U 根据Unicode 字符集解析字符 ,影响\w \W \b \B
re.X 使我们更灵活的理解正则表达式
re.A 匹配字符串的开始 ,只是匹配该个字符串的开头
功能: 扫描整个字符串,并返回结果列表
'''
print(re.findall("jiuzhu","jiuzhu is a good man jiuzhu is a good man"))
正则元字符
import re
print("********匹配单个字符*******")
r'''
. 匹配出换行符以外的任意字符
[0123456789] 字符集合,表示匹配方括号中任意的元素
[jiuzhu] 匹配j i u z h u 任意一个字符
[a-z] 匹配任意小写字母
[A-Z] 匹配任意大写字母
[0-9] 匹配任意数字
[0-9a-zA-Z] 匹配任意数字和字母
[0-9a-zA-Z—_] 匹配任意数字和字母和下划线
[^jiuzhu] 匹配除了jiuzhu这几个字符以外的所有字符
^脱字符 表示不匹配集合中的字符
[^0-9] 匹配所有的非数字类型
\d 匹配所有数字 和[0-9]一样
\D 匹配非数字字符,效果同[^0-9]
\w 匹配数字 字母和_ 效果同[0-9a-zA-Z—_]
\W 匹配非数字 字母和_ 效果同[^0-9a-zA-Z—_]
\s 匹配任意的空白符 (空格 换行 回车 换页 制表)
\S 匹配任意的非空白符 (空格 换行 回车 换页 制表)
'''
print(re.search("." , "jiuzhu is a good man"))
print(re.search("[12346]","jiuzhu is 1 3 a good man"))
print(re.findall("[^12346]","jiuzhu is 1 3 a good man"))
print("*********** 锚字符(边界字符)********")
'''
^ 行首匹配 ,和[]的不一样
$ 行尾匹配
\A 匹配整个字符串的开始 即使在re.M下也不会匹配每行
\Z 匹配整个字符串的结束 即使在re.M下也不会匹配每行
\b 匹配一个单词的边界 单词和空格之间的位置
\B 匹配 非 单词边界
'''
print(re.search("^jiuzhu" , "jiuzhu is a good man"))
print(re.search("man$" , "jiuzhu is a good man"))
print(re.findall("^jiuzhu" , "jiuzhu is a good man\njiuzhu is a good man"))
print(re.findall("\Ajiuzhu" , "jiuzhu is a good man\njiuzhu is a good man"))
print("*********** 锚字符(边界字符)********")
'''
说明下方的xyz均为假设的普通字符 不是正则元字符
( xyz )
x? 匹配0个或者1个x
x* 匹配0个或者任意多个x (.*表示一个或者任意多个字符)
'''
import re
print(re.findall(".","hello ... WORLD_\t"))
print(re.findall("[abc]","helloabc"))
print(re.findall("[A-Z]","hello...WORLD",re.I))
print(re.findall("[0-9.]","hello...WORLD123"))
print(re.findall("[^123]","hello...WORLD123"))
print(re.findall("\d","hello...WORLD123"))
print(re.findall("\D","hello...WORLD123"))
print(re.findall("\w","hello...WORLD123"))
print(re.findall("\W","hello...WORLD123"))
print(re.findall("\s","hello ... WORLD123\t\n"))
print(re.findall("\S","hello ... WORLD123\t\n"))
'''
边界字符
'''
print(re.findall("^s","shello\nsabc",re.M))
print(re.findall("s$","hellos\nabcs",re.M))
print(re.findall("\As","shello\nsabc",re.M))
print(re.findall("s\Z","hellos\nabcs",re.M))
print(re.findall("\Bsss","a hello world\ndsss",re.M))
print(re.findall("(abc)","hello adbc"))
print(re.findall("d?","dd"))
print(re.findall("d*","dd abc dddd"))
print(re.findall("\d+","hello12345a6789b 0"))
print(re.findall("\d{2}","hello12345a6789b 0"))
print(re.findall("\d{2,4}","1234567890abc123"))
print(re.findall("\|","1234567890abc123|"))
序列化 和反序列化
'''
文件操作: 可以写入对象 列表等
序列化:将数据转为二进制存储到文本文件中
反序列化:将二进制 转为 原来格式
'''
import pickle
path = r"F:\百度首页\baidu.txt"
class Penson:
def __init__(self):
print("hahhah")
p = Penson()
with open(path,"wb") as f4:
pickle.dump(p,f4)
with open(path,"rb") as f:
a = pickle.load(f)
print(a)
"""
编码格式和解码格式一致
"""
def bianma():
path = r"F:\新建文件夹\4.txt"
with open(path, "w", encoding="utf-8") as f:
str = "jiuzhu is a good man 久竺 "
f.write(str)
with open(path, "r", encoding="utf-8", errors="ignore") as f:
data = f.read()
print(data)
print(type(data))
with open(path, "wb") as f:
str = "jiuzhu is a good man 久竺 "
f.write(str.encode("utf-8"))
with open(path, "rb") as f:
data = f.read()
print(data)
print(type(data))
newdata = data.decode("utf-8")
print(newdata)
print(type(newdata))
def erjinzhi():
path = r"F:\新建文件夹\二进制编码.txt"
with open(path, "wb") as erjinzhi:
str = "如果二进制的要编码和解码 二进制写入和读取文件时不需要编码参数 但是输入输出需要编码格式"
erjinzhi.write(str.encode("utf-8"))
with open(path, "rb") as erjinzhiread:
str = erjinzhiread.read()
str1 = str.decode("utf-8")
print(str1)
print(str)
erjinzhi()
TCP UDP
time datatime os random math re