#Summary of list operations functionsPrint("Summary of list operations functions") List_demo= [' First','Second','Thrid','Fourth']#copy list_demo list nameList =list_demo[:]Print("The original list is:", list)Print("-----------------------------")Print("The first element of the output list:", list[0])Print("The last element of the output list:", list[-1])Print("elements from 2 start to 3rd output list:", List[1:3])Print("elements from 2 start to end of output list:", list[1:])Print("-----------------------------")#insert element to end of listList =List_demo[:]list.append ("Hello")Print("Insert an element to the end of the list:", list)#inserts an element at the specified position in the listList =List_demo[:]list.insert (1,"Hello")Print("inserts the element at the specified position in the list:", list)Print("-----------------------------")#remove an element from a specified position in a listList =list_demo[:]delList[1]Print("Delete the element at the specified position in the list:", list)#removes the element from the specified position in the list and recordsList =List_demo[:]popone= List.pop (1)Print("Delete the element at the specified position in the list and record:", List,"the deleted elements are:", Popone)#Delete an element of a specified value in a listList =List_demo[:]list.remove (" First")Print("to delete data from a specified value in a list:", list)Print("-----------------------------")#List parsing: Merging the code for loops and expressions into one lineList = [value**2 forValueinchRange (1, 5)]Print("List parsing results:", list)Print("-----------------------------")#checks whether the specified element is in the list: in or not. List =list_demo[:]if " First" inchlist:Print("judging ' first ' in the list")Print("-----------------------------")#determine if there is a value in the listiflist:Print("there is a value in the Judgment list. ")Else: Print("The judgment list is empty. ")
Operation Result:
List Operation function Summary The original list is: [' first ', ' second ', ' Thrid ', ' fourth ']-----------------------------output list, the last element of the list: Fourth elements from 2 start to 3rd output list: [' Second ', ' Thrid '] from 2 to the end of the output list of elements: [' second ', ' Thrid ', ' fourth ']-------------------------- ---list insert element to end: [' first ', ' second ', ' Thrid ', ' fourth ', ' hello '] list in the specified position of the insertion element: [' first ', ' Hello ', ' second ', ' Thrid ', ' fourth ' ']-----------------------------Delete the element at the specified position in the list: [' First ', ' Thrid ', ' fourth '] Delete the element at the specified position in the list and record: [' first ', ' Thrid ', ' fourth The deleted element is: Second delete data from the specified value in the list: [' Second ', ' Thrid ', ' fourth ']-----------------------------list parse Result: [1, 4, 9,]--------- --------------------judge that ' first ' has a value in the list-----------------------------judgment list.
List of Python operations