div result sub A/sub B except Exception as err: print("Ar exception happened:"+str(err) return div resul diy test divide(10.0) print("div test"div test) 1 2.封装 3继承 from datetime import datetime classAsset(object): share=0 buyTime =datetime.strptime(1900-01-01,%Y-%m-%d) sellTime=datetime.strptime(1900-01-01%Y-%m-%d) def_init_(self,id,price): self.id=id self.price price def print_id(self): print("The ID of the asset is:%s%(self.id)) def buy(self,share,time) self.share +share self.buyTime=datetime.strptime(time,%Y-%m-%d) def sell(self,share,time) self.share-=share self.sellTime=datetime.strptime(time,%Y-%m-%d) Asset1 Asset('005',22.3) Asset1.print_id() class NotshourtableAsset(Asset) def sell(self,share,time):
div_result = sub_A / sub_B except Exception as err: print("An exception happened:" + str(err)) return div_result div_test = divide(10,0) print("div_test",div_test) 类: 1.类 2.封装 3.继承 from datetime import datetime class Asset(object): share = 0 buyTime = datetime.strptime('1900-01-01','%Y-%m-%d') sellTime = datetime.strptime('1900-01-01','%Y-%m-%d') def __init__(self,id,price): self.id = id self.price = price def print_id(self): print("The ID of the asset is : %s " %(self.id)) def buy(self,share,time): self.share += share self.buyTime = datetime.strptime(time,'%Y-%m-%d') def sell(self,share,time): self.share -=share self.sellTime = datetime.strptime(time,'%Y-%m-%d') Asset1 = Asset('005',22.3) Asset1.print_id() class NotshourtableAsset(Asset): def sell(self,share,time):
if self share >=share: self.shareshare self.sellTime=datetime.strptime(time,%m% else: print("Not permitted!") print("Current share is",self.share) 程序输出结果: the asset is:005 线程 Python提供了两个模块来实现多线程thread和threading,thread由于有一些缺陷,建议大 家用threading。 def music(func): for i in range(2): sleep(1) def move(func): for i in range(2): (un,ctime( sleep(5) theads=[] t1=threading.Thread(target=music,args=(u'love',)) theads.append(t1) t2=threading.Thread(target=move,args=(u'move',) theads.append(t2) ifname=='main': fortin theads: mon(True) t.start() print("all over %s"%ctime()) 程序输出结果: Iam listen to love.Tue Dec 417:20:042018 I am at the move!Tue Dec 417:20:04201 all over Tue Dec 4 17:20:04 2018 Iam listen to love.Tue Dec 417:20:05 2018 I am at the move!Tue Dec 4 17:20:09 201
if self.share >= share: self.share -= share self.sellTime = datetime.strptime(time,'%Y-%m-%d') else: print("Not permitted!") print("Current share is ", self.share) 程序输出结果: The ID of the asset is : 005 线程 Python 提供了两个模块来实现多线程 thread 和 threading,thread 由于有一些缺陷,建议大 家用 threading。 import threading from time import ctime,sleep def music(func): for i in range(2): print("I am listen to %s. %s"%(func,ctime())) sleep(1) def move(func): for i in range(2): print("I am at the %s! %s"%(func,ctime())) sleep(5) theads = [] t1 = threading.Thread(target = music,args=(u'love',)) theads.append(t1) t2 = threading.Thread(target = move,args=(u'move',)) theads.append(t2) if __name__ == '__main__': for t in theads: t.setDaemon(True) t.start() print("all over %s"%ctime()) 程序输出结果: I am listen to love. Tue Dec 4 17:20:04 2018 I am at the move! Tue Dec 4 17:20:04 2018 all over Tue Dec 4 17:20:04 2018 I am listen to love. Tue Dec 4 17:20:05 2018 I am at the move! Tue Dec 4 17:20:09 201
第二阶段数据处理 数据可视化Matplotlib 数据导入pandas 矩阵处理numpy 静态矩阵的基本处理方法: import numpy as np a=np.array([1,2,3,4J b=np.array([5,6,7,8]) c=np.array([[1,2,3,41,4,5,6,71.[7,8,9,10j print(a.shape) print(b.shape print(c.shape c.shape =2,6 print(c) c.shape=3,-1 print(c】 c.shape=-1.3 print(c) d=c.reshape(12,1) print(d) 输出的结果: 1a1 (3,4 123445) [6778910 【12341 【4567刀 i78910 123) 【445] 【677刀 [8910川 【3 [4
第二阶段 数据处理 矩阵处理 numpy 科学计算 SCIPY 数据可视化 Matplotlib 数据导入 pandas 矩阵处理 numpy 静态矩阵的基本处理方法: import numpy as np a = np.array([1,2,3,4]) b = np.array([5,6,7,8]) c = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]]) print(a.shape) print(b.shape) print(c.shape) c.shape = 2,6 print(c) c.shape = 3,-1 print(c) c.shape = -1,3 print(c) d = c.reshape(12,1) print(d) 输出的结果: (4,) (4,) (3, 4) [[ 1 2 3 4 4 5] [ 6 7 7 8 9 10]] [[ 1 2 3 4] [ 4 5 6 7] [ 7 8 9 10]] [[ 1 2 3] [ 4 4 5] [ 6 7 7] [ 8 9 10]] [[ 1] [ 2] [ 3] [ 4]
[4 【7刀 [8 【91 1 可以通过shape这个函数,将当前的矩阵,进行变化。 元素类型:数组元素的类型,可以通过dype属性获得。 import numpy as np a=np.array([1,2,3,4],dtype=np.int32) b=np.array([1,2,3,4],dtype=np.float) c=np.array([1.2.3,41.dtype complex) print(a) print(b) print(c) print(a.dtype.type) print(b.dtype.type) print(c.dtype.type 输出结果: 1234 [1.2.3.4J 1.+0j2.+0j3.+0j4.+0l <class 'numpy.int32'> <classnumpy.float64'> classnumpy.complex128 注意:如果在计算的时候,发现类型不匹配,那么就可以采用astype,可以对数组的元素 的类型进行转换。 import numpy as np a=np.array([1,2,3,4],dtype=np.int32) b=np.array([1,2,3,4],dtype=np.float) c=a.astype(dtype np.float64) d=b.astype(dtype=np.int64) print(a.dtype.type print(b.dtype.type print(c.dtype.type print(d.dtype.type) 输出的结果:
[ 4] [ 5] [ 6] [ 7] [ 7] [ 8] [ 9] [10] ] 说明:可以通过 shape 这个函数,将当前的矩阵,进行变化。 元素类型:数组元素的类型,可以通过 dtype 属性获得。 import numpy as np a = np.array([1,2,3,4],dtype = np.int32) b = np.array([1,2,3,4],dtype = np.float) c = np.array([1,2,3,4],dtype = complex) print(a) print(b) print(c) print(a.dtype.type) print(b.dtype.type) print(c.dtype.type) 输出结果: [1 2 3 4] [1. 2. 3. 4.] [1.+0.j 2.+0.j 3.+0.j 4.+0.j] <class 'numpy.int32'> <class 'numpy.float64'> <class 'numpy.complex128'> 注意:如果在计算的时候,发现类型不匹配,那么就可以采用 astype,可以对数组的元素 的类型进行转换。 import numpy as np a = np.array([1,2,3,4],dtype = np.int32) b = np.array([1,2,3,4],dtype = np.float) c = a.astype(dtype = np.float64) d = b.astype(dtype = np.int64) print(a.dtype.type) print(b.dtype.type) print(c.dtype.type) print(d.dtype.type) 输出的结果:
sclass 'numovint32'> <class'numpy.float64'> class'numpy.float64 class'numpy.int64 自动生成数组 在numpy中,可以有一个函数arange,通过指定开始值,终值和步长来创建。 牛意:所得的结果,句括起始值,不句括终值。 import numpy as np a =np.arange(1.10.2) print(a) b=np.arange(0,1,0.1) 输出的值 [13579] 0.0.10.20.30.40.50.60.70.80.9j 下面介绍一种生成等差数列的一种方法:n pace(),可以通过等差数列的一维数组的参 数endpoint指定是否包含终值,默认True为包含终值。如果设定为False,不包含终值。 import numpy as np a=np.linspace(1,10,10,endpoint=True) print(a) b=np.inspace(1,10,10,endpoint=False) print(b) 输出的结果: 【1.2.3.4.5.6.7.8.9.10l [1.1.92.83.74.65.56.47.38.29.1) 接若介绍等比数列1 ogspace,注意:等比数列,都是以10^X import numpy as np A np.logspace(0,2,100 print(A) 输出的结果: 1 104761575 109749877 1.149757 120450354 1.2618 58 1.3219411 1.38 828 1.5199110 1.59228279 1.6681005 1.747528 1.8307382 1.9179102 2.009233 2.10490414 2.20513074 2.3101297 2.42012826 2.53536449 2.65608778 2.7825594 291505306 3.05385551 3.19926714 3.3516 3.5111 73 3.678 077 3.8535285g 4.03701 229 5.0941380 5.3366992 5.5908101 5.857020 6.1359072 6.42807312 6.73415066 7.05480231 7.39072203 7.74263683 8.111308318.497534368.902150859.32603347 9.77009957
<class 'numpy.int32'> <class 'numpy.float64'> <class 'numpy.float64'> <class 'numpy.int64'> 自动生成数组 在 numpy 中,可以有一个函数 arange,通过指定开始值,终值和步长来创建。 注意:所得的结果,包括起始值,不包括终值。 import numpy as np a = np.arange(1,10,2) print(a) b = np.arange(0,1,0.1) print(b) 输出的值: [1 3 5 7 9] [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] 下面介绍一种生成等差数列的一种方法:linspace(),可以通过等差数列的一维数组的参 数 endpoint 指定是否包含终值,默认 True 为包含终值。如果设定为 False,不包含终值。 import numpy as np a = np.linspace(1,10,10,endpoint = True) print(a) b = np.linspace(1,10,10,endpoint = False) print(b) 输出的结果: [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] [1. 1.9 2.8 3.7 4.6 5.5 6.4 7.3 8.2 9.1] 接着介绍等比数列 logspace,注意:等比数列,都是以 10^X import numpy as np A = np.logspace(0,2,100) print(A) 输出的结果: [ 1. 1.04761575 1.09749877 1.149757 1.20450354 1.26185688 1.32194115 1.38488637 1.45082878 1.51991108 1.59228279 1.66810054 1.7475284 1.83073828 1.91791026 2.009233 2.10490414 2.20513074 2.3101297 2.42012826 2.53536449 2.65608778 2.7825594 2.91505306 3.05385551 3.19926714 3.35160265 3.51119173 3.67837977 3.85352859 4.03701726 4.22924287 4.43062146 4.64158883 4.86260158 5.09413801 5.33669923 5.59081018 5.85702082 6.13590727 6.42807312 6.73415066 7.05480231 7.39072203 7.74263683 8.11130831 8.49753436 8.90215085 9.32603347 9.77009957