Basic Python Tutorial (iv)

Source: Internet
Author: User

List

This section continues the discussion of the list of different tuples and strings: The list is mutable (mutable)---- can change the contents of the list, and the list has many useful, specialized methods.

the list function can split a string into a list.

>>> List (‘Chongshi ") ["  C ",  ' h",  ' o ",  ' n ",  "g", h",  "i "            

Changing the list: assigning values to an element

For example, we want to assign a value to an element in a list.

>>> x =[1,2,3,4]>>> x[2]=18>>> x[1, 2, 4] 

Delete Element

It is also easy to remove elements from the list, using the DELE statement.

>>> names = [‘Zhangsan','Lisi','Wangwu','sunliu']>>> del names [2]>>> names['zhangsan', 'Lisi', 'sunliu']  

Shard Assignment :

>>> name = List (‘Huzi‘) >>>name[‘H‘,‘U‘,‘Z‘,‘I‘]>>> Name[2:]=list ( "dazhi " >>> Name[ ' h",  "u" d  ", " a ', z",  ' h",  i "         

NameThe content is "Huzi", starting with the first 3 characters (2), replacing"da "with"zi"and being expanded"zhi", so the new The content of name is "Hudazhi".

List method

A method is a function that is closely related to some object, which can be a list, a number, or a string or other type of object, which in general is called by the method:

Object. Method (Parameter)

A method call is similar to a function call.

1,append

The Append method is used to append a new object to the end of the list:

>>> ABC = []>>> abc.append (4) >>> abc[1, 2, 3, 4]  

2. Count

The Count method counts the number of occurrences of an element in the list:

>>> [‘to ",  '  Be ",  ' or",  ' not ",  "to", 

3, Extend

The Extend method can append multiple values from another sequence at the end of the list at once. To extend an existing list with a new list:

>>> a = []>>> b = [4,5,6]>>> a.extend (b) >>> a[1, 2, 3, 4, 5, 6]
         

4. Index

The index method is used to find the first occurrence of a value from a list.

>>> Knights = [‘We‘,‘Is ",  ' the",  ' kninghts ",  "who",  ' say",  ni "]>>> Knights.index (  "who"  ) 4>>> knights[4] " who "         

5. Insert

The Insert method is used to insert an object into the list:

>>> numbers = [1,2,3,5,6,7]>>> Numbers.insert (3,'four') >>>'  Four', 5, 6, 7]       

6. Pop

The pop method removes an element from the list (the last one by default) and returns the value of the element:

>>> x = []>>> x.pop () 3>>> x[1, 2]>>> x.pop (0) 1>> > x[2]     

7. Remove

The Remove method is used to remove the first occurrence of a value in the list:

>>> x = [‘To‘,‘Be‘,‘Or‘,‘Not‘,‘To ", " be ' to  ",  ' or",  ' not ",  ' to ",  "be"]      

8, Reverse

The revers method stores the elements in the list in reverse

>>> x = []>>> x.reverse () >>> x[3, 2, 1]  

9. Sort

The sort method is used to sort the list at the original location. "Sort in place" changes the original list so that the elements can be sorted in a certain order.

>>> x = [4,6,2,1,7,9]>>> x.sort () >>> x[1, 2, 4, 6, 7, 9]  

Tuples: Immutable sequences

Tuples, like lists, are also a sequence. The only difference is that tuples cannot be changed. The syntax for creating tuples is simple: if you split some values with commas, you automatically create tuples.

>>>(1, 2, 3) >>> (  Square) # the tuple (1, 2, 3) >>> ()  # empty tuple ()      

How do you implement a tuple that contains a value? The method is a bit peculiar ---- must be comma-even if there is only one value:

>>>, (3) >>> (20+1)>>> 3 * (+/-) 63>>> (20+1 ,      (+)

Tuple function

the function of the tuple function is basically the same as the list function: Take a sequence as a parameter and convert it to a tuple.

>>> tuple ([C-C]) (1, 2, 3) >>> tuple ('ABC')('a'b') C') >>> tuple ((+)) (1, 2, 3)           

Basic Python Tutorial (iv)

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.