A detailed description of the use of lists and tuples in Python

Source: Internet
Author: User
Tags python list
This article mainly introduces the definition and use of Python list and tuple, and analyzes the function, difference, definition and use of the list and tuple in Python in the form of instance, which can be referenced by friends.

The examples in this article describe the definition and use of Python lists and tuples. Share to everyone for your reference, as follows:


#coding =utf8print "can use lists and tuples as normal" arrays ", which can hold any number of arbitrary types of Python objects. Lists and tuples access elements through a numeric index (starting at 0).             Differences between lists and tuples:------------------------------------------------------------------------------------tuples \  List------------------------------------------------------------------------------------list elements with brackets [] \  Tuple elements with parentheses () the number of elements and the value of the element can be changed \ The number of elements and the value of the element cannot be changed------------------------------------------------------------------------------------tuples can see a read-only list. Lists and tuples can be obtained by using the index operator ([]) and the Slice operator ([:]) to get a subset of ' numberlist=[1,2,3,4,5,6,7,8.9,0101,017,0xab]stringlist=[' hello ', "hello World "," ' goddness ' ']mixlist=[12,13.2,01, ' abc ', ' Hello ']numbertouple= (1,2,3,4,5,6,7,8.9,0101,017,0xab) stringtouple= (' Hello ', ' Hello World ', ' ' goddness ') mixtouple= (12,13.2,01, ' abc ', ' Hello ') print "Output the element Of the NumberList by index---------> ", numberlist[0],numberlist[1],numberlist[2],numberlist[-1]print" output the element of the Stringlist by index---------> ", Stringlist[0],stringlist[1],stringliSt[2],stringlist[-1]print "Output the element of the Mixlist by index--------->", Mixlist[0],mixlist[1],mixlist[2], Mixlist[-1]print "Output the element of the Numbertouple by index--------->", Numbertouple[0],numbertouple[1], Numbertouple[2],numbertouple[-1]print "Output the element of the Stringtouple by index--------->", Stringtouple[0], Stringtouple[1],stringtouple[2],stringtouple[-1]print "Output the element of the Mixtouple by index--------->", Mixtouple[0],mixtouple[1],mixtouple[2],mixtouple[-1]print "Output of the element of the numberlist by slice--------->" , numberlist[0:2],numberlist[1:3],numberlist[0:],numberlist[:-1]print "Output the element of the stringlist by Slice---------> ", stringlist[0:1],stringlist[2:3],stringlist[0:],stringlist[:-1]print" output the element of the Mixlist by Slice---------> ", mixlist[0:],mixlist[:1],mixlist[0:2],mixlist[2:-1]print" output the element of the Numbertouple by Slice---------> ", NUMBERTOUPLE[0:2],NUMBERTOUPLE[1:3],NUMBERTOUPLE[2:],numbertouple[:-1]print "Output the element of the stringtouple by slice--------->", Stringtouple[0:2], Stringtouple[1:3],stringtouple[2],stringtouple[-1]print "Output of the element of the mixtouple by slice--------->", Mixtouple[0:],mixtouple[1:3],mixtouple[2],mixtouple[:-1]numberlist[0]=59#numbertouple[0]=56print "Change the Value of numberlist[0] to------------', numberlist[0] #print "Can not change the value of numbertouple[0] to---------- --", numbertouple[0]

Operation Result:

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.