List:
? In Python, the list is represented by square brackets ([]), and the elements are separated by commas.
? Python provides a special syntax for accessing the last list element. You can have Python return the last list element by specifying the index as 1. This Convention also applies to other negative indexes, for example, index 2 returns the second-to-last list element, index-3 returns the third-lowest list element, and so on.
? Method Append () Adds an element to the end of the list without affecting all other elements in the list:
? Use the Insert () method to add a new element anywhere in the list. To do this, you need to specify the index and value of the new element.
Example: Motorcycles.insert (0, ' Ducati ')
? You can use the DEL statement if you know the position of the element you want to delete in the list.
Example: Del motorcycles[0]
? The method pop () removes the element at the end of the list and allows you to continue to use it.
The list is like a stack, and deleting the element at the end of the list is equivalent to ejecting the top element.
In fact, you can use POP () to delete an element from anywhere in the list, just specify the index of the element you want to delete in parentheses.
? Sometimes, you don't know where the value you want to remove from the list is located. If you only know the value of the element you want to delete, you can use the method remove (). Note the Remove () method removes only the first specified value
? Method sort () to sort the list in a permanent order (in alphabetical order)
You can also arrange the list elements in reverse alphabetical order, simply passing the parameters to the sort () method reverse=true
? Method Sorted () to sort the list temporarily
When the function sorted () is called, the order of the list elements is not changed. If you want to display the list in the reverse order of alphabetical order, you can also pass parameters to the function sorted () reverse=true
? To reverse the order of the list elements, you can use the method reverse ()
Method reverse () permanently modifies the order in which the list elements are arranged, but can revert to the original order at any time, just call reverse () again on the list.
? Use the function Len () to quickly learn the length of a list
? Traverse List
? Magicians = [' Alice ', ' David ', ' Carolina ']
? For magician in Magicians:
? Print (magician)
? For value in range (1,5):
Print (value)//printing number 1-4
? Numbers = List (range (1,6))
Print (numbers)//store 1-5 of numbers in the list
? You can also specify the step size when you use the function range (). For example, the following code prints an even number within 1~10:
Even_numbers = List (range (2,11,2))
Print (even_numbers)
? Method min (), Max (), sum (), find the minimum, maximum, and sum of the list of numbers.
To copy a list, you create a slice that contains the entire list by omitting both the start index and the Terminate index ([:]).
python--List