Use of Python lists

Source: Internet
Author: User

List definition: is made up of specific elements which can make anything

List names=[' Li nei ', ' Han Meimei ', ' Lily '

Access element: List name [ordinal] (sequence number starting from 0) Names[0], if the sequence number is -1,-2, it points to the last element and the second penultimate element.

Modifying list elements: names[0]= "Wang"

add element: Names.append (' Yang ') added to the end

Insert element: Names.insert (0, ' Liu ')

Delete element: (1) del names[0]0 represents its index

(2) names.pop (0) Delete the last value of the list (pop if you need to continue using it)

(3) names.remove ("Li nei") deletes elements based on values

Sort:

(1) sort () permanent sorting in alphabetical and numeric order

(2) sorted (names) retains its original order and is only temporarily present.

Invert: reverse () Reverses the order of the list

List length: Len (names)

For loop for name in names: takes an element from names, stored in the variable name range (1,5) produces 1~4

Generate a list of numbers: List (range (1,6,2)) 2 is the step size

Statistical calculation of the list: (1) min (names) min

(2) max (names)

(3) sum (names)

List resolution: Nums=[num for NUM in range (1,11)]

Slice: Names[0:2]

Tuples: are immutable during program operation

Sizes= (15,20)

Although you cannot modify the elements of a tuple, you can assign values to tuples. Sizes= (20,25)

Use of Python lists

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.