Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing the upper limit of range inside a loop doesn't make it run forever [duplicate]

I'm an intro computer science student working in Python 3.7.1.

We were working with "Additorials" where you take a number and take get the sum of the number plus every number before it. Ie: for the number 10-- 10+1+2+3+4+5+6+7+8+9 = 55

I had to write a program that performed this operation as a function. However, I did it in a way that shouldn't work, but it does.

def bigAdd(n):
    for i in range(0,n):
        n+=i
    return n

for example, if I input the number 10, it returns 55

But... Why?

If the upper limit of this loop is n, and it is constantly being incremented by i, shouldn't it run forever because it is constantly raising its limit? Why does it return any answer, let alone the correct one?

like image 886
Pingu21 Avatar asked Nov 21 '19 17:11

Pingu21


People also ask

Can we increment inside for loop?

A for loop doesn't increment anything. Your code used in the for statement does.

How do you increment a number in a for loop in Python?

In python, if you want to increment a variable we can use “+=” or we can simply reassign it “x=x+1” to increment a variable value by 1.

Can a for loop on its own end up in an infinite loop?

A for loop is only another syntax for a while loop. Everything which is possible with one of them is also possible with the other one. Any for loop where the termination condition can never be met will be infinite: for($i = 0; $i > -1; $i++) { ... }

Can you make an infinite for loop in Python?

We can create infinite loops in Python via the while statement. In a loop, the variable is evaluated and repeatedly updated (while the given condition is True). We can create an infinite loop in Python if we set the condition in a way that it always evaluates to True.


2 Answers

You are adding to n, which initially is 10 (or whichever upper bound you are using). Thus your result is indeed 10 (the initial value) + 0 + 1 + ... + 9 (from the range).

Having said that, I'd still recomment not using the initial value of n and instead getting the sum of range(1, n+1), as that's much clearer.

>>> sum(range(1, n+1))
55

Or if you want to show off:

>>> n*(n+1)//2
55

About your second question:1 No, the range(0, n) is evaluated only once, when the for loop is first entered, not in each iteration. You can think of the code as being roughly2 equivalent to this:

r = range(0, n) # [0, 1, 2, 3, ..., n-2, n-1]
for i in r:
    n+=i

In particular, Python's for ... in ... loop is not the "typical" for (initialization; condition; action) loop known from Java, C, and others, but more akin to a "for-each" loop, iterating over each element of a given collections, generator, or other kind of iterable.

1) Which, I now realise, is actually your actual question...

2) Yes, a range does not create a list but a special kind of iterable, that's why I said "roughly".

like image 145
tobias_k Avatar answered Oct 17 '22 21:10

tobias_k


range(0,n) is evaluated once before the loop is entered.

This isn't like a typical for loop from other languages that has a condition that is constantly checked. range returns a range object that produces numbers, and the upper limit is set when the range object is created. Changing n has no effect on the range object that's already been constructed.

like image 16
Carcigenicate Avatar answered Oct 17 '22 20:10

Carcigenicate