The usage and distinction of list and tuple in Python __python

Source: Internet
Author: User
Tags in python

1, list-list

A list is an ordered set of elements that you can add and delete at any time.

Lists all elements in the array num:

Access the elements in the list, the index starts at 0, 0 is the first element, and when the index is out of range (this case index is greater than 9 o'clock), the index cannot cross, and the last element's index is Len (num)-1 (Len ()). You guess)

If you want to take the last element, in addition to calculating the index position, you can also use-1 to index directly to the last element (bottom one)

Use append () to add elements at the end of the list

Use Insert () to add element inserts to the specified location


Use Pop () to delete the last element of the list

Using Pop (i) where I is the index number, you can delete the element at the specified location

To replace an element, you can assign a value directly to the element that corresponds to the index number position

The element types in the list can be different (as can be seen from the example above), and not only that, but also its elements can be another list

2, tuple

Tuple is a sequence table, which is very similar to the list, but (but not all of it is nonsense)

Tuple can not be modified once initialized, and there is no append () of these methods to get the element but not assign it to another element


If you want to define an empty tuple can be written ()

If you define 1 elements. (1) write like this.

NoNoNo

Define an element to do this (1,) to add a comma oh

(1) This represents the number 1, is the number of


Tuple how to change the elements inside.


It's not that tuple can change, but that the elements in the list that tuple point to can change

Tuple ()

Lis T with []



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.