0

I just have a basic question about a for loop in Python 3. It's a stupid, simple question, but I figure everyone else here is far more knowledgeable to me, and most answers are pretty bright! I do not profess to be intelligent, so I'd like some clarification. Running this loop seems to add a value of only one. I have defined x to be 1, so this makes sense, but I didn't give i a true value and it seems to just naturally be 1.

for i in range(0, 50):
    x = 1
    i = x + i
    print(i)

I'm not getting an error, I'm just curious as to what's going on behind the scenes. Would love some explanation from someone who understands this more than I do!

M. Knepper
  • 153
  • 2
  • 8
  • At this point you're just confusing yourself, it seems. *You* told what values `i` should take by writing `i in range(0, 50)`. And then, you added a `1` to each value that `i` was taking in the range object. (starting from 0, if you notice. ) Change the Range around, and see what happens. – Paritosh Singh Aug 04 '19 at 16:35
  • Well since you defined `x` to be `1`, you will each time print `x + ` that value. Since the range starts with `0`, then `i+x` starts with `1`. – Willem Van Onsem Aug 04 '19 at 16:35
  • Ahhhh, I see what you mean. The range is giving `i` its value, yes? Starting with 0, add x, then 1, add x, so on, so on. – M. Knepper Aug 04 '19 at 16:38
  • 2
    Indeed. which means any extra work you do on the variable `i` is *thrown away* in the next iteration, because `i` is reassigned a new value from the Ranges. – Paritosh Singh Aug 04 '19 at 16:39
  • Ah yes, I see what's happening. Thank you. I was trying to do something like increase the value of `i` each time the loop starts over, like a Fibonacci sequence. You're right, I was confusing myself. Thank you so much. – M. Knepper Aug 04 '19 at 16:41
  • Yes, it appears so. I'm going to say this is a duplicate, but I love everyone's answers. Is there a way to keep everyone's responses once this gets marked a duplicate? Sorry again, I'm still pretty new to this and a slow learner. – M. Knepper Aug 04 '19 at 16:56
  • Don't worry the responses don't go away. Marking dupes is a very important aspect of the "housekeeping" of this site though, to avoid dilution of answers. Essentially, the goal of the site stays to become a resource that everyone can easily search through and find answers for. Marking dupes helps that process. :) – Paritosh Singh Aug 04 '19 at 16:58

1 Answers1

3

You do not need the x=i and i=x+i inside your loop.

for i in range(0, 50):
    print(i)

would work just fine and would output 0,1,2,3...47,48,49. i is being set according to the range(0,50). If you want i to start at 1 simply change the range to range(1,50). Your code also added 1 to each i value so in order to account for this in the range you would need to do range(1,51) which would print 1,2,3...48,49,50.

Further Reading

I would recommend reading these to better understand how for loops work and also more info about ranges:

Range()

For Loops

Noah Cristino
  • 757
  • 8
  • 29