Learn about list and tuple in Python

Source: Internet
Author: User

Learn about list and tuple in Python

List Definition

The sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.

The list is the most commonly used Python data type, and it can be used as asquare bracketsThe comma-separated values within the value appear. The data items of the list do not need to have the same type. Create a list by enclosing separate data items separated by commas in square brackets.
1 list=['du','dpsl', 123,5926]2 print list[2]3 list[2]=3214print  list5 del  6print list

The result will be:

123['du'dpsl', 321, 5926][' DPSL ', 321, 5926]

List SliceWhat if you want to take the last one? Use-1Represents the last element. Similarly, the second-to-last use-2 means that the penultimate-3 means that the penultimate fourth uses-4.
1list=['123','Dudu','Iiiu']2 PrintList3 PrintList[-1]4 PrintList[-2]5List.insert (0,'du')#Adding an element, the first parameter represents an increase to the first position. The pop () method always deletes the last element of the list, and it also returns the element. 6 PrintList

The result is:

> ['123'dudu'iiiu'] iiiududu['du'123'Dudu  "'iiiu')
     Tuple tuples

A python tuple is similar to a list, except that the elements of the tuple cannot be modified (there is an example later proving that it is also "mutable"). Tuples use parentheses , and the list uses square brackets.

Tuple creation is simple, just add elements in parentheses and separate them with commas.

Tuples can use the subscript index to access the values in a tuple you can use the DEL statement to delete an entire tuple, but it is not allowed to delete elements in tuples. TupleNoThe Append () method, nor the insert () and Pop () methods. element values in tuples are not allowed to be modified, but we can concatenate combinations of tuples. Like what:
1Tup1= ('123', 31)2 PrintTup13Tup2= ('Dudu','du', 123,321,'zzz')4 Printtup25tup3=tup1+tup26 PrintTup37 8 Print "T2:", Tup2[2:4]9 Ten Print "=======" One  A delTup1 - PrintTup1#there is no tup1 at this time. 

The result is:

> ('123', 31)('Dudu','du', 123, 321,'zzz')('123', 31,'Dudu','du', 123, 321,'zzz') T2: (123, 321)=======>Traceback (most recent): File"/demo/test.py", line 19,inch     PrintTup1nameerror:name'Tup1'  is  notDefined

A special place:

Because () both can represent a tuple and can be used as parentheses to indicate the priority of the operation, the result (1) is calculated by the Python interpreter as result 1, which results in not being a tuple, but an integer 1.

It is because the tuple with the () definition of a single element is ambiguous, so Python specifies that the cell tuple should have a comma ",".

tup1= (121,)print tup1
> (121,)

The above is written to the tuple is immutable. So, is the tuple really immutable? See Example:
1T = ('du','Dudu', ['A','B'])2 PrintT3L=T[2]#gets the element in the tuple that has an index of 2, which is the list. 4l[0]='XX'  #modifies the first element in a list. 5l[1]='YYY'  #modifies the first element in a list. 6 PrintT

Look at the results:

 > ( du   ", "  dudu  , ["   A  ,    B   " ]) (  '     yyy   ' ] ' 
Obviously has changed, then explained:
on the surface, the elements of a tuple do change, but in fact it is not a tuple element, but a list element. The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of a tuple is directed to never change. That point to ' a', it cannot be changed to point to 'b', pointing to a list, can not be changed to point to other objects, But the list itself is mutable!

Thank you: Liu Co official websiteMu class netHui Zhi Network

Learn about list and tuple in Python

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.