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