Python编程技巧
1. Python:基础编译器
2. NumPy:支持数组和矩阵
3. ipython:一个净强化的交互Python Shell,对探索NumPy的特性非常方便。
4. matplotlib:将允许你绘图
5. Scipy:在NumPy的基础上提供了很多科学模块
1. Python 索引从0开始,#作为注释符,未结束的条件行以:结尾,
numpy.zeros((m, n)) 和 numpy.ones((m, n)),a**b代表乘方,.transpose或.T代表转置,
a*b代表elementwise相乘,dot(a,b)代表矩阵乘,concatenate(a,b)矩阵的列链接
2. 创建一个数组
from numpy import *
import numpy as np
a=array([[1,2,3],[4,5,6]]);a.shape
b=arange(15);print b
b.reshape(3,5)
c=zeros((4,5));print c
d=ones((4,5));print d
e=add(c,arange(20).reshape(4,5))
f=dot(e,d);print f
np.array([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9, 10]], dtype=np.float)
np.arange(0,1,0.1)
np.linspace(0, 1, 12)
np.logspace(0, 2, 20)
3. ndarray的几个常用属性:shape,ndim,size,T,dtype
ndarray的比较有用的方法:tolist(),item(*args),dump(file),dumps(),reshape(),resize(),swapaxes(),flatten()
max,min,sum,cumsum,prod,cumprod,all,any,mean
4. 创建一个矩阵
a = np.matrix([[1,2,3],[5,5,6],[7,9,9]])
demo
import numpy as np
def main():
print "hello world"
a,b,c = 1,2,3
print a
print b
print c
helptext=""" 1
2
3"""
print helptext
string="hello world"
print string[1]
#mytest = raw_input("please input a number:")
mytest = 10
mytest = int(mytest)
if mytest == 10:
print "you input number is ten."
elif mytest == 20:
print "you input number is twenty."
else:
print "another number."
a = 0
while a < 5:
a = a + 1
print a
else:
print "a's value is five"
mylist = "for statement"
for word in mylist:
print word
else:
print "End list"
a = np.array((6,6));
# compile: python demo1.py
if __name__ == "__main__":
main()
参考:http://marinzou.blogbus.com/logs/65390334.html