1.python注释符
> #
2.注意事项
3.数组定义
># coding=utf-8
import numpy as np
ar=np.array([1,2,3,4,5])
3.数组求均值
> # coding=utf-8
> import numpy as np
> a = np.array([2,4,6,8])
> l = len(a)
> sum1 = 0
> for i in a:
> sum1 = sum1+i
> print("the sum of a is:"sum1)
> print("the mean of a is:" sum1/l)
4.文件路径
需要注意的是u+单引号
```python
path = u'D:\备份\文档'
5.某个txt文件读取
# coding=utf-8
filetxt = open(u'D:\备份\文档\data.txt','r')
try:
line=filetxt.read()
if line:
print(line)
finally:
filetxt.close()
6.python常用数据库
(1)numpy
#调用方式
import numpy as np
#定义数组
r=np.array([3,4,5,8])
[3 4 5 8]
#创建数值为1的数组,
r=np.ones([6,6],dtype=np.double)
[[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]]
#固定数值的数组
r=np.full([2,2],8)
[[8 8]
[8 8]]
#单位矩阵
r=np.eye(6)
[[1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 1.]]
```python
#对角矩阵
r=np.diag([1,2,3,4])
[[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]
#还可创建随机数组 np.random ,排序 np.sort();数组运算、矩阵的删减: np.delete() np.append(); 文件操作:np.loadtxt()等
(2)os 主要是对目录和文件进行操作
import os
#获取当前路径
os.getcwd()
C:\Use\h\
#修改当前路径
os.chdir('')
7.python中路径的定义
(1)有中文的路径前加u
path1=u'D:\文件'
(2)加r防止字符串转义
path2=r'D:\'
参考:“https://blog.csdn.net/qq_30990097/article/details/80882555”
https://blog.csdn.net/weixin_29468281/article/details/111903750
“https://blog.csdn.net/weixin_33850015/article/details/93184616”
https://blog.csdn.net/qq_44704609/article/details/89844772
https://blog.csdn.net/weixin_39867708/article/details/113967579