func.py #-*-CODING:GBK-*-"" "in Python the default is ANSI encoding format to use Chinese need to explicitly specify coded array into dynamic array and static array dynamic array can dynamically add element static array cannot change data structure def definition func tion def func:return void type function definition def funcshow (): print "Void type Func Test" returnfuncshow (); "" Import stringimport func #引入另一个模块import sys# test output Print R "This was a line with \ n" Print 5**5print u "abcde:%s---g%s"% ("Yuedo Ngwei ", [' One ', ' II ', ' three ') ') ' For I in range (0, 5): Print Ielse:passprint" string "+ hex #输出print __name__# array Test A = [1,2,[1,2,[1,2,[1,2,[2,3,4,5]]]]]print a[2][2][2][2]ii = 0arr = [1,23,4]arr.append] Arr.reverse () #reserve Reverse output print arrarr.append ([2,3,4,5,6,7]), Arr.sort ();p rint arr; arrfixed= (1,2,3,4,5) #固定数组print Arrfixedarrmultidimensional = [I for I in range (3,10), 1,[]]print arrmultidimensionalarrmultidimensional[2].append (" Test ") Print arrmultidimensional# dictionary operation dic={" Key1 ": 1," Key2 ": 2};p rint dicprint dic.keys () print dic.get (" Key1 ") print dic ["Key1"]print sys.path# function called Test Func. Funcshow (); #FuncShow ();p rint "Result:" +str (Func.add(100,200)) Print __doc__; #返回注释print nonesystest.py#-*-coding:gbk-*-"" "System Call" "" Import Osfilehandle=open ("C:\\1.txt", "W") Filehandle.write (" I am a programmer ") Filehandle.close () Filehandle=open (" C:\\1.txt "," R ") print Filehandle.read () object-oriented python#-*-CODING:GBK-*-" The object-oriented Python self is the default implicit delivery "" "Class Data:i=0 def GetData (self) that shows delivery of C + + Java, etc.: Return self.i def __ini T__ (self,a,b,c): self.a=a; Self.b=b; Self.c=c; def ShowData (self): print "a=%d,b=%d,c=%d"% (SELF.A,SELF.B,SELF.C) Returnobj=data (all in all) print obj. GetData () obj. ShowData ()
Code practice Simple File read-write string array processing list Dictionary