Python the next day, list and tuple

Source: Internet
Author: User

Concept: List is a collection, and is a mutable set, a tuple is a set of tuples, immutable collections. A 1.Python built-in data type is a list: lists. A list is an ordered set of elements that can be added and removed at any time.

Main points of knowledge: Initialize set param = [' Jerry ', Tom ', ' Lily ', ' Lucy ']; Query the specified location according to subscript Param[0];


For example, by listing the names of all the classmates in the class, you can use a list to indicate:


The variable classmates is a list. Use the Len () function to get the number of list elements


Use an index to access the elements of each location in the list, remembering that the index starts at 0:


Python will report a indexerror error when the index is out of range, so make sure the index is not out of bounds, and remember that the index of the last element is Len (classmates)-1.


If you want to take the last element, in addition to calculating the index location, you can also use 1 to index, get the last element directly:


Of course, if you cross the border, you will get an error.

The list is a mutable, ordered table, so you can append the element to the end of the list. Append to an array by using. Append ()


You can also insert elements into the specified position, such as the index number 1, where it is mainly through. Insert ():


To delete the element at the end of the list, using the pop() method, this method removes the last element of the collection:


When you need to delete the element at the specified position, use pop (i) to enter the subscript of the element:


To replace an element with another element, you can assign a value directly to the corresponding index position, and the array in Java is changed in the same way:


The data types of the elements in the list can also be different, such as:


Of course, you can also insert an array:


We want to take out Apple and need to get it through classmates[4][0]:


It can be seen that Python also supports two-dimensional arrays, three-dimensional arrays and other multidimensional degrees of the group.

----------------------------------------------------Sheng Tian Antimeron split line-------------------------------------------------------------- ---

A tuple is an ordered list called tuple. Tuple and list are very similar, but once the tuple is initialized, it cannot be modified, for example, the name of the classmate is also listed:


Now, the tuple cannot change, nor does it have a append (), insert () method. Other methods of acquiring elements are the same as lists, and you can use students[0],students[-1] normally, but you cannot assign them to another element.
What is the meaning of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, you can use a tuple instead of a list as much as possible.
The trap of a tuple: when you define a tuple, the elements of the tuple must be determined when you define it, and it is useless to change the collection.


If you want to define an empty tuple, you can write ():


However, to define a tuple with only 1 elements, if you define this:


The definition is not a tuple, is 1 this number! This is because parentheses () can represent both tuples and parentheses in mathematical formulas, which creates ambiguity, so Python rules that, in this case, the parentheses are calculated and the result is naturally 1.
Therefore, only 1 elements of a tuple definition must be added with a comma, to eliminate ambiguity:


Python will also add a comma when displaying a tuple of only 1 elements, lest you misunderstand the parentheses in the mathematical sense.
Finally, let's look at a "mutable" tuple:


We first defined a list of arr, added two elements to it, and then added the list to arr, where the elements of the tuple could not be changed, but we succeeded in changing the elements of arr 0 to Mike when we changed it, because on the surface, The elements of a tuple do change, but they are not the elements of a tuple, but the elements of a list. 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.

After understanding "point to invariant", how do you create a tuple that does not change the content? It is important to ensure that every element of the tuple itself cannot be changed.

List and tuple are Python's built-in ordered set, one variable, one immutable. Choose to use them as needed.

Python the next day, list and tuple

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: 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.