3.1 Traversing the entire list
You often need to iterate through all the elements of the list, performing the same action on each element. You can use the for loop in Python.
magicians.py
Magicians = ['Alice','David','Carolina ' ] for in magicians: print(magician)>> >Alice David Carolina
In-depth research cycle
In the simple loop used in magicians.py, Python first reads one line of code:
for in Magicians:
This line of code allows Python to get the first value (' Alice ') in the list magicians and store it in the variable magician. Next, Python reads the next line of code:
Print (magician)
It lets python print the value of the magician-still ' Alice '. Since the list also contains other values, Python returns to the first line of the loop:
for in Magicians:
Python gets the next name in the list, ' David ', and stores it in the variable magician, and then executes the loop body code. This is repeated until there is no additional code behind the For loop. In addition, you can specify any name for the temporary variable that is used to store each value in the list.
3.2 Creating a value list using the function range ()
The function range () allows you to easily generate a series of numbers.
for in range (1,5): print(Vale)1234
function range () lets Python start with the first value you specify and stop after the second value you specify, so the output does not contain a second value. If you are using range (), try +1 or-1 if you do not meet your expectations.
Use range () to create a list of numbers
To create a list of numbers, you can use the function list () to convert the result of range () directly to a list.
Numbers = List (range (1,6))print(numbers)>>>[1,2,3,4,5]
You can also specify the step size when you use the function range (). To print an even number within a 1~10.
Even_numbers = List (range (2,11,2))print(even_numbers)>>>[2,4,6,8,10]
The function range () starts at 2 and then increases by 2.
Use function range () to create almost any set of numbers you need. For example, create a square list that contains the first 10 integers. In Python, two asterisks (* *) represent a exponentiation operation.
Squares = [] for in range (1,11): = value**2 squares.append (squrare) print(squares) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
You can also append each computed value directly to the end of the list without using temporary variables:
Squares = [] for in range (1,11): squares.append (value**2) Print (squares)
Perform simple statistical calculations on a list of numbers
digits = [1,2,3,4,5,6,7,8,9, 0]min (digits) max (digits) sum (digits)
List parsing
List resolution merges the code for the For loop and the creation of the new element into a single line and automatically attaches the new element
for in range (1,11)]print(squares)
To use this syntax, first specify a descriptive list name, such as squares, and then specify an opening parenthesis and define an expression to generate the values that you want to store in the list. In this example, the expression is value**2. Next, write a for loop that provides a value for the expression, plus a closing parenthesis.
3.3 Using a portion of a list slice
To create a slice, you specify the index of the first element to be used and the index of the last element plus 1. To output the first 3 elements of the list, you need to specify the index 0~3, which will output 0,1,2.
Players = ['a','b','C','D','e']Print(Players[0:3])>>>['a','b','C']
column as you want to take the 2nd to 4th element of the list, you can specify an index of 1, 4. If you do not specify the first index, Python will automatically start extracting from the list header: In the same vein, without specifying the last index, Python will automatically go from the first index to the end of the list.
A negative index returns the element at the corresponding distance from the end of the list. For example, if you want to output the last 3 elements, you can use a slice players[-3:].
Players = ['a','b','C','D','e']Print(players[-3:])>>>['C','D','e']
Traversing slices
If you want to traverse some of the elements in the list, you can use slices in the For loop.
players = [ " a , " b , " c " , " Span style= "Color:rgb (0, 0)" >d ", ' e " ] for player in players[:3]: print (Player.title ()) ABC
Copy List
To copy a list, you create a slice that contains the entire list.
My_love = ['a','b','C','D','e']friend_love=my_love[:]Print(My_love)Print(friend_love) ['a','b','C','D','e']['a','b','C','D','e']
3.4 tuples
Python will not be able to modify values called immutable, and immutable lists are called tuples.
Defining tuples
For example, there is a rectangle that cannot be changed in size, and it can be stored in a tuple for a long width, ensuring that they are not modifiable.
Dimensions = (200,50)print(dimensions[0])print(dimensions[1]) 20050
Traverse all values in a tuple
Like lists, tuples can also be traversed with a for loop.
Dimensions = (200,50) for in dimensions: print(dimension) 20050
modifying tuple variables
Although you cannot modify the elements of a tuple, you can assign a value to a variable that stores a tuple. So to modify the dimensions of the rectangle, you can redefine the entire tuple
Dimensions = (200,50) for in dimensions: print= (400,100 ) for in dimensions: print(dimension)20050400100
Python Essay 3 (action list)