Why do I not have to define the variable in a for loop using range(), but I do have to in a while loop in Python?

后端 未结 6 677
悲哀的现实
悲哀的现实 2021-01-18 17:36

I have the following code using a for loop:

    total = 0
    for num in range(101):
       total = total + num
       print(total)

Now the

6条回答
  •  野的像风
    2021-01-18 17:52

    For Loop iterates each element from the list until the given range. So no need of any variable to check condition.

    While Loop iterates until the given condition is true. Here we need some variable or value to check the condition, So the variable num is used before the loop.

提交回复
热议问题