Describes how to use the list and ancestor in Python, and how to use python.

Source: Internet
Author: User

Describes how to use the list and ancestor in Python, and how to use python.

List

A built-in data type in Python is list: list. List is an ordered set that allows you to add and delete elements at any time.

For example, you can use a list to list the names of all the students in the class:

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

The variable classmates is a list. Use the len () function to obtain the number of list elements:

>>> len(classmates)3

Use indexes to access the elements at each position in the list. Remember that the index starts from 0:

>>> classmates[0]'Michael'>>> classmates[1]'Bob'>>> classmates[2]'Tracy'>>> classmates[3]Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: list index out of range

When the index is out of the range, Python will report an IndexError. Therefore, make sure that the index does not cross-border. Remember that the index of the last element is len (classmates)-1.

To obtain the last element, in addition to the index location, you can use-1 as the index to directly obtain the last element:

>>> classmates[-1]'Tracy'

And so on, you can get 2nd to the last and 3rd to the last:

>>> classmates[-2]'Bob'>>> classmates[-3]'Michael'>>> classmates[-4]Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: list index out of range

Of course, the last 4th will be out of the border.

List is a variable ordered table. Therefore, you can append an element to the end of the list:

>>> classmates.append('Adam')>>> classmates['Michael', 'Bob', 'Tracy', 'Adam']

You can also insert an element to a specified position, for example, the index number is 1:

>>> classmates.insert(1, 'Jack')>>> classmates['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']

To delete the element at the end of the list, use the pop () method:

>>> classmates.pop()'Adam'>>> classmates['Michael', 'Jack', 'Bob', 'Tracy']

To delete the element at the specified position, use the pop (I) method, where I is the index position:

>>> classmates.pop(1)'Jack'>>> classmates['Michael', 'Bob', 'Tracy']

To replace an element with another element, you can directly assign a value to the corresponding index location:

>>> classmates[1] = 'Sarah'>>> classmates['Michael', 'Sarah', 'Tracy']

The Data Types of the elements in the list can also be different, for example:

>>> L = ['Apple', 123, True]

The list element can also be another list, for example:

>>> s = ['python', 'java', ['asp', 'php'], 'scheme']>>> len(s)4

Note that s has only four elements, of which s [2] is a list. It is easier to understand if it is separated and written:

>>> p = ['asp', 'php']>>> s = ['python', 'java', p, 'scheme']

To get 'php', you can write p [1] Or s [2] [1]. Therefore, s can be regarded as a two-dimensional array, similar to 3D and four-dimensional ...... Array, but rarely used.

If no element exists in a list, it is an empty list with a length of 0:

>>> L = []>>> len(L)0

Tuple

Another ordered list is tuple. Tuple and list are very similar, but tuple cannot be modified once initialized. For example, they are also used to list the names of students:

>>> classmates = ('Michael', 'Bob', 'Tracy')

Now, the tuple of classmates cannot be changed, and it does not have methods such as append () and insert. Other methods for retrieving elements are the same as those for listing. You can normally use classmates [0], classmates [-1], but cannot assign values to other elements.

What is the significance of an immutable tuple? Because tuple is immutable, the code is safer. If possible, use tuple instead of list.

Tuple trap: When you define a tuple, The tuple elements must be determined. For example:

>>> t = (1, 2)>>> t(1, 2)

To define an empty tuple, you can write it ():

>>> t = ()>>> t()

However, to define a tuple with only one element, if you define it as follows:

>>> t = (1)>>> t1

Tuple is not defined, it is the number of 1! This is because parentheses () can represent both tuple and parentheses in the mathematical formula, which leads to ambiguity. Therefore, Python stipulates that, in this case, parentheses are used for calculation, the calculation result is 1.

Therefore, when defining a tuple with only one element, you must add a comma to eliminate ambiguity:

>>> t = (1,)>>> t(1,)

When Python displays a tuple with only one element, it also adds a comma to avoid misunderstanding as a square brackets in the mathematical sense.

Finally, let's look at a "variable" tuple:

>>> t = ('a', 'b', ['A', 'B'])>>> t[2][0] = 'X'>>> t[2][1] = 'Y'>>> t('a', 'b', ['X', 'Y'])

This tuple defines three elements: 'A', 'B', and a list. Doesn't tuple be immutable once defined? Why have they changed?

Don't worry. Let's take a look at the three elements that tuple contains when defining:

When we change the 'A' and 'B' elements of the list to 'X' and 'y', tuple is changed:

On the surface, the tuple elements have indeed changed, but in fact they are not the tuple elements, but the list elements. The list to which tuple points at the beginning is not changed to another list. Therefore, tuple's so-called "unchanged" means that every element of tuple will always point to it. That is, if you point to 'A', you cannot change it to 'B' or to a list. You cannot change it to another object, but the list itself is variable!

After "pointing to unchanged", how does one create a tuple with unchanged content? Therefore, each element of the tuple cannot be changed.
Summary

List and tuple are Python's built-in ordered sets. They are a variable and an immutable set. Use them as needed.

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.