Data structure of the Python language 1 (Sequence--list, tuple, string)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.