First, traverse the entire list
Use for loop out a list of each of the variable, and then stored in the intermediate variables, the print intermediate variable; extraction cycle;
1, a simple for loop
Example:
carssa = [ 'richan', 'fengtian', 'bentian', 'aodi', 'nisang']
for ax in carssa :
print(ax)
Execution results are as follows:
richan
fengtian
bentian
AODI
nisang
2, performs more operations in a for loop
Example:
carssa = [ 'richan', 'fengtian', 'bentian', 'aodi', 'nisang']
AX in carssa for:
Print ( 'My favorite model is:' + AX)
Print ( 'My favorite model is:' + ax.upper ())
Execution results are as follows:
My favorite model is: richan
my favorite models are: RICHAN
my favorite models are: fengtian
my favorite models are: FENGTIAN
my favorite models are: bentian
my favorite models are: BENTIAN
my favorite models are: aodi
I favorite model is: AODI
my favorite models are: nisang
my favorite models are: NISANG
3, to perform some operations after the for loop
Example:
carssa = [ 'richan', 'fengtian', 'bentian', 'aodi', 'nisang']
AX in carssa for:
Print ( 'My favorite model is:' + ax)
print ( 'Hello, everybody, the above is my favorite model name')
Execution results are as follows:
My favorite model is: richan
my favorite models are: fengtian
my favorite models are: bentian
my favorite models are: aodi
my favorite models are: nisang
Hello everyone, the above is my favorite model name
=============================================================================
=============================================================================
=============================================================================
Second, avoid indentation error
1, forget indent
2, forget the extra lines of code indentation
3, unnecessary indentation
4, after the cycle of unnecessary indentation
5, missing a colon
=============================================================================
=============================================================================
=============================================================================
Third, create a list of values
1, using the range () function
Note: This function from a specified start value you, you specify and after reaching the second value is stopped, so the result does not include a second output value;
Using the range () function, the if the output does not meet the expectations, try the specified value plus or minus 1 1;
Example:
for value in range(1,5) :
print(value)
Results of the:
1
2
3
4
2, using the range () to create a list of numbers
Example:
number = list(range(1,7))
print(number)
Results of the:
[1, 2, 3, 4, 5, 6]
Example: specified step length, each plus 2, the final result is less than 17
number = list(range(1,17,2))
print(number)
Results of the:
[1, 3, 5, 7, 9, 11, 13, 15]
Example:
squares = [ ]
for value in range(1,11) :
square = value**2
squares.append(square)
print(squares)
Results of the:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Example:
squares = [ ]
for value in range(1,11) :
squares.append(value**2)
print(squares)
Results of the:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
3, perform simple statistical calculations of a list of numbers
Example:
ax = [1,2,3,4,5,6,7,8,9,10]
print (I) (ax)
print(max(ax))
print(sum(ax))
Results of the:
1
10
55
4. List Analysis: The for loop and create new elements merged into one line of code, and automatically append a new element
Example:
sq = [ value**2 for value in range(1,11) ]
print(sq)
Results of the:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
=============================================================================
=============================================================================
=============================================================================
Fourth, the use of part of the list - the list of processing element section referred to the slice --python
Example:
players = ['0a','1b','2c','3d','4c']
print (players [0: 3]) # display the index of the three elements 0,1,2
print (players [1: 3]) # the second and third display elements
print (players [: 3]) # 0 starts from the display after three elements
print (players [0:]) # 0 to the last Display
print (players [1:]) # display from the second to the last
print (players [-1:]) # displays the last
print (players [-3:]) # 3 displays the last, i.e., -1, -2, -3
Results of the:
[ '0a', '1b', '2c']
[ '1b', '2c']
[ '0a', '1b', '2c']
[ '0a', '1b', '2c', '3d ',' 4c ']
[' 1b ',' 2c ',' 3d ',' 4c ']
[' 4c ']
[' 2c ',' 3d ',' 4c ']
2, traversing sections: to traverse the list of some elements may be used for loop slices
Example:
players = ['a0a','b1b','c2c','d3d','e4e']
print(players)
for players in players[0:3] :
print(players.title())
Results of the
['a0a', 'b1b', 'c2c', 'd3d', 'e4e']
A0A
B1B
C2C
Example:
a = ['a','b','c']
b = ['c','d','e']
print(a)
print(b)
b = a
print(a)
print(b)
Results of the:
['a', 'b', 'c']
['c', 'd', 'e']
['a', 'b', 'c']
['a', 'b', 'c']
3. Copy the list
Example:
a = ['a','b','c']
b = a [:] # where a further increase in copy
print(a)
print(b)
a.append('X')
b.append('D')
print(a)
print(b)
Results of the:
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c', 'X']
['a', 'b', 'c', 'D']
###### *************** Example:
a = ['a','b','c']
where b = a # 2 refer to the same variable list
print(a)
print(b)
a.append('X')
b.append('D')
print(a)
print(b)
Results of the:
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c', 'X', 'D']
['a', 'b', 'c', 'X', 'D']
=============================================================================
=============================================================================
=============================================================================
Quintuple
The list can be amended. However, sometimes you need to create a series of non-modifiable elements of the tuple to meet this demand.
The python can not modify a value called invariable without variable list, called tuples.
Note: tuples with parentheses instead of square brackets to mark.
1, defined tuple
Example:
a = ('a','b','c','d','e')
print(a)
print(a[0])
print(a[-1])
print(a[0:3])
Results of the:
( 'a', 'b', 'c', 'd', 'e')
and
e
(and, b ',' c ')
2, all the values in the tuple traversal
Example:
a = ('a','b','c','d','e')
for b in a :
print(b)
Results of the:
a
b
c
d
e
3, modified variable-tuple
To tuple variable assignment is legal
Example:
a = ('a','b','c','d','e')
print(a)
a = (1,2,3,4,5)
print(a)
Results of the:
('a', 'b', 'c', 'd', 'e')
(1, 2, 3, 4, 5)