0

In my Jupyter notebook (Python 3) I use the variable kk over and over:

kk = 7
for kk in range(2): # ranging from 0 to 1
    for kk in range(90,92): # ranging from 90-91
        for kk in range(10,12): # ranging from 10-11
            print(kk)
kk

As I understand, the for kk in range(2) loop uses its own kk variable (scope) and does not care about the kk defined above, nor the kk from the other loops. The same is true for the for kk in range(90,92) loop and the for kk in range(10,12) loop. How come all of them overwrite the globally defined kk, so that the final kk is 11, but they never read the global kk?

I am trying to understand scopes and namespaces. But I expected that:

  1. none of the loops would overwrite the global kk, as they use kk in their own scope (which I expected due to Namespace and scope in Python); or
  2. the loop should end after the first iteration, as kk is now set to 10, which lies outside the required range of the outermost loop.

I think Scope and Namespace in Python is trying to explain it, but I do not understand.

6
  • Does this answer your question? Python nested functions variable scoping Commented Apr 16, 2024 at 10:39
  • 2
    Hello Tim, your entire code is in one scope. Simplifying a bit variables are isolated only when they are created inside different functions. Commented Apr 16, 2024 at 10:40
  • 2
    In regards to 2. - You are incorrect in thinking that kk is somehow checked against range. Whatever is after in is used as iterable, taking elements from it one by one and assigning it to kk. This is done disregarding what kk currently is, and will be repeated untill all elements of iterable are exhausted. Commented Apr 16, 2024 at 10:55
  • 1
    I think this answers the question: stackoverflow.com/questions/291978/… --- basically, for-loops don't get their own scope, only functions do. Commented Apr 16, 2024 at 11:10
  • In a nutshell: there's only one variable kk, and its value gets constantly overwritten by each loop. That's it. You may be incorrectly assuming that the loop works like for (kk = 0; kk < 42; kk++) in some other languages, but that's wrong, it doesn't. Commented Apr 16, 2024 at 11:17

1 Answer 1

1

Well the kk changes each loop each step.

The for is not static as in C++ or C# so it actually does not care if the kk is changed in the middle.

kk = 7 # Here it is 7


for kk in range(2): 
    # Here kk is 0
    for kk in range(90,92): 
        # Here kk is 90 
        for kk in range(10,12):
            # Here kk is 10 and 11
            print(kk) # <- 10\n11
# Now it goes for the second loop
    for kk in range(90,92): 
        # Here kk is 91
        for kk in range(10,12):
            # Here kk is 10 and 11
            print(kk) # <- 10\n11
# Now the inner loop is finished so it goes all over again


for kk in range(2): 
    # Here kk is 1
    for kk in range(90,92): 
        # Here kk is 90 
        for kk in range(10,12):
            # Here kk is 10 and 11
            print(kk) # <- 10\n11
# Now it goes for the second loop
    for kk in range(90,92): 
        # Here kk is 91
        for kk in range(10,12):
            # Here kk is 10 and 11
            print(kk) # <- 10\n11

# Now the outer loop has finished so the job is finished, last kk value is 11
# Since you implemented kk before the loops the last value is not lost.

Python does not care what variable you use in loops.

For example

some_list = [1,2]

for item in some_list:
    some_list.remove(item) # <- this will work but once

This will work for one iteration, the 1 will be removed by the 2 will not because python goes from index 0 -> 1 -> 2. Since it found index 0 it went for iteration then it could not find index 1 so it has ended.

If you go

some_list = [1,2]

for item in some_list:
    some_list.append(item)

You will get infinite loop of [1, 2, 1, 2, 1, 2, ...]

Bacuse you use range(X) it just creates a list of numbers which then is used so, there is no issue with overusing kk but each loop you lose previous value.

Does this answer your question?

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean by # Since you implemented kk before the loops the last value is not lost.? The last value is not lost even if you hadn't implemented kk before the loops.
Thank you for your helpful answer. "# Here kk is 91" should read "# Here kk is 90 and 91"; so even though all loops constantly overwrite the original kk, none of them are affected because after each loop has "learned" the rule kk in range(), it never reads the kk again...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.