Python provides three built-in data structures:
List tuple Dictionaries
A. List (you can check the list, insert, delete, change)
#!/usr/bin/python#filename:using_list.pyshoplist=[' apple ', ' mango ', ' carrot ', ' banana ']print ' i have ', Len (shoplist), ' items to purchase ' print ' these item sare: ',for item in Shoplist: print item, ' \ n ' shoplist.append (' rice ') print shoplistshoplist.sort () print ' \ n ',shoplistprint ' \ n ',shoplist[0]del shoplist[0]print shoplist ~ "using_list.py" 14L, 333C written [[email protected]~]# python using_list.pyi have 4 items to purchasethese item sare: apple mango carrot banana [' apple ', ' mango ', ' carrot ', ' banana ', ' rice ' [' apple ', ' banana ', ' carrot ', ' mango ', ' rice ']apple[' banana ', ' carrot ', ' mango ', ' rice '
Two. Tuples (like strings, not modifiable)
Print '%s is%d years old '% (name,age)
#!/usr/bin/python#filename:print_tuple.pyage=22name= ' Swaroop ' print '%s is %d years Old '% (name,age) print ' Why is %s playing with that python? ' %name~ "print_tuple.py" [New] 6L, 152C written [[email protected] ~]# python print_ Tuple.pyswaroop is 22 years oldwhy is swaroop playing with that python?
Three. Dictionaries
If ' Guido ' in AB:
Print "%s"% ab[' Guido ']
#!/usr/bin/python#filename:using_dict.pyab={' Swaroop ': ' [email protected] ', ' Larry ' : ' [email protected] ', ' Matsumoto ': ' [email protected] ', ' spammer ': ' [email protected] '}print ' swaroop ' s address is %s ab[' Swaroop '] "Ab[' Guido ']= ' [email protected] ' del ab[' spammer ']print len (AB) for name,address in ab.items (): print ' contact %s at a%s '% (name,address) if ' Guido ' in ab: print ' \nguido ' s address is %s '% ab[' Guido '] ~ "using_dict.py" [New] 15L, 445C Written [[email protected]~] # python using_dict.pyswaroop ' s address is %s ab[' Swaroop ']4Contact Swaroop at [email protected]Contact Matsumoto at [email protected]Contact Larry at [email protected]contact guido at [email protected]guido ' s Address is [email protected]
Index
Shoplist[1:3]
Shoplist[2:]
shoplist[:]
#!/usr/bin/python#filename:seq.pyshoplist=[' apple ', ' mango ', ' canot ', ' banana ']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]print ' item 1 to 3 is ',shoplist[1:3]print ' Item 2 to end is ',shoplist[2:]print ' item 1 to to -1 is ', shoplist[1:-1]print ' Item start to end is ', shoplist[:]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[:] "using_seq.py" [New] 18L, 600c written[[email protected] ~]# python using_seq.pyitem 0 is Appleitem 1 is mangoitem 2 is canotitem 3 is bananaitem -1 is bananaitem -2 is canotitem 1 to 3 is [' Mango ', ' Canot ']item 2 to end is [' canot ', ' banana ']item 1 to to -1 is [' Mango ', ' canot ']item start to end is [' apple ', ' mango ', ' canot ', ' banana ']characters 1 to 3 is wacharacters 2 to End is aroopcharacters&nBsp;1 to -1 is waroocharacters start to end is swaroop
Method of String:
If Name.startswith (' Swa '):
If ' a ' in name:
If Name.find (' War ')!=-1:
Delimiter= ' _*_ '
Arr=[' A ', ' B ', ' C ']
Delimiter.join (arr)
A_*_b_*_c
#!/usr/bin/python#filename:str_methods.pyname= ' Swaroop ' if name.startswith (' Swa '): print ' Yes,the string start 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) ~ ~ "str_methods.py" 12L, 344C written [[email protected] ~]# python str_methods.pyyes,the string start with swayes,it contains the string "a" yes,it contains the string " War "Brazil_*_russia_*_india_*_china
This article is from the "Wang Nimei Adult Road" blog, reprint please contact the author!
Python Development (ii)