1 代碼1: 2 # -*- coding: utf-8 -*- 3 def function(*numbers): 4 sum = 0 5 for num in numbers: 6 sum = sum + num*num 7 return sum 8 9 10 print function(1,2,3) 11 a = [7,8] 12 print function(*a) 13 print function(*[1,2]) 14 print function(*(1,2,3)) 15 16 代碼2: 17 if __name__ == '__main__' 18 這段代碼怎麼理解?? 19 20 __name__ 是當前模塊名,當模塊被直接運行時模塊名爲 __main__ 。這句話的意思就是,當模塊被直接運行時,如下代碼塊將被運行,當模塊是被導入時,代碼塊不被運行。 21 22 23 代碼3: 24 import datetime as dt 25 26 # 從dt包裏取出datetime類,而後now是datetime類的方法 27 a=dt.datetime.now() 28 print a 29 30 # 從dt包裏取出datetime類,而後經過其初始化方法初始化datetime對象 31 d1 = dt.datetime(2005, 2, 16) 32 d2 = dt.datetime(2004, 12, 31) 33 print (d1 - d2).days 34 35 代碼4: 36 import pandas as pd 37 import numpy as np 38 39 40 41 dates = pd.date_range('20130101',periods=6) 42 43 df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD')) 44 45 print df 46 47 print '取出數據:',df.at[dates[0],'A'] 48 49 print '轉置\n',df.T