更多範例程式 (More Examples)

Hello.py:

#-*- coding: big5 -*- # 引用程式庫 import sys # 類別 class Hello: # 物件 def __init__(self): self.who = None # created by PyUML def sayHi(self): if self.who != None: print "Hello %s" % (self.who) def setWho(self, who): self.who = who # 主程式 if __name__ == "__main__": # 宣告陣列 whoList = ['鳥人'] # 計次迴圈 for count in range(1, len(sys.argv)): whoList.append(sys.argv[count]) # 初始物件 hello = Hello() # 窮舉迴圈 for man in whoList: hello.setWho(man) hello.sayHi()

Calc2.py:

#-*- coding: big5 -*- # 引用程式庫 import sys # 類別 class Calc: # 物件 def __init__(self): pass def dot(self, a, b): # 宣告二維陣列 ret = [[0 for x in xrange(len(a))] for x in xrange(len(b))] for i in range(0, len(a)): for j in range(0, len(b)): ret[i][j] = a[i] * b[j] return ret def output(self, ans, n): for i in range(0, n): for j in range(0, n): sys.stdout.write("%.4f\t" % (ans[i][j])) print "" # 主程式 if __name__ == "__main__": calc = Calc() a = [ 2.3, 1.4, 3.5] b = [-1.2, 6.1, -2.4] ans = calc.dot(a, b) calc.output(ans, len(a))

Calc.py:

#-*- coding: big5 -*- # 引用程式庫 import sys # 類別 class Calc: # 物件 def __init__(self): pass def dot(self, a, b): ret = [] for i in range(0, len(a)): for j in range(0, len(b)): ret.append(a[i] * b[j]) return ret def output(self, ans, n): for i in range(0, n): for j in range(0, n): sys.stdout.write("%.4f\t" % (ans[i*n + j])) print "" # 主程式 if __name__ == "__main__": calc = Calc() a = [ 2.3, 1.4, 3.5] b = [-1.2, 6.1, -2.4] ans = calc.dot(a, b) calc.output(ans, len(a))