A function that is common in the For Loop is range (), but sometimes when you print a range directly, the result is not what we want, and at this point we can use the list to convert the result of range so that we clear out the elements in range.
For example:
s=10d=5 for in range (s,s+D): s=s+1 print(s) Print (i) Print (Range (s, S + D, 1)) Print (List (range (s,s+d,1)))
Operation Result:
11
10
Range (11, 16)
[11, 12, 13, 14, 15]
12
11
Range (12, 17)
[12, 13, 14, 15, 16]
13
12
Range (13, 18)
[13, 14, 15, 16, 17]
14
13
Range (14, 19)
[14, 15, 16, 17, 18]
15
14
Range (15, 20)
[15, 16, 17, 18, 19]
Here you can see clearly the different results of two outputs, such as the result range (11, 16) and [11, 12, 13, 14, 15], in the statement
Print (range (s, S + D, 1)), the last parameter 1 is to indicate that the element step in range is 1, if write 2, meaning is one element to take one, so, modify the code:
s=10d=5 for in range (s,s+D): s=s+1 print(s) Print (i) Print (Range (s, S + D, 1)) Print (List (range (s,s+d,1))) Print (List (range (s,s+d,2)))
Operation Result:
11
10
Range (11, 16)
[11, 12, 13, 14, 15]
[11, 13, 15]
12
11
Range (12, 17)
[12, 13, 14, 15, 16]
[12, 14, 16]
13
12
Range (13, 18)
[13, 14, 15, 16, 17]
[13, 15, 17]
14
13
Range (14, 19)
[14, 15, 16, 17, 18]
[14, 16, 18]
15
14
Range (15, 20)
[15, 16, 17, 18, 19]
[15, 17, 19]
Python Basic--list Conversion range () print results