更新时间:2023-07-18
参考:
菜鸟教程
python环境
输出当前版本
python --version
下载包的位置
F:\python\Lib\site-packages
环境变量
F:\python
F:\python\Scripts
pip查询安装升级
# 查看已安装库
pip list
# 安装库 推荐用其他域名下载,比较快
pip install 库名 -i https://pypi.douban.com/simple/ --user
# 卸载库
pip uninstall 库名
# 更新pip
pip install -U pip
生成批量已安装的包txt:
pip freeze > requirements.txt
安装卸载包
pip install -r requirements.txt
# 或
pip uninstall -r requirements.txt -y
常用库:
打包库:
pyinstaller
pyqt5:
PyQt5==5.10.1
pyqt5-tools
绘图库:
matplotlib
字符输出带参
sum = 123456
#输出带参数的内容的方法
print('1加到100为{}'.format(sum))#用format方式输出
print('1加到100为%d'%sum)#用占位输出
print('1加到100为'+str(sum))#用字符串拼接形式输出
print('1加到100为',sum)#挨个输出各个参数的内容
# 输出
# 1加到100为123456
# 1加到100为123456
# 1加到100为123456
# 1加到100为 123456
字符前面添加字符
参考:Python中字符串前“b”,“r”,“u”,“f”的作用
1、字符串前加 b
b" "前缀表示:后面字符串是bytes 类型。
bytes和str互转
str.encode('utf-8')
bytes.decode('utf-8')
2、字符串前加 r
例:r"\n\n\n\n” 表示一个普通字符串 \n\n\n\n,而不表示换行了。
语法总结
while循环
ret = True
while ret:
print()
while not ret:
print()
while True:
print()
b = 10
while b < 10:
b += 1
print()
没啥好讲的,跟其他语言的while循环类似。
for循环
# for循环使用
# 遍历列表
a = [1,2,3,4,5]
# 方式一
for i in a:
print(i,end="")
# --> 12345
# 方式二
for i in range(len(a)):
print(a[i],end="")
# --> 12345
列表
# 快速生成列表 range(从1到10,每次+1)
a = [n for n in range(1,11,1)]
b = [n for n in range(10,0,-1)]
print(a)
print(b)
# --> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# --> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# 列表相加
print(a+b)
# --> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# 列表小到大排序
b.sort()
print(b)
# --> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# 列表反转
a.reverse()
print(a)
# --> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# 列表添加
a.append(99)
print(a)
# --> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 99]
# 列表删除 3种方法都可以
d = a.pop(0) #可以返回删除值
print(d)
# --> 10
del a[0]
print(a)
a.remove(11) #如果没有该元素,会报错
# --> [8, 7, 6, 5, 4, 3, 2, 1, 99]
'''
a.remove(11) #如果没有该元素,会报错
ValueError: list.remove(x): x not in list
'''
# 计算最大值,最小值,总数,平均值
a = [n for n in range(1,11,1)]
print(a)
print(max(a))
print(min(a))
print(sum(a))
print(sum(a)/len(a))
'''
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
10
1
55
5.5
'''
# 列表清空
a.clear()
简单计算
# 计算最大值,最小值,总数,平均值
a = [n for n in range(1,11,1)]
print(a)
print(max(a))
print(min(a))
print(sum(a))
print(sum(a)/len(a))
'''
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
10
1
55
5.5
'''
线程和线程池
新建一个线程
def my_thread_function(arg1: int, arg2: str):
# do something with the arguments here
print("Thread started with args:", arg1, arg2)
print(type(arg1))
print(type(arg2))
thread = threading.Thread(target=my_thread_function, args=(11, "msg"))
thread.setDaemon(True)#是否跟随主线程退出而退出
thread.start()
线程池使用:
def my_thread_function(arg1: int, arg2: str):
# do something with the arguments here
print("Thread started with args:", arg1, arg2)
print(type(arg1))
print(type(arg2))
thread_pool_1 = ThreadPoolExecutor(max_workers=1)
# 在线程池中添加任务
thread_pool_1.submit(my_thread_function, 1, "123")
# 等待1号线程池中的任务完成
thread_pool_1.shutdown()
封装函数
绘制线性图
import matplotlib.pyplot as plt
def plot_line_chart(data):
"""
绘制一组int列表的线型图
:param data: int列表
:return: None
"""
fig, ax = plt.subplots()
x = range(len(data))
ax.plot(x, data)
ax.set_xlabel('x')
ax.set_ylabel('y')
# plt.tight_layout()
plt.show()
python项目打包
Pyinstaller
-f 创建一个单文件绑定的可执行文件。
-w 不带控制台
打包方式一:
指定exe图标打包
Pyinstaller -F -i xx.ico main.py
这种方法打包会缺少相关依赖
打包方式二:
第一步:
Pyinstaller -F main.py
当前目录下生成两个文件夹(bulid、dist)和一个文件setup.spec,现在删除两个文件夹,只保留main.spec文件
第二步:
编辑main.spec文件:
添加你写的py文件,和引用的所有本地配置文件
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
# 添加所有py文件 包含项目需要的所有python脚本
py_files = [
'main.py',
]
# 添加所有资源文件 包含涉及到的所有资源文件,每个文件是2元组的形式存放
add_files = [
('E:\\anaconda3\\envs\\env37\Lib\\site-packages\\cv2\\data', 'cv2\\data'),
]
a = Analysis(
py_files,
pathex=['E:\\code\\pythonProject'], #项目路径
binaries=[],
datas=add_files,
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True, #是否启用控制台
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon='my_icon.ico', #头像路径
version="file_verison_info.txt" #生成的exe文件版本说明
)
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='main',
)
第三步:
运行命令:
pyinstaller main.spec
最后就会在dist文件夹下生成打包后的程序