Home > Back-end >  Are Python for loop variables a dictionary?
Are Python for loop variables a dictionary?

Time:12-07

PyCharm seems to indicate that my variable in my for loop is a dictionary. But I can't access it like such. For instance for i in range(10): the i shows as a dictionary when I hover over it.

I am expecting to be able to access the variable like a dictionary and print values().

CodePudding user response:

No, the variable i in the inside of a for i in range(10): block is not a dictionary (assuming range is actually the built-in Python function and has not been replaced with something bonkers). It's an ordinary integer value. If your IDE is telling you otherwise, then your IDE is incorrect.

CodePudding user response:

If familiar with C /C think of i in the same manor that you would have in the for loop:

for (int i = 0; i < 5; i  ) 
{
cout << i << "\n";
}

This would translate in python to:

for i in range (5):
 i =1

Where 'i' is essentially a temp variable holding manipulated data rather than a dictionary or something that holds actual reliable data.

  • Related