Python learning notes (8) Python list (1), python learning notes

Source: Internet
Author: User

Python learning notes (8) Python list (1), python learning notes

Basic list operations

A list definition list is an object type and a sequence in Python.

Object Type: list

Representation Method: []

The element in the python list can be any type of object.

1 >>> [] # square brackets are also a list 2 [] 3 >>> type ([]) 4 <type 'LIST'> 5 >>> a = [] 6 >>> bool (a) # Use bool () to check whether the list is empty. If it is null, false is returned, null only indicates no object in the list. 7 False 8 >>> a = ["python", 5, 5.6] 9 >>> a10 ['python', 5, 5.6] 11 >>> B = ["python", 5, 5.6, ["python", 5, 5.6] # The element in the python list can be any type of object 12 >>> b13 ['python', 5, 5.6, ['python', 5, 5.6] 14 >>>

Indexes are similar to strings.

Slice extracts some values according to a certain number of returned results, which is similar to the string.

1 >>> a 2 ['python', 5, 5.6] 3 >>> a [0] # retrieve element 4 'python' 5 with an index value of 0 >>> a [1] # retrieve element 6 with an index value of 1 7 >>> a [2] # retrieve elements with an index value of 2 8 5.6 9 >>>> a [0: 2] # retrieve the index value from the Left to 0, 10 ['python', 5] 11 >>> a [: 2] # from the left, all elements before the index value is 2. Principles 12 ['python', 5] 13 >>> a [0:] # Starting from 0 on the left, until the end of 14 ['python', 5, 5.6] 15 >>> a [-1] # Start from the right of the first element 16 5.617 >>> b18 ['python ', 5, 5.6, ['python', 5, 5.6] 19 >>> B [3] # retrieve element 20 ['python ', 5, 5.6] 21 >>> B [3] [0] # 2D list or multidimensional list 22 'python' 23 >>>. index (5) # retrieve the index value of the element 5 in list a 24 125 >>>. index (5.6) # retrieve the index value of the element 5.6 in list a 26 227 >>>. index ("python") # retrieve the index value of the python element in list a 28 029 >>>

Reverse

1. sice ([start], stop, [step]) start value, end value, step size, positive step size, left-to-right number, negative step size, and right-to-left number

2. reversed ()

1 >>> lst = [1, 3, 4, 5, 6] 2 >>> lst [:-1] # Another method of slice sice ([start], stop, [step]) three start values, end values, step 3 [6, 5, 4, 3, 2, 1] 4 >>> lst [0: 4] # The index start value is 0, the end value, the index value is 4, the default step is 1, the slice principle is left included, and the right does not include. Take 1 to 4 5 [1, 2, 3, 4] 6 >>> lst [] # start value 0, end value 4, step 1 7 [1, 2, 3, 4] 8 >>> lst [] # start value is 0, end value 4, step 2 9 [1, 3] 10 >>> lst [4: 1: -1] # The start value is 4, the end value is 1, The step size is-1, the step size is positive, from left to right, The step size is negative, and from right to left. 11 [5, 4, 3] 12 >>> lst [:-1] # Start on the right and end on the left. The step size is-113 [6, 5, 4, 3, 2, 1] 14 >>> lst [:-2] # Start on the right and end on the left. The step size is-215 [6, 4, 2] 16 >>> list (reversed (lst) # Another way of inversion reversed () 17 [6, 5, 4, 3, 2, 1] 18 >>>

A list is a sequence.

There are these operations len (), +, *, max (), min (), cmp ()

 1 >>> len(lst) 2 6 3 >>> a 4 ['python', 5, 5.6] 5 >>> lst + a 6 [1, 2, 3, 4, 5, 6, 'python', 5, 5.6] 7 >>> a *3 8 ['python', 5, 5.6, 'python', 5, 5.6, 'python', 5, 5.6] 9 >>> max(lst)10 611 >>> min(lst)12 113 >>> cmp(a,lst)14 115 >>>

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.