Non container data type : A variable can only hold one data
Container data type : A variable can hold multiple data at the same time
list --------- >(list)
Dictionaries --------->(dict)
Tuples --------->(tuple)
aggregate ---------->(set)
character string ------>(str)
list = []
[] As a sign of the list , Multiple elements are separated by commas :[ Elements 1, Elements 2, ... , Elements n]Query of list elements :
Look up a single : Get an element one by one
list [ Subscript ] -----> Gets the element corresponding to the specified subscript list : Any expression that results in a list , Usually use the variable that saves the list [] : Fixed grammar Subscript : A subscript is also called an index , It is the position information of elements in an ordered sequence len( list ) : Get the number of elements in the list ( Get the length of the list )section : Get some elements
list [start : end : step] Start subscript , End subscript : Subscript value ; Used to determine the effective range of slices :[start, end); start The default is the first element in the direction , end The default is the last element in the direction step : Positive or negative integers ; Use the sign to determine the acquisition direction ; The absolute value is used to determine the acquisition method , The default is 1:: Fixed writing
- If the direction corresponding to the step size is inconsistent with the direction from the element corresponding to the start subscript to the element corresponding to the end subscript , Slice invalid , The result is an empty list
- Slice valid , First determine the effective range
([start,end)), Within the effective range, the element is obtained according to the absolute value of the step according to the direction of the step , Common new lists
list [start : end] — Omit step size , The default step size is 1 list [ : end : step] — If the step size is just , from 0 Start ; If the step size is negative , from -1 Start . list [start : : step] — According to the direction of the step to the end , Step size is positive , To the last element , Step size is negative , To the first element Traverse : One by one , Take it out
1. Method 1 : Get elements directly
for Variable in list:
pass
# Variables in turn get the elements in the list
2. Method 2 : --- Get the subscript first , Get elements with subscripts
for Variable in range(len(list)):
pass
# The variable gets the subscript of the element at a time
3. Method 3 :---- Enumeration , Directly fetch the element in the list and its corresponding index
for Subscript , Elements in enumerate(list):
pass
# Get both list elements and Subscripts
The addition of list elements :
list .appen( Elements ): Add the specified element at the end of the specified list list .insert( Subscript , Elements ) : Insert the specified data at the specified position in the specified list Deletion of list elements :
list .clear(): clear list list .remove( Elements ) : Always delete the specified data in the specified list ( Element does not exist will report an error ; If there is more than one data , Which of the first ones will be deleted )del list [ Subscript ] : Delete the element with the specified subscript in the list list .pop( Indexes ): Extract the elements of the specified index in the list list .pop(): Take out the last element Modification of list elements : list [ Subscript ] = The new element : Modify the list elements