In reality, we often need to generate some lists. What better way to append than to use a for loop to make a single move to the list, in addition to being inefficient?
Python provides us with a very powerful way to create a list.
For example, we need to create a list of data for 1~100.
Range (1,101)
We're going to create a list of 12,22,32...1002, how do we do that?
[X*x for X in range (1,101)] | |------------------x get 1,2,3,4....100 list |-------------------------square calculation of elements in a list
How do we judge the list first if we need it? We can use IF.
[X*x for X in range (1,1001) if x%2==0] | ------First Judge X, the even number is left.
Multi-loop survival full list For example, we need to generate all 9*9 multiplication tables, so we can use a double loop.
[Str (x) + "*" +str (y) + "=" +str (x*y) for x in range (0,10) for Y in range (0,10)]
In the above code, the outer loop is x, and the inner loop is Y.