List and tuple of Python basics

Source: Internet
Author: User
Tags stdin

One of the data types built into the Listpython is the list: lists. A list is an ordered set of elements that can be added and removed at any time.   For example, by listing the names of all the classmates in the class, you can use a list to indicate: >>> classmates = [' Michael ', ' Bob ', ' Tracy ']>>> classmates [' Michael ', ' Bob ', ' Tracy '] variable classmates is a list. Use the Len () function to get the number of list elements: >>> Len (classmates) 3 uses an index to access the elements of each position in the list, remembering that the index is starting at 0: >>> Classmates[0] ' Michael ' >>> classmates[1] ' Bob ' >>> classmates[2] ' Tracy ' >>> classmates[3] Traceback (most recent):  File "<stdin>", line 1, in <module>indexerror:list index out of rang e when the index is out of range, Python will report a indexerror error, 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 the index location, you can also use 1 to index, directly get the last element: >>> Classmates[-1] ' Tracy ' and so on, you can get the bottom 2nd, the bottom 3rd:  >>> 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 bottom 4th is the more The world.  list is aChange the ordered table, so that you can append elements to the list to the end of: >>> classmates.append (' Adam ') >>> classmates[' Michael ', ' Bob ', ' Tracy ', ' Adam ') can also insert elements into the specified position, such as the position of index number 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 assign a value directly to the corresponding index position: >>> CLASSMATES[1] = ' Sarah ' >>> classmates[' Michael ', ' Sarah ', the data type of the elements inside the ' Tracy ']list can also be different, such as: 
>>> L = [‘Apple‘, 123, True]
The list element can also be another list, such as: >>> s = [' Python ', ' Java ', [' asp ', ' php '], ' scheme ']>>> len (s) 4 to note that S has only 4 elements, of which s[2 ] is a list, if it is easier to understand: >>> p = [' asp ', ' php ']>>> s = [' Python ', ' Java ', p, ' scheme '] to get ' php ' can write p[1] or s [2] [1], so s can be regarded as a two-dimensional array, similar to three-dimensional, four-dimension ... arrays, but rarely used. If a list does not have an element, it is an empty list with a length of 0: >>> L = []>>> len (l) 0tuple Another ordered list is called tuple: 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:
>>> classmates = (‘Michael‘, ‘Bob‘, ‘Tracy‘)
Now, classmates this tuple cannot be changed, and it does not have a append (), insert () method. Other methods of acquiring elements are the same as lists, and you can use classmates[0],classmates[-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.  tuple traps: When you define a tuple, the elements of a tuple must be determined when defined, such as: >>> t = (1, 2) >>> T (1, 2) If you want to define an empty tuple, you can write (): >>> t = () >>> t () but, to define a tuple with only 1 elements, if you define: >>> t = (1 >>> T1 defined 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 disambiguate: >>> t = (1,) >>> T (1,) Python will also add a comma when displaying only a tuple of 1 elements , lest you misunderstand the parentheses in the sense of mathematical calculation.   finally look at a "variable" tuple: >>> t = (' A ', ' B ', [' A ', ' B ']) >>> t[2][0] = ' X ' >>> t[2][1] = ' Y ' &G T;>> T (' A ', ' B ', [' X ', ' Y ']) when this tuple is defined, there are 3 elements, namely ' a ', ' B ', and a list. Doesn't it mean that once a tuple is defined, it's immutable? Why did you change it later?   Don't worry, let's take a look at the definition when the tuple contains 3 elements:   when we change the list element ' A ' and ' B ' to ' X ' and ' Y ', the tuple becomes:   on the surface, The elements of a tuple do change, but not the elements of a tuple., but the elements of the 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. That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable!   Understand the "point to invariant", to create a content is also the same as the tuple how to do? It is important to ensure that each element of a tuple cannot be changed.   Practice please use the index to remove the specified element from the list below:  #-*-coding:utf-8-*- l = [    [' Apple ', ' Google ', ' Microsoft '],& nbsp;   [' Java ', ' Python ', ' Ruby ', ' PHP '],    [' Adam ', ' Bart ', ' Lisa ']]   Run summary list and tuple are Python's built-in ordered set, one variable, one immutable. Choose to use them as needed.  

List and tuples of Python basics

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.