Python Learning note-day2-list

Source: Internet
Author: User

Today, Python's various data structures, such as list, dict,tuple,string and other common data structures, as well as their common methods and methods of use, are mentioned.

A list of lists

1, the definition of the list

Or:

New_list = [] #空列表

2. Print List

Use the index value to refer to a specific value in the list, as follows

3, list how to add elements

Insert (0, ' Shanghai '), which is the insertion of elements in the 0 position Shanghai, the list of existing elements corresponding to the backward position

Results:

4. Deleting elements

(1) The first method: Using Pop (index value), the example is equivalent to the 0 index position of the element is deleted,

# Cities.pop (3)  #索引不能越界, or you will get an error.

Results:

(2) The second method of deleting elements

(3) The third method, using Del to delete the specified element

5. Clear the list

6. Modify the elements in the list

7. Querying list elements

List method:

List.index (' specified element ')

List.count (' specified element ')

8. List reversal

List method: List.reverse (), the return value is None

9. Sort the list

10. List Merge

11. How multidimensional arrays are given the elements in the list

By the level of the list, one layer at a level to take the value can

12, the length of the list

Use Len (), Python's built-in function, for list,string and more.

13. List Operation Example Program

Example 1:

Second, the list of slices

1, the use of slices

If range () is used, it is also Gu Tou regardless of tail:

2, the step use of slices

3. The difference between list.reverse () and list[::-1]

4, different slices of the same method, the same output results

Three, the slice of the string, the above section also applies to the string

Iv. Cycle of the list

1. Iterate through the list using loops

2. Looping through strings using loops

3. Iterate through two-dimensional arrays using loops

Example:

Python Learning note-day2-list

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.