Guibs Python Learning _ list
# list # list consists of a series of elements arranged in a particular order, where elements and elements can have no relationship # in Python, use square brackets [] to represent the list, and separate the elements with commas, languages = [' Swift ', ' Python ', ' obj ' Ective-c "]print (Languages) # access list element # list is an ordered collection, so to access any element of the list, simply tell Python the location or index of the element # in Python, the index of the first element is 0print ( Languages[0]) print (languages[1]) print (languages[2]) # When the list is not empty: # Set the index to 1 to return the last element of the Python list print (languages[-1]) # Set the index to 2 to return the second-to-last element of the Python list print (languages[-2]) # Set the index to 3 to return to the third-lowest element of the Python list print (languages[-3]) # Use the elements in the list wanna_use = "I ' d like-to-use" + languages[1].title () + "." Print (wanna_use) # Add, modify, delete element # Modify list Element motorcycles = [' Honda ', ' Yamaha ', ' Suzuki ']print (motorcycles) motorcycles[0] = ' Ducati ' Print (motorcycles) # Add list element # Add # At the end of the list # [. Append ()]motorcycles.append (' Yamaha ') print (motorcycles) # Insert element # in the list # [. Insert ()]motorcycles.insert (1, "HL") print (motorcycles) # Delete list element # [Del]print (motorcycles) del motorcycles[1]print ( Motorcycles) # Delete the specified element and return the deleted value # [. Pop ()]poped_motor = Motorcycles.pop (0) print (poped_motor) # delete Element # [. Remove ()]# Only the first occurrence of the element that matches the value is deleted by the PRINT (motorcycles) motorcycles.remove (' Yamaha ') print (motorcycles) # organization list # to permanently sort the list # [. Sort ()]cars = [' BMW ', ' Audi ', ' Toyota ', ' mini ']print (Cars) Cars.sort () print (Cars) # Flashback Cars.sort (reverse=true) print (cars) # Use the sorted () list for a temporary sort cars2 = [' BMW ', ' Audi ', ' Toyota ', ' mini ']print (cars2) print (sorted (cars2)) print (cars2) # Flashback print (sorted (cars2, reverse=true)) # Permanently reverse list Order # [. Reverse ()]foods = [' Apple ', ' pear ', ' banana ']print (Foods) Foods.reverse () print (foods) # Determine the length of the list # [Len ()] Print (len (foods)) # Avoid index error when using List # action list # traverse the entire list magicians = [' Alice ', ' David ', ' Carolina ']for magician in Magicians:prin T (Magician.title () + ", that is a great trick!") Print ("I can ' t wait to see your next trick\n") print ("Thank You Everyone") # Create a numeric list # # Generate a series of numbers # [range ()]for value in range (1, 5): Print (value) # 1, 2, 3, 4# Specify the step for value in range (1, 5, 2): print (value) # 1 3# Use the function list () to convert the result of range () to a list n Umbers = List (range (1, 6)) print (numbers) # numeric list processing numbers = [1, 3, 2, 7, 4, 5, 6]print (max (numbers)) # Max print (min (Numbers) # Minimum print (sum (numbers)) # summation # list Resolution squares = [value**2 for value in range (1, ten)]print (squares) # Use part of the list # slice [to create a slice, you can Specify the index of the first and last elements to use +1]computers = [' MacBook ', ' MacBook Pro ', ' IMac ', ' Mac Pro ']print (Computers[1:2]) # get 1 <= Index < 2 element Print (computers[1:]) # get 1 <= indexed element print (Computers[:2]) # Get index < 2 element print (computers[:-1]) # Gets the element before the last element p Rint (computers[-3:]) # Get the last three elements # Traverse slice for computer in Computers[1:3]: print (Computer.upper ()) # Copy List # By creating a slice that omits the index limit Wann A_food = [' Apple ', ' pear ', ' banana ']print (wanna_food) Buy_food = Wanna_food[:]print (buy_food) # If slices are not used. Assigning an old list directly to a new array is equivalent to referencing the same list directly, not the copy copy
The above is the Guibs Python learning _ List of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!