0. Sequence
Lists, tuples, strings are sequences.
There are two features of the sequence: index operator and slice operator. The index operator allows us to fetch a specific item from the sequence. The slice operator allows us to get a slice of the sequence, which is part of the sequence.
Take the string as an example:
1 str= "01 3456 89? "2 3 print (str[0]) #4 print (Str[1:5]) #< Span style= "color: #008000;" > ' 1 34 ', sliced 5 print (Str[5:1:-1]) # ' 543 ' 6 print (str[ 1:5:-1]) #output a ' \ n ', no error
1. List
Common actions for lists:
1 fruitlist = [‘Lemon‘,‘Mango‘]2Print (Len (fruitlist),"Kinds of fruit" )#2 Kinds of fruit34For fruitInchFruitlist:5Print(fruit)67 Fruitlist.append ("Banana" )#fruitlist = [' Lemon ', ' mango ', ' banana '] 9 Fruitlist[0]= ' orange ' #fruitlist = [' Orange ', ' Mango ']10 fruitlist.sort () fruitlist = [' banana ', ' orange ']
Of course you can play the same way:
1 special_list = ["str", 1, ("Tuplea", 2), [3,4,5]]#Tuplea#5
2. Tuples
Tuples and lists are very similar, except that tuples and strings are immutable. That is, you cannot modify tuples. The tuple is initialized in the following way:
1 fruittuple = ('lemon'mango'#empty tuple#tuple have only one element
Note When initializing a tuple of an element, a comma is required after the unique element, indicating that a tuple is currently initialized instead of an object with parentheses.
Commonly used in print statements, and% cooperation to achieve custom string output.
3. String
Common String Operations:
1Smile ="~^_^~"2 ifSmile.startswith ("@[email protected]"):3 Print("It is fake smile")4 if "~" inchSmile:5 Print("Shy")6 ifSmile.find ("?")! =-1:7 Print("Is you confused?")8 9Alist = ["1","2","3"]TenJoinstr = Smile.join (alist)#joinstr = ' 1~ ^_^ ~2~ ^_^ ~ ' OneSplitlist = Joinstr.split ('~')#splitlist = [' 1 ', ' ^_^ ', ' 2 ', ' ^_^ ', ' 3 ']
Data structure of the Python language 1 (Sequence--list, tuple, string)