Python basic tutorial-list and tuple

Source: Internet
Author: User

List

One of the data types built into Python is the list: lists. A list is an ordered set of elements that can be added and removed at any time.

Like what:

>>> classmates = [' Michael ', ' Bob ', ' Tracy ']
>>> Classmates
[' Michael ', ' Bob ', ' Tracy ']
>>> Len (Classmates)
3

An index to access the elements of each location in the list, starting with 0:

>>> classmates[0]'Michael'>>> classmates[3]traceback ( Most recent call last):  '<pyshell#4>' in <module>     classmates[3]indexerror:list index out of range

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

>>> classmates[-1]'Tracy'

And so on, you can get the bottom 2nd, the bottom 3rd one:

>>> classmates[-2]'Bob'>>> classmates[-3]'  Michael'>>> classmates[-4]traceback (most recent)  :"  <pyshell#10>" in <module>    classmates[-4] Indexerror:list index out of range

List is a mutable, ordered table, so you can append an element to the end of the list:

>>> classmates.append ("A")>>> classmates['  Michael'Bob'Tracy '  'A']

You can also insert elements into the specified position, such as the index number 1:

>>>classmates['Michael','Bob','Tracy','A']>>> Classmates.insert (1,"B")>>>classmates['Michael','B','Bob','Tracy','A']

To delete the element at the end of the list, use the method of pop (i) where I is the index position:

>>>classmates['Michael','B','Bob','Tracy','A']>>> Classmates.pop (1)'B'>>>classmates['Michael','Bob','Tracy','A']

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

>>>classmates['Michael','Bob','Tracy','A']>>> Classmates[3] ="Sarah">>>classmates['Michael','Bob','Tracy','Sarah']

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

>>> L = ['a', 45,true,3.45]>>> l['A  ', A, True, 3.45]

The elements of a list can also contain another list, such as:

>>> M = ['d', L]>>> m['d ', ['A', ' A ', ' True, 3.45]]

Note that M has only two elements, m[1] is also a list:

>>> len (M)2>>> len (m[1])4

If a list does not have an element, it is an empty list with a length of 0:

>>> n = []>>> len (n) 0
Tuple

Another ordered list in Python is called tuples: tuple. Tuple and list are very similar, but the tuple cannot be modified once initialized:

>>> classmates = ('A','B','C  ')>>> classmates ('A'B ' ' C ')

Now, classmates this tuple cannot be changed, and it does not have a append (), insert () method. Other methods to get the element are the same as the list, or you can use classmates[0],classmates[-1], but you cannot assign a value to another element.

So, what is the meaning of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, use a tuple instead of list to try using a tuple.

The trap of a tuple: when you define a tuple, the elements of a tuple must be determined when defined, such as:

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

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

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

However, to define a tuple of only 1 elements, if you define as follows:

>>> t = (1)>>> T1

So you're not defining a tuple, which is 1, because parentheses () can represent both tuples and parentheses in mathematical formulas.

This creates ambiguity, so Python stipulates that, in this case, the parentheses are calculated.

Therefore, only one element of the tuple definition must be added with a comma "," to disambiguate:

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

When Python displays a tuple of only one element, it also adds a comma "," so that you don't misunderstand the parentheses in the mathematical sense.

"Variable" tuple:

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

When this tuple is defined, there are three elements, on the surface, the elements of a tuple do change, but not the elements of a tuple, but the elements of the list. A tuple at the beginning of the list is not changed to another list, so the so-called immutable tuple is said that each element of a tuple points to never change, such as pointing to a list, can not be changed to point to other objects, but this list is variable.

Summarize

The list and tuple are the ordered collections built into Python, which are mutable and immutable.

Python basic tutorial-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.