Python: List Operations Summary

Source: Internet
Author: User

One, create a list

Just enclose the comma-delimited data items in square brackets.

1 list1=['physics','chemistry', 1997,2000]  2 list2=[1,2,3,4,5,6,7]

[Note]: 1. Data items in the list do not need to have the same data type

2. The index of the list starts at 0, and the list can be intercepted, combined

Second, access the value in the list

Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters

1list1=['Physics','Chemistry', 1997,2000]2list2=[1,2,3,4,5,6,7]3 4 Print('List1[0]:', List1[0])#output: list1[0]:p hysics5 Print('List2[1:5]:', List2[1:5])#output: [2,3,4,5]

Third, List script operators

P Ython-expression

Results Describe
Len ([+]) 3 Length
[1,2,3]+[4,5,6] [1,2,3,4,5,6] Combination
[' hi! '] * [' hi! ', ' hi! ', ' hi! ', ' hi! '] Repeat
3 in [All in all] True

Whether the element exists in the list

For x in [All-in-A-Z]: print (x) The

Iteration

Iv. List interception

The hypothetical list is as follows:

list=[1,2,3,4,5,6,7,8,9]

Operation:

Python expressions Results Describe
LIST[2] 3 Read the third element in the list
List[-2] 8 Reads the second-to-last element in a list
List[1:4] [2,3,4] Intercepting an element from subscript 1 to subscript 4 (excluding elements of subscript 4), the result is a list
List[1:8:2] [2,4,6,8] Intercept elements starting from subscript 1 to subscript 8 (excluding elements of subscript 8), step 2, and result as a list
List[:3] [A] An element that is truncated from the first element of the list to the subscript 3 (excluding the element with subscript 3), resulting in a list
List[5:] [6,7,8,9] Intercepts an element from subscript 5 to the end of the list, and the result is a list
list[:] [1,2,3,4,5,6,7,8,9] Copy the entire list

Python: List Operations Summary

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.