本文共 4112 字,大约阅读时间需要 13 分钟。
作为基础练习吧。列表LIST,元组TUPLE,集合SET,字符串STRING等等,显示,增删,合并。。。
#===========List=====================shoplist = ['apple','mango','carrot','banana']print 'I have ',len(shoplist), ' items to purchase.'print 'These items are:'for item in shoplist: print item,print '\nI also have to buy rice.'shoplist.append('rice')print 'My shopping list is now', shoplistprint 'I will sort my list now'shoplist.sort()print 'Sorted shopping list is', shoplistprint 'The first item I will buy is', shoplist[0]olditem = shoplist[0]del shoplist[0]print 'I bought the', olditemprint 'My shopping list is ',shoplist#=======================Tuple===================zoo = ('python','elephant','penguin')print 'Number of animals in the zoo is',len(zoo)new_zoo = 'monkey', 'camel', zooprint 'Number of cages in the new zoo is', len(new_zoo)print 'All animals in new zoo are', new_zooprint 'Animals brought from old zoo are',new_zoo[2]print 'Last animal brought from old zoo is', new_zoo[2][2]print 'Number of animal in the new zoo is ',\ len(new_zoo)-1+len(new_zoo[2])#===========Dictionary======================ab = { 'Swaroop' :'swaroop@swaroopch.com', 'Larry' :'larry@wall.org', 'Matsumoto':'matz@ruby-lang.org', 'Spammer' :'spammer@hotmail.com' }print "Swaroop's address is", ab['Swaroop']del ab['Spammer']print '\nThere are {0} contacts in the address-book.\n'.format(len(ab))for name, address in ab.items(): print 'Contact {0} at {1}'.format(name, address)ab['Guido'] = 'guido@python.org'if 'Guido' in ab: print "\nGuido's address is",ab['Guido']#=============Sequence==============print 'Item 0 is',shoplist[0]print 'Item 3 is',shoplist[3]print 'Item -2 is',shoplist[-2]print 'Item 1 to 3 is',shoplist[1:3]print 'Item start to end is',shoplist[:]print 'Item step 2 is',shoplist[::2]#=============Set================bri = set(['brazil','russia','china','india'])print 'india' in briprint 'usa' in bribric = bri.copy()bric.add('france')print bric.issuperset(bri)bri.remove('russia')print bri & bric#=================References================print 'Simple Assignment'mylist = shoplistdel shoplist[0]print 'shoplist is', shoplistprint 'mylist is', mylistprint 'Copy by making a full slice'mylist = shoplist[:]del mylist[0]print 'shoplist is', shoplistprint 'mylist is', mylist#=====================String================name = 'Swaroop'if name.startswith('Swa'): print 'Yes, the string starts with Swa'if 'a' in name: print 'Yes, the string contains the string "a"'if name.find('war') != -1: print 'Yes, the string contains the string "war"'delimiter = '_*_'mylist = ['BRAZIL','RUSSIA','INDIA','CHINA']print delimiter.join(mylist)
输出:
C:\webpy\webpy\Scripts\python.exe C:/pycode/pycode.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 ['banana', 'carrot', 'mango', 'rice'] Number of animals in the zoo is 3 Number of cages in the new zoo is 3 All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin')) Animals brought from old zoo are ('python', 'elephant', 'penguin') Last animal brought from old zoo is penguin Number of animal in the new zoo is 5 Swaroop's address isThere are 3 contacts in the address-book.
Contact Swaroop at
Contact Matsumoto at Contact Larry atGuido's address is
Item 0 is banana Item 3 is rice Item -2 is mango Item 1 to 3 is ['carrot', 'mango'] Item start to end is ['banana', 'carrot', 'mango', 'rice'] Item step 2 is ['banana', 'mango'] True False True set(['brazil', 'china', 'india']) Simple Assignment shoplist is ['carrot', 'mango', 'rice'] mylist is ['carrot', 'mango', 'rice'] Copy by making a full slice shoplist is ['carrot', 'mango', 'rice'] mylist is ['mango', 'rice'] Yes, the string starts with Swa Yes, the string contains the string "a" Yes, the string contains the string "war" BRAZIL_*_RUSSIA_*_INDIA_*_CHINAProcess finished with exit code 0
转载地址:http://tuxyo.baihongyu.com/