Oct-06-2019, 11:50 AM
When you know how many times you want to go through the loop, better to use a for statement. While is more for loops where you are less sure of the number of iterations and want to check for a condition.
x = [] for count in range(5): print(count) x.append(count) print (x)
Output:0
1
2
3
4
[0, 1, 2, 3, 4]
To use while, instead, your code is closerx = [] count = 0 while count<5 : print (count) x.append(count) count+=1 print (x)Gives same output, but you can see the code is slightly longer because you are manually handling "count"