Variables in for loops

I'm totally new to programming, I don't know the lingo. I'm probably saying the most typical layperson thing ever but I need help.

I'm using Automate the Boring Stuff with Python and it seems to me that sometimes the author doesn't explain a few things, or maybe I'm missing something.

On the section about for loops, the formula has a variable. Which I didn't get, in my mind, variables were something you prepared for a piece of code you want the program to run (hopefully that's understandable). I asked ChatGPT if in a for loop, the variable was sort of embedded and it said yes but that this variable was a counter, and it tracks iterations for the program itself.

total = 0

for num in range(101):

total = total + num

print(total)

I don't get what's happening here. Is num where I'm storing iterations? I don't get how num is working here. I've used Python Tutor and still, don't understand its purpose.

Posted Nov 12, 2024, 8:38 PM UTC

222
Loading comments…