]]>瀛ython涓ゅぉ鍗婃椂闂村啓鐨勬垚鏋?/title>http://www.shnenglu.com/fenglin/articles/125516.html椋庢灄椋庢灄Wed, 01 Sep 2010 05:11:00 GMThttp://www.shnenglu.com/fenglin/articles/125516.htmlhttp://www.shnenglu.com/fenglin/comments/125516.htmlhttp://www.shnenglu.com/fenglin/articles/125516.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125516.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125516.html#!/usr/bin/python #Filename:myAddressBook.py import os #used cPlickle store import cPickle as p
########class Person############## class Person: def__init__(self,name,email): self.name = name self.email = email
def__del__(self): pass ################################ #######class AddressBook####### class AddressBook: #the long name of data file addressFile ='Address.data'
#dictionary addressList ={}
def__init__(self): #If the data file of addressbook is not exists,then create it. ifnot os.path.isfile(AddressBook.addressFile): f = file(AddressBook.addressFile,'w') p.dump(AddressBook.addressList,f) f.close() print'Created file successfull!' else: #If the data file of addressbook is exists,then list all. f = file(AddressBook.addressFile) AddressBook.addressList = p.load(f) print'Current address list:' f.close() self.List()#call List function #When destroy object,save data to file. def__del__(self): try: try: f = file(AddressBook.addressFile,'w') p.dump(AddressBook.addressList,f) print'Save data successfull!' except: print'Save data fail!' finally: f.close()
#purpose:Add new record def Add(self,pepole): if len(pepole.name)==0: print'The length of name must longer than zero.' elif len(pepole.email)==0: print'The length of emial must longer than zero.' else: pr = Person(pepole.name,pepole.email) AddressBook.addressList[pepole.name] = pr print'Add new person successfull!'
#purpose:Delete a record by name def Del(self,name): if AddressBook.addressList.has_key(name): del AddressBook.addressList[name] print'Deleted %s successfull!'% name else: print'The name is not exists.'
#purpose:Modify a record by name def Edit(self,name,people): if AddressBook.addressList.has_key(name): AddressBook.addressList[name] = people print'Edit %s\'s information successfull!' % name else: print'The name is not exists.'
#purpose:Find a record by name def Find(self,name): if AddressBook.addressList.has_key(name): print'%s\'s information is :\nName:%s\nEmail:%s'\ % (name,\ AddressBook.addressList[name].name,\ AddressBook.addressList[name].email) else: print'The name is not exists.'
#purpose:List all record def List(self): print'\n'+'*'*10+'Address List'+'*'*10+'\n' for name,people in AddressBook.addressList.items(): print'%s\'s information is :\nName:%s\nEmail:%s' % (name,people.name,people.email) print'-'*30
################main#################################### os.system('clear') ad = AddressBook() while True: print'\n'+'*'*10+'Main menu'+'*'*10+''' 1.Add new address 2.Delete an address by name 3.Edit an address by name 4.Find an address by name 5.List all record 6.Exit'''+\ '\n'+'*'*30 try: choice = int(raw_input('Please choice:')) except: os.system('clear') print'Error:Please enter a number from 1 to 6,enter again.' continue
if choice ==1: name = raw_input('Please input name:') email = raw_input('Please input email:') people = Person(name,email) ad.Add(people) elif choice ==2: name = raw_input('Please input name:') ad.Del(name) elif choice ==3: name = raw_input('Please input name:') newemail = raw_input('Please input new email:') people = Person(name,newemail) ad.Edit(name,people) elif choice ==4: name = raw_input('Please input name:') ad.Find(name) elif choice ==5: ad.List() elif choice ==6: break else: os.system('clear')
]]>鍦ㄥ嚱鏁頒腑鎺ユ敹鍏冪粍鍜屽垪琛?/title>http://www.shnenglu.com/fenglin/articles/125479.html椋庢灄椋庢灄Wed, 01 Sep 2010 00:28:00 GMThttp://www.shnenglu.com/fenglin/articles/125479.htmlhttp://www.shnenglu.com/fenglin/comments/125479.htmlhttp://www.shnenglu.com/fenglin/articles/125479.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125479.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125479.html浣跨敤*鍜?*鍓嶇紑銆?br>榪欑鏂規(guī)硶鍦ㄥ嚱鏁伴渶瑕佽幏鍙栧彲鍙樻暟閲忕殑鍙傛暟鐨勬椂鍊欑壒鍒湁鐢ㄣ?br>>>> def powersum(power, *args): ... '''Return the sum of each argument raised to specified power.''' ... total = 0 ... for i in args: ... total += pow(i, power) ... return total ... >>> powersum(2, 3, 4) 25 >>> powersum(2, 10) 100 鐢變簬鍦╝rgs鍙橀噺鍓嶆湁*鍓嶇紑錛屾墍鏈夊浣欑殑鍑芥暟鍙傛暟閮戒細(xì)浣滀負(fù)涓涓厓緇勫瓨鍌ㄥ湪args涓傚鏋滀嬌鐢ㄧ殑 鏄?*鍓嶇紑錛屽浣欑殑鍙傛暟鍒欎細(xì)琚涓烘槸涓涓瓧鍏哥殑閿?鍊煎銆?/span>
]]>sys妯″潡http://www.shnenglu.com/fenglin/articles/125429.html椋庢灄椋庢灄Tue, 31 Aug 2010 10:06:00 GMThttp://www.shnenglu.com/fenglin/articles/125429.htmlhttp://www.shnenglu.com/fenglin/comments/125429.htmlhttp://www.shnenglu.com/fenglin/articles/125429.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125429.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125429.html#!/usr/bin/python # Filename: cat.py import sys def readfile(filename): '''Print a file to the standard output.''' f = file(filename) while True: line = f.readline() if len(line) == 0: break print line, # notice comma f.close() # Script starts from here if len(sys.argv) <2: print'No action specified.' sys.exit() if sys.argv[1].startswith('--'): option = sys.argv[1][2:] # fetch sys.argv[1] but without the first two characters if option =='version': print'Version 1.2' elif option =='help': print'''\ This program prints files to the standard output. Any number of files can be specified. Options include: --version : Prints the version number --help : Display this help''' else: print'Unknown option.' sys.exit() else: for filename in sys.argv[1:]: readfile(filename)杈撳嚭 $ python cat.py No action specified. $ python cat.py --help This program prints files to the standard output. Any number of files can be specified. Options include: --version : Prints the version number --help : Display this help $ python cat.py --version Version 1.2 $ python cat.py --nonsense Unknown option. $ python cat.py poem.txt Programming is fun When the work is done if you wanna make your work also fun: use Python!
]]>try..finallyhttp://www.shnenglu.com/fenglin/articles/125423.html椋庢灄椋庢灄Tue, 31 Aug 2010 09:19:00 GMThttp://www.shnenglu.com/fenglin/articles/125423.htmlhttp://www.shnenglu.com/fenglin/comments/125423.htmlhttp://www.shnenglu.com/fenglin/articles/125423.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125423.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125423.html鍙互浣跨敤finally鍧楁潵瀹屾垚銆傛敞鎰忥紝鍦ㄤ竴涓猼ry鍧椾笅錛屼綘鍙互鍚屾椂浣跨敤except浠庡彞鍜宖inally鍧椼傚鏋?br>浣犺鍚屾椂浣跨敤瀹冧滑鐨勮瘽錛岄渶瑕佹妸涓涓祵鍏ュ彟澶栦竴涓?br>
#!/usr/bin/python # Filename: finally.py import time try: f = file('poem.txt') while True: # our usual file-reading idiom line = f.readline() if len(line) == 0: break time.sleep(2) print line, finally: f.close() print'Cleaning upclosed the file'
杈撳嚭 $ python finally.py Programming is fun When the work is done Cleaning up...closed the file Traceback (most recent call last): File "finally.py", line 12, in ? time.sleep(2) KeyboardInterrupt
]]>raise 寮曞彂寮傚父http://www.shnenglu.com/fenglin/articles/125421.html椋庢灄椋庢灄Tue, 31 Aug 2010 09:12:00 GMThttp://www.shnenglu.com/fenglin/articles/125421.htmlhttp://www.shnenglu.com/fenglin/comments/125421.htmlhttp://www.shnenglu.com/fenglin/articles/125421.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125421.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125421.html鍙互寮曞彂鐨勯敊璇垨寮傚父搴旇鍒嗗埆鏄竴涓狤rror鎴朎xception綾葷殑鐩存帴鎴栭棿鎺ュ鍑虹被銆?br>
#!/usr/bin/python # Filename: raising.py class ShortInputException(Exception): '''A user-defined exception class.''' def__init__(self, length, atleast): Exception.__init__(self) self.length = length self.atleast = atleast try: s = raw_input('Enter something --> ') if len(s) <3: raise ShortInputException(len(s), 3) # Other work can continue as usual here except EOFError: print'\nWhy did you do an EOF on me?' except ShortInputException, x: print'ShortInputException: The input was of length %d, \ was expecting at least %d' % (x.length, x.atleast) else: print'No exception was raised.'
杈撳嚭 $ python raising.py Enter something --> Why did you do an EOF on me? $ python raising.py Enter something --> ab ShortInputException: The input was of length 2, was expecting at least 3 $ python raising.py Enter something --> abc No exception was raised.
]]>澶勭悊寮傚父http://www.shnenglu.com/fenglin/articles/125420.html椋庢灄椋庢灄Tue, 31 Aug 2010 09:03:00 GMThttp://www.shnenglu.com/fenglin/articles/125420.htmlhttp://www.shnenglu.com/fenglin/comments/125420.htmlhttp://www.shnenglu.com/fenglin/articles/125420.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125420.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125420.html鐞嗚鍙ユ斁鍦╡xcept-鍧椾腑銆?br>
#!/usr/bin/python # Filename: try_except.py import sys try: s = raw_input('Enter something --> ') except EOFError: print'\nWhy did you do an EOF on me?' sys.exit() # exit the program except: print'\nSome error/exception occurred.' # here, we are not exiting the program print'Done'
杈撳嚭 $ python try_except.py Enter something --> Why did you do an EOF on me? $ python try_except.py Enter something --> Python is exceptional! Done
]]>鍌ㄥ瓨鍣?/title>http://www.shnenglu.com/fenglin/articles/125418.html椋庢灄椋庢灄Tue, 31 Aug 2010 08:54:00 GMThttp://www.shnenglu.com/fenglin/articles/125418.htmlhttp://www.shnenglu.com/fenglin/comments/125418.htmlhttp://www.shnenglu.com/fenglin/articles/125418.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125418.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125418.html鍚庝綘鍙堝彲浠ユ妸瀹冨畬鏁存棤緙哄湴鍙栧嚭鏉ャ傝繖琚О涓?鎸佷箙鍦?鍌ㄥ瓨瀵硅薄銆?br>榪樻湁鍙︿竴涓ā鍧楃О涓?span style="COLOR: red">cPickle錛屽畠鐨勫姛鑳藉拰pickle妯″潡瀹屽叏鐩稿悓錛屽彧涓嶈繃瀹冩槸鐢–璇█緙栧啓鐨勶紝鍥?br>姝よ蹇緱澶氾紙姣攑ickle蹇?000鍊?/span>錛夈備綘鍙互浣跨敤瀹冧滑涓殑浠諱竴涓紝鑰屾垜浠湪榪欓噷灝嗕嬌鐢╟Pickle妯?br>鍧椼傝浣忥紝鎴戜滑鎶婅繖涓や釜妯″潡閮界畝縐頒負(fù)pickle妯″潡銆?br>
#!/usr/bin/python # Filename: pickling.py import cPickle as p #import pickle as p shoplistfile ='shoplist.data' # the name of the file where we will store the object shoplist = ['apple', 'mango', 'carrot'] # Write to the file f = file(shoplistfile, 'w') p.dump(shoplist, f) # dump the object to a file f.close() del shoplist # remove the shoplist # Read back from the storage f = file(shoplistfile) storedlist = p.load(f) print storedlist
]]>鏂囦歡http://www.shnenglu.com/fenglin/articles/125415.html椋庢灄椋庢灄Tue, 31 Aug 2010 08:33:00 GMThttp://www.shnenglu.com/fenglin/articles/125415.htmlhttp://www.shnenglu.com/fenglin/comments/125415.htmlhttp://www.shnenglu.com/fenglin/articles/125415.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125415.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125415.html鎭板綋鍦拌鍐欐枃浠躲傚鏂囦歡鐨勮鍐欒兘鍔涗緷璧栦簬浣犲湪鎵撳紑鏂囦歡鏃舵寚瀹氱殑妯″紡銆傛渶鍚庯紝褰撲綘瀹屾垚瀵規(guī)枃 浠剁殑鎿嶄綔鐨勬椂鍊欙紝浣犺皟鐢╟lose鏂規(guī)硶鏉ュ憡璇塒ython鎴戜滑瀹屾垚浜嗗鏂囦歡鐨勪嬌鐢ㄣ?br>
#!/usr/bin/python # Filename: using_file.py poem ='''\ Programming is fun When the work is done if you wanna make your work also fun: use Python! ''' f = file('poem.txt', 'w') # open for 'w'riting f.write(poem) # write text to file f.close() # close the file f = file('poem.txt') # if no mode is specified, 'r'ead mode is assumed by default while True: line = f.readline() if len(line) == 0: # Zero length indicates EOF break print line, # Notice comma to avoid automatic newline added by Python f.close() # close the file
杈撳嚭 $ python using_file.py Programming is fun When the work is done if you wanna make your work also fun: use Python!
]]>浣跨敤綾諱笌瀵硅薄鐨勫彉閲?/title>http://www.shnenglu.com/fenglin/articles/125411.html椋庢灄椋庢灄Tue, 31 Aug 2010 08:04:00 GMThttp://www.shnenglu.com/fenglin/articles/125411.htmlhttp://www.shnenglu.com/fenglin/comments/125411.htmlhttp://www.shnenglu.com/fenglin/articles/125411.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125411.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125411.html#!/usr/bin/python # Filename: objvar.py class Person: '''Represents a person.''' population = 0 def__init__(self, name): '''Initializes the person's data.''' self.name = name print'(Initializing %s)'% self.name # When this person is created, he/she # adds to the population Person.population +=1 def__del__(self): '''I am dying.''' print'%s says bye.'% self.name Person.population -=1 if Person.population == 0: print'I am the last one.' else: print'There are still %d people left.'% Person.population def sayHi(self): '''Greeting by the person. Really, that's all it does.''' print'Hi, my name is %s.'% self.name def howMany(self): '''Prints the current population.''' if Person.population ==1: print'I am the only person here.' else: print'We have %d persons here.'% Person.population swaroop = Person('Swaroop') swaroop.sayHi() swaroop.howMany() kalam = Person('Abdul Kalam') kalam.sayHi() kalam.howMany() swaroop.sayHi() swaroop.howMany() 杈撳嚭 $ python objvar.py (Initializing Swaroop) Hi, my name is Swaroop. I am the only person here. (Initializing Abdul Kalam) Hi, my name is Abdul Kalam. We have 2 persons here. Hi, my name is Swaroop. We have 2 persons here. Abdul Kalam says bye. There are still 1 people left. Swaroop says bye. I am the last one.
]]>__init__鏂規(guī)硶http://www.shnenglu.com/fenglin/articles/125408.html椋庢灄椋庢灄Tue, 31 Aug 2010 07:39:00 GMThttp://www.shnenglu.com/fenglin/articles/125408.htmlhttp://www.shnenglu.com/fenglin/comments/125408.htmlhttp://www.shnenglu.com/fenglin/articles/125408.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125408.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125408.html鏈涚殑 鍒濆鍖?銆傛敞鎰忥紝榪欎釜鍚嶇О鐨勫紑濮嬪拰緇撳熬閮芥槸鍙屼笅鍒掔嚎銆?br>
#!/usr/bin/python # Filename: class_init.py class Person: def__init__(self, name): self.name = name def sayHi(self): print'Hello, my name is', self.name p = Person('Swaroop') p.sayHi() # This short example can also be written as Person('Swaroop').sayHi()
杈撳嚭 $ python class_init.py Hello, my name is Swaroop
]]>綾?/title>http://www.shnenglu.com/fenglin/articles/125407.html椋庢灄椋庢灄Tue, 31 Aug 2010 07:33:00 GMThttp://www.shnenglu.com/fenglin/articles/125407.htmlhttp://www.shnenglu.com/fenglin/comments/125407.htmlhttp://www.shnenglu.com/fenglin/articles/125407.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125407.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125407.html#!/usr/bin/python # Filename: simplestclass.py class Person: pass# An empty block p = Person() print p 杈撳嚭 $ python simplestclass.py <__main__.Person instance at 0xf6fcb18c>
]]>瀛楃涓茬殑鏂規(guī)硶http://www.shnenglu.com/fenglin/articles/125399.html椋庢灄椋庢灄Tue, 31 Aug 2010 06:22:00 GMThttp://www.shnenglu.com/fenglin/articles/125399.htmlhttp://www.shnenglu.com/fenglin/comments/125399.htmlhttp://www.shnenglu.com/fenglin/articles/125399.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125399.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125399.html#!/usr/bin/python # Filename: str_methods.py name ='Swaroop'# This is a string object if name.startswith('Swa'): print'Yes, the string starts with "Swa"' if'a'in name: print'Yes, it contains the string "a"' if name.find('war') !=-1: print'Yes, it contains the string "war"' delimiter ='_*_' mylist = ['Brazil', 'Russia', 'India', 'China'] print delimiter.join(mylist) 杈撳嚭 $ python str_methods.py Yes, the string starts with "Swa" Yes, it contains the string "a" Yes, it contains the string "war" Brazil_*_Russia_*_India_*_China
]]>寮曠敤http://www.shnenglu.com/fenglin/articles/125398.html椋庢灄椋庢灄Tue, 31 Aug 2010 06:14:00 GMThttp://www.shnenglu.com/fenglin/articles/125398.htmlhttp://www.shnenglu.com/fenglin/comments/125398.htmlhttp://www.shnenglu.com/fenglin/articles/125398.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125398.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125398.html瀵硅薄鏈韓錛佷篃灝辨槸璇達(dá)紝鍙橀噺鍚嶆寚鍚戜綘璁$畻鏈轟腑瀛樺偍閭d釜瀵硅薄鐨勫唴瀛樸傝繖琚О浣滃悕縐板埌瀵硅薄鐨勭粦 瀹氥?br>
#!/usr/bin/python # Filename: reference.py print'Simple Assignment' shoplist = ['apple', 'mango', 'carrot', 'banana'] mylist = shoplist # mylist is just another name pointing to the same object! del shoplist[0] print'shoplist is', shoplist print'mylist is', mylist # notice that both shoplist and mylist both print the same list without # the 'apple' confirming that they point to the same object print'Copy by making a full slice' mylist = shoplist[:] # make a copy by doing a full slice del mylist[0] # remove first item print'shoplist is', shoplist print'mylist is', mylist # notice that now the two lists are different
杈撳嚭 $ python reference.py Simple Assignment shoplist is ['mango', 'carrot', 'banana'] mylist is ['mango', 'carrot', 'banana'] Copy by making a full slice shoplist is ['mango', 'carrot', 'banana'] mylist is ['carrot', 'banana']
]]>搴忓垪http://www.shnenglu.com/fenglin/articles/125384.html椋庢灄椋庢灄Tue, 31 Aug 2010 03:41:00 GMThttp://www.shnenglu.com/fenglin/articles/125384.htmlhttp://www.shnenglu.com/fenglin/comments/125384.htmlhttp://www.shnenglu.com/fenglin/articles/125384.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125384.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125384.html鐐規(guī)槸绱㈠紩鎿嶄綔絎﹀拰鍒囩墖鎿嶄綔絎︺傜儲寮曟搷浣滅璁╂垜浠彲浠ヤ粠搴忓垪涓姄鍙栦竴涓壒瀹氶」鐩傚垏鐗囨搷浣?br>絎﹁鎴戜滑鑳藉鑾峰彇搴忓垪鐨勪竴涓垏鐗囷紝鍗充竴閮ㄥ垎搴忓垪銆?br>
#!/usr/bin/python # Filename: seq.py shoplist = ['apple', 'mango', 'carrot', 'banana'] # Indexing or 'Subscription' operation print'Item 0 is', shoplist[0] print'Item 1 is', shoplist[1] print'Item 2 is', shoplist[2] print'Item 3 is', shoplist[3] print'Item -1 is', shoplist[-1] print'Item -2 is', shoplist[-2] # Slicing on a list print'Item 1 to 3 is', shoplist[1:3] print'Item 2 to end is', shoplist[2:] print'Item 1 to -1 is', shoplist[1:-1] print'Item start to end is', shoplist[:] # Slicing on a string name ='swaroop' print'characters 1 to 3 is', name[1:3] print'characters 2 to end is', name[2:] print'characters 1 to -1 is', name[1:-1] print'characters start to end is', name[:]
杈撳嚭 $ python seq.py Item 0 is apple Item 1 is mango Item 2 is carrot Item 3 is banana Item -1 is banana Item -2 is carrot Item 1 to 3 is ['mango', 'carrot'] Item 2 to end is ['carrot', 'banana'] Item 1 to -1 is ['mango', 'carrot'] Item start to end is ['apple', 'mango', 'carrot', 'banana'] characters 1 to 3 is wa characters 2 to end is aroop characters 1 to -1 is waroo characters start to end is swaroop
#!/usr/bin/python # Filename: using_dict.py # 'ab' is short for 'a'ddress'b'ook ab = { 'Swaroop' : 'swaroopch@byteofpython.info', 'Larry' : 'larry@wall.org', 'Matsumoto' : 'matz@ruby-lang.org', 'Spammer' : 'spammer@hotmail.com' } print"Swaroop's address is %s"% ab['Swaroop'] # Adding a key/value pair ab['Guido'] ='guido@python.org' # Deleting a key/value pair del ab['Spammer'] print'\nThere are %d contacts in the address-book\n'% len(ab) for name, address in ab.items(): print'Contact %s at %s'% (name, address) if'Guido'in ab: # OR ab.has_key('Guido') print"\nGuido's address is %s"% ab['Guido']
]]>鍏冪粍http://www.shnenglu.com/fenglin/articles/125380.html椋庢灄椋庢灄Tue, 31 Aug 2010 03:09:00 GMThttp://www.shnenglu.com/fenglin/articles/125380.htmlhttp://www.shnenglu.com/fenglin/comments/125380.htmlhttp://www.shnenglu.com/fenglin/articles/125380.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125380.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125380.html鎷彿涓敤閫楀彿鍒嗗壊鐨勯」鐩畾涔夈傚厓緇勯氬父鐢ㄥ湪浣胯鍙ユ垨鐢ㄦ埛瀹氫箟鐨勫嚱鏁拌兘澶熷畨鍏ㄥ湴閲囩敤涓緇勫?br>鐨勬椂鍊欙紝鍗寵浣跨敤鐨勫厓緇勭殑鍊間笉浼?xì)鏀瑰彉銆?br>
#!/usr/bin/python # Filename: using_tuple.py zoo = ('wolf', 'elephant', 'penguin') print'Number of animals in the zoo is', len(zoo) new_zoo = ('monkey', 'dolphin', zoo) print'Number of animals in the new zoo is', len(new_zoo) print'All animals in new zoo are', new_zoo print'Animals brought from old zoo are', new_zoo[2] print'Last animal brought from old zoo is', new_zoo[2][2]
杈撳嚭 $ python using_tuple.py Number of animals in the zoo is 3 Number of animals in the new zoo is 3 All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin')) Animals brought from old zoo are ('wolf', 'elephant', 'penguin') Last animal brought from old zoo is penguin
#!/usr/bin/python # Filename: print_tuple.py age =22 name ='Swaroop' print'%s is %d years old'% (name, age) print'Why is %s playing with that python?'% name
杈撳嚭 $ python print_tuple.py Swaroop is 22 years old Why is Swaroop playing with that python?
]]>鍒楄〃http://www.shnenglu.com/fenglin/articles/125368.html椋庢灄椋庢灄Tue, 31 Aug 2010 02:54:00 GMThttp://www.shnenglu.com/fenglin/articles/125368.htmlhttp://www.shnenglu.com/fenglin/comments/125368.htmlhttp://www.shnenglu.com/fenglin/articles/125368.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125368.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125368.html涓涓喘鐗╁垪琛紝涓婇潰璁拌澆鐫浣犺涔扮殑涓滆タ錛屼綘灝卞鏄撶悊瑙e垪琛ㄤ簡銆傚彧涓嶈繃鍦ㄤ綘鐨勮喘鐗╄〃涓婏紝鍙?br>鑳芥瘡鏍蜂笢瑗塊兘鐙嚜鍗犳湁涓琛岋紝鑰屽湪Python涓紝浣犲湪姣忎釜欏圭洰涔嬮棿鐢ㄩ楀彿鍒嗗壊銆?br>鍒楄〃涓殑欏圭洰搴旇鍖呮嫭鍦ㄦ柟鎷彿涓紝榪欐牱Python灝辯煡閬撲綘鏄湪鎸囨槑涓涓垪琛ㄣ備竴鏃︿綘鍒涘緩浜嗕竴 涓垪琛紝浣犲彲浠ユ坊鍔犮佸垹闄ゆ垨鏄悳绱㈠垪琛ㄤ腑鐨勯」鐩傜敱浜庝綘鍙互澧炲姞鎴栧垹闄ら」鐩紝鎴戜滑璇村垪琛?br>鏄?鍙彉鐨?鏁版嵁綾誨瀷錛屽嵆榪欑綾誨瀷鏄彲浠ヨ鏀瑰彉鐨勩?br>
#!/usr/bin/python # Filename: using_list.py # This is my shopping list shoplist = ['apple', 'mango', 'carrot', 'banana'] print'I have', len(shoplist),'items to purchase.' print'These items are:', # Notice the comma at end of the line for item in shoplist: print item, print'\nI also have to buy rice.' shoplist.append('rice') print'My shopping list is now', shoplist print'I will sort my list now' shoplist.sort() print'Sorted shopping list is', shoplist print'The first item I will buy is', shoplist[0] olditem = shoplist[0] del shoplist[0] print'I bought the', olditem print'My shopping list is now', shoplist
杈撳嚭 $ python using_list.py I have 4 items to purchase. These items are: apple mango carrot banana I also have to buy rice. My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice'] I will sort my list now Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice'] The first item I will buy is apple I bought the apple My shopping list is now ['banana', 'carrot', 'mango', 'rice']
]]>create module and call modulehttp://www.shnenglu.com/fenglin/articles/125363.html椋庢灄椋庢灄Tue, 31 Aug 2010 02:31:00 GMThttp://www.shnenglu.com/fenglin/articles/125363.htmlhttp://www.shnenglu.com/fenglin/comments/125363.htmlhttp://www.shnenglu.com/fenglin/articles/125363.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125363.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125363.html#!/usr/bin/python # Filename: mymodule.py def sayhi(): print'Hi, this is mymodule speaking.' version ='0.1' # End of mymodule.py 璁頒綇榪欎釜妯″潡搴旇琚斁緗湪鎴戜滑杈撳叆瀹冪殑紼嬪簭鐨勫悓涓涓洰褰曚腑錛屾垨鑰呭湪sys.path鎵鍒楃洰褰曚箣涓銆?br>
杈撳嚭 $ python mymodule_demo.py Hi, this is mymodule speaking. Version 0.1
涓嬮潰鏄竴涓嬌鐢╢rom..import璇硶鐨勭増鏈?/strong>
#!/usr/bin/python # Filename: mymodule_demo2.py from mymodule import sayhi, version # Alternative: # from mymodule import * sayhi() print'Version', version