微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python学习笔记01

if <条件>:

    语句

elif<条件>:

   语句

else:

 语句

for   a in b:

   语句

else:

  语句

berak continue pass break

def  函数名(参数):

  语句 \

return  a

a<b and b>c

数值计算库:numpy+scipy(Fortran数值计算库)=matlab

符号计算库:sympy

绘图及可视化: matplotlib

a[起始:终点:步长]

import numpy as np
persontype = np.dtype({
'names':['name', 'age', 'weight'],
'formats':['S32','i', 'f']})
a = np.array([("Zhang",32,75.5),("Wang",24,65.2)],
dtype=persontype)
+ - * / //整除 ** power % 求余
def triangle_wave(x, c, c0, hc):
x = x - int(x) # 三角波的周期为1,因此只取x坐标的小数部分进行计算
if x >= c: r = 0.0
elif x < c0: r = x / c0 * hc
else: r = (c-x) / (c-c0) * hc
return r
x = np.linspace(0, 2, 1000)
y = np.array([triangle_wave(t, 0.6, 0.4, 1.0) for t in x])
len(a)
a.shape
>>> a = np.arange(12).reshape(2,3,2)
>>> b = np.arange(12,24).reshape(2,2,3)
>>> c = np.dot(a,b)

>>> np.save("a.npy", a)
>>> c = np.load( "a.npy" )
>>> c
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

from scipy.optimize import fsolve
2 from math import sin,cos
3
4 def f(x):
5 x0 = float(x[0])
6 x1 = float(x[1])
7 x2 = float(x[2])
8 return [
9 5*x1+3,
10 4*x0*x0 - 2*sin(x1*x2),
11 x1*x2 - 1.5
12 ]

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_axes([0.15, 0.1, 0.7, 0.3])

>>> fig = plt.figure()
>>> fig.show()
>>> fig.patch.set_color("g")
>>> fig.canvas.draw()

result = fsolve(f, [1,1,1])

print result
 print f(result)

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐