Introduction to "Python Programming: Getting Started to practice" Chapter3 list

Source: Internet
Author: User

Introduction to the Chapter3 list
3.1 What is a list?
A list is a series of elements that are arranged in a particular order.
bicycle = [' Trek ', ' Cannondale ']
Print Bicycle

3.1.1 Accessing list elements
Print (Bicyle[0])
3.1.2 Index starting from 0 instead of 1
Python provides a special syntax for accessing a list element. You can have Python return the last list element by specifying the index as 1.
Print (Bocycles[-1])
Index-2: Return to the penultimate one ...
3.1.3 using each value in the list

3.2 Modifying, adding and deleting elements
3.2.1 Modifying list elements
Array = [' One ', ' both ', ' three ']
Array[0] = ' first '
3.2.2 Adding elements to a list
1. Add elements at the end of the list
Array.append (' four ')
2. Inserting elements into a list
Array.insert (0, ' insert0 ')
3.2.3 removing elements from the list
1. Delete an element using the DEL statement
Del Array[0]
2. Use the method Pop () to delete an element
The method pop () removes the element at the end of the list and allows you to continue to use it.
Print (Array.pop ())
3. The element at any point in the pop-up list
Second = Array.pop (1)
4. Deleting elements based on values
Array.remove (' both ')

3.3 Organization List
3.3.1 Use method Sort () to sort the list in a permanent order
Sort () Ascending
Sort (reverse = True) Descending
3.3.2 Use function sorted () to sort a list temporarily
3.3.3 the Print List backwards
Array.reverse ()
Print (array)
Method reverse () permanently modifies the order of the list elements, but can revert back to the original ordering order at any time, and only need to call Reserse () on the list again
3.3.4 determining the length of a list
Len (Array)
3.4 Avoid errors when using lists

Introduction to "Python Programming: Getting Started to practice" Chapter3 list

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.