Python: Basic Introduction 3 --- (for loop + list) the structure of the for loop in Python is quite different from that in C, C ++, and other languages, however, it is easy to get started. The Python list is similar to the array in C language, but it is more powerful than the array function. joke is that it hits the array in hormones.
For loop
(1) syntax structure of the For loop:
For element in variable (list, etc)
The source code of the operation is attached:
# For loop contact print ("======= For loop exercises =======") str1 = "For loop exercises" for I in str1: print (I, end = "") print ("\ n") print ("= range () use ====== ") with the For loop # In general, use the For loop and the range () function together with # The syntax of the range () function is: range ([start], stop [, step = 1]) # The first parameter is the starting number, and the second parameter is the ending number (excluding the ending number) # The last parameter is the step size. the default value is 1. the default start position is 0 for m in range (5): print (m, end = "") print ("\ n ") for n in range (, 2): print (n, end = "") print ("\ n") # List exercises: an array with hormones # A list mark: [,] print ("------ output entire list ------") list = ["turtle", "pudding ", "Lost"] print (list) print ("------ elements in the output list ------") list = ["turtle", "pudding ", "Lost"] for I in range (3): # Be sure to print the elements in the list starting from 0 (list [I], end = "") print ("\ n") print ("------ create a list of elements of the hybrid type ------") mixture = ["Yao Xiangxi", 1, 2] for I in range (3 ): print (mixture [I], end = "") print ("\ n") # add an element to the list and add the element list to the end of the element. append ("Yao Xiangxi") print (list, "\ n") # obtain the length of the list print ("The number of elements in the list is % d" % len (list ), "\ n") # add multiple elements to the end of the list at a time using extend. the parameter is a list listadd = ["Guoxiangyao", "Xiaoshuai"] list. extend (listadd) for I in range (6): print (list [I], end = "") print ("\ n") # use insert (,) function insert data # where the first element is the position of the element to be inserted # The second parameter is the list of elements to be inserted. insert (0, "") for I in range (7): print (list [I], end = "") print ("\ n ") # obtain and delete an element list from the list. remove ("turtle") # Deleting an element is a specified element, not an index for I in range (5): print (list [I], end = "") print ("\ n") # use an index to delete the Element del list [0] for I in range (4): print (list [I], end = "") print ("\ n") # delete the last element of the list. use the pop () function list. pop () for I in range (3): print (list [I], end = "") print ("\ n") # it can also be used in pop () to display the list of elements at the specified position. pop (0) for I in range (2): print (list [I], end = "")
The above is the content of Python Basic Introduction 3 --- (for loop + list). for more information, see PHP Chinese network (www.php1.cn )!