Python-list and tuple

Source: Internet
Author: User

A list is a built-in data type that is an ordered set of elements that can be added and removed at any time.

classmates = [' Michael ', ' Bob ', ' Tracy '];

1, Len (classmates): Returns the number of list elements 3

2. Index: classmates[0] ' Machael '

Get the last element: Classmates[len-1] or classmates[-1]

The same as the bottom 2nd: classmates[len-2] or classmates[-2]

3. Append element: Append to end Classmates.append (' Adam ')

Insert element at specified location Classmates.insert (1, ' Jack ')

4. Delete element: Delete last element Classmates.pop () returns the value of a pop

Removes the element at the specified location Classmates.pop (1)

5. Replacement element: classmates[1] = ' Sarah '

6. The data type of the elements in the list can be different: L = [' Apple ', 123,true]

7, the elements in the list can also be another list:s = [' Python ', ' Java ', [' asp ', ' php '], ' scheme ']

Len (s) 4 S can be viewed as a two-dimensional array, accessing ' ASP ' s[2][0]

8, if L = [], then Len (L) 0

A tuple tuple is similar to a list, but cannot be modified once the tuple is initialized

Classmates = (' Michael ', ' Bob ', ' Tracy ')

At this time classmates this tuple can not be changed, that is, can not be inserted, deleted, only read, and the same as list.

1, t = (+)

2, T = ()

3, T = (1,)

Python-list and tuple

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.