Why does my Python loop keep overwriting the variable instead of storing all the values?
I need some help understanding something about loops in Python that I can't get past.
I'm writing a loop that goes through a list of numbers, does a small calculation on each one, and I want to save every result. But after the loop finishes, my variable only holds the last value from the final iteration. Everything before it is gone.
Here is a simple version of what I'm doing:
result = 0
for num in [1, 2, 3, 4, 5]:
result = num * 2
print(result) # only prints 10, I want all results
I want to keep every value, not just the last one. I checked the FAQ and the index but couldn't find something that directly addresses this specific loop behavior for a mid-level beginner. I understand basic loops but I'm clearly missing something about how Python handles variable reassignment inside a loop.
Should I be using a list and appending each result? I tried that briefly but wasn't sure if that was the right direction or if there's a cleaner way I should learn first.
Been learning Python for about 5 months on my own. Not a complete beginner but still solidifying the fundamentals.