A list of Python learning

Source: Internet
Author: User

In Python, the list is equivalent to an array in other high-level programming languages, summarizing the basics of the list:

  To access a list element:

  In Python , the index of the first list element is 0, not 1. This is true in most programming languages, which is related to the underlying implementation of list operations.

  To edit an element in a list:

 Edits usually include additions, deletions, and other actions.

First say add an element, adding an element at the end of the list, just like in other languages, the Append () function, appended to the end of the list.

Motorcycles = ['Honda'Yamaha'Suzuki ' ]print(motorcycles) motorcycles.append ('Ducati' ) )print(motorcycles)

Insert an element in the list, and use the method Insert () to add a new element anywhere in the list. To do this, you specify the index and value of the new element.

Motorcycles = ['Honda'Yamaha'Suzuki '  'Ducati')print

The delete operation is the Del () function, using del motorcycles[0] to remove the No. 0-bit element.

Another function is a pop () function, like a stack in a data structure, pops a value from the end of the list, and if you want to use this element after you delete the element, you can consider the deletion method. At the same time, this pop () function can also pop up any element, in parentheses can be added a position parameter, such as first_owned = motorcycles.pop (0) , that is, the No. 0 position of the popup element, The disease is assigned to another variable for subsequent use.

The Remove () function removes the element motorcycles.remove (' Ducati ') based on the element value.

  Sort:

  Sorting is also an important operation of the list, and the sort () function is permanently sorted.

Cars = ['bmw'Audi'Toyota ' 'Subaru']cars.sort ()print(Cars)

The results of the observations indicate that the sorting results are sorted alphabetically.

  Use the function sorted () to sort the list temporarily, leaving the list elements in their original order and rendering them in a specific order.

  To reverse the order of the list elements, you can use the method reverse (), such as cars.reverse () .

  Problem with the length of the list:

If a list name is cars, the length is Len (cars).

Traversing the list:

Magicians = ['Alice'David'Carolina  ' for in magicians:     print(magician)

In this chapter, we learn what the list is and how to use it, how to define the list and how to delete it, how to sort the list in perpetuity, how to sort the list, and how to determine the length of the list.

  

A list of Python learning

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.