# Use of lists # list (list) The definition of -- It's a data type # 1. use [] Brackets to define , Use... Between data # 2. Orderly , The data is repeatable # 3. Take value through subscript index , from 0 Start # 4. Is the most frequently used data type list_my = [] # It's an empty list There was no one in the queue print(list_my) # You can put any type of data , Can be repeated list_2 = [' Heard of ',' Apple ',' Three little ',' stone ',' neighborhood ',True,123,0.22] # List operation - Reading data ( Yes 2 Methods ) # 1. list ( Indexes ) Take value through index # The value is obtained by subscript : List name ( Subscript indices ) print( list_2[2]) # Take value through subscript index , from 0 Start # 2. List name .index( data ) Get the index of the first occurrence of data # Get his subscript by value : List name .index( data ) print(list_2.index(' Heard of ')) # Use index Function to get the data location # Immutable type : Numbers , Boolean value , character string # list : Is a variable type , Can increase the , Deleting , Modifiable # List operation - Add data ( Yes 3 Methods ) # 1. The new data , The default append is at the end . # Format : List name .append( data ) This is the most common way list_2.append(' A little down and out ') # Add new data at the end print(list_2) # 2. Jump in line to add # Format : List name .insert( Indexes , value ) list_2.insert(0,' uncle ') # Add to the original subscript by queue jumping 0 The location of print(list_2) # 3.extend: Will list 2 Merge to list 1 # Format : list 1.extend( list 2) new_list = [' disinfect ','henry','tohu'] # New list list_2.extend(new_list) # use extend Will list 2 Merge to list 1 print(list_2) # List operation - Modifying data # Find the index , Modify the value corresponding to the index index = list_2.index(' A little down and out ') # use index To find the location , list_2[index] = ' Grow up fast ' # Then change the value print(list_2) # print(list_2[14]) # Traceback (most recent call last) Consciousness is out of range # List operation - Delete data ( There are four ways ) # Delete data at a certain location , From left to right # 1. list .remove( data ) Delete the formulation data that appears for the first time in the list list_2.remove(True) # Delete... From the list True This data Only one can be deleted at a time print(list_2) # 2. del list [ Indexes ] Delete the data of an index in the list del list_2[6] # Delete the data at the current index location Only one can be deleted at a time print(list_2) # 3. list .pop() Delete the data at the end of the list list_2.pop() # The last data will be deleted print(list_2) # 4. list .clear() Clear list data # list_2.clear() # All data will be cleared # print(list_2) # Print out a null value # Get the length of the list len() print(len(list_2)) # View the length of the list item = list_2[len(list_2)-1] # After obtaining the length -1 print(item) # member operator # member in List, list / character string member not in list / character string aaa = ' Heard of ' in list_2 # Heard of stay list_2 in print(aaa) # It's true bbb = ' Heard of ' not in list_2 # Heard of be not in list_2 in print(bbb) # For false # Homework after class # 1、. Delete... From the list below " Short, poor and ugly ", Write 2 One or more methods : info = ["yuze", 18, " male ", " Short, poor and ugly ", [" high ", " rich ", " handsome "], True, None, " What are the eyes of a wolf "] # The first method :del list [ Indexes ] print(' The first method :del list [ Indexes ]') del info[3] print(info) # The second method : list .remove( data ) print(' The second method : list .remove( data )') # info.remove(" Short, poor and ugly ") # print(info) print('*********************************************************************************') # # 2、 Now there's a list li2=[1,2,3,4,5], lin2 = [1,2,3,4,5] # # Please change it to... Through relevant operations li2 = [0,1,2,3,66,4,5,11,22,33], # The first method : Use List name .append( data ) To add new 11,22,33 data , Need to add 3 Time print(' The first way is to use : List name .append( data ) To add new 11,22,33 data , Need to add 3 Time ') lin2.append(11) lin2.append(22) lin2.append(33) print(lin2) # The second method : Use extend: Will list 2 Merge the data in to the list 1 in , To add new 11,22,33 data print(' The second way is to use :extend: Will list 2 Merge the data in to the list 1 in , To add new 11,22,33 data ') # new_list = [11,22,33] # lin2.extend(new_list) # print(lin2) print(' Use the method of queue jumping : list .insert( Indexes , value ) To add new 0,66 data ') lin2.insert(0,0) lin2.insert(4,66) print(lin2) print('*********************************************************************************') # 3、 Please write out how to delete the elements in the list . # The methods to delete elements in the list are 4 Methods : my_list = [' Xiaohong ',' Xiao Ming ',' Big dog ',12,78,0.45,' A flower '] print(' The first is : Delete data at a certain location , From left to right , The format is : list .remove( data ) Delete the formulation data that appears for the first time in the list ,') my_list.remove(78) print(my_list) print(' The second kind :del list [ Indexes ] Delete the data of an index in the list ') del my_list[3] print(my_list) print(' The third kind of : list .pop() Delete a data at the end of the list by default ') my_list.pop() print(my_list) print(' A fourth : list .clear() All data in the list will be cleared ') my_list.clear() print(my_list) print('*********************************************************************************') # 4、 Yes 5 A small question ( Use the list operation to complete ): # a. A dating show needs to get your personal information , Please store your : full name 、 Gender 、 Age name = ' Vivier ' # assignment gender = ' Woman ' age = '27' print(' The list stores names 、 Gender 、 Age ') my_list = [' Vivier ',' Woman ',27] # Generate a new list print(my_list) # b. Someone is interested in you , The platform needs you to supplement your height and contact information ; print(' use append( data ) To add height and contact information ') my_list.append(178) my_list.append(15112552466) print(my_list) # c, Platform to protect your privacy , Need you to delete your contact information ; print(' use remove( data ) To delete contact information ') my_list.remove(15112552466) print(my_list) # d, In order to get better grades , Need to take a flower name , And modify your height and other information you feel you need to change . print(' Add a flower name and modify the height and gender ') my_list.insert(4,' Dahua ') # Add a flower name index1 = my_list.index(178) # Find the value that needs to be modified index2 = my_list.index(' Woman ') # Find the value that needs to be modified my_list[index1] = 180 # Then change it to a new value my_list[index2] = ' male ' # Then change it to a new value print(my_list) # e, You further add your own interests , Need at least 3 term ( Members in the list can be of any type , Consider adding a member , And the members are lists !!!). print(' Use insert Function to add a member to a list , It contains 3 Item interest ') new_list1 = [' Sing a song ',' Climbing the mountain ',' Study '] my_list.insert(5,[' Sing a song ',' Climbing the mountain ',' Study ']) print(my_list)