Python code:
for i in xrange(10):
for j in xrange(5):
pass
# The for-loop ends, but i,j still live on
print i,j # 9, 4
C code:
for(int i=0; i<=10; i++)
for(int =0; j<=5; j++)
;
// The for-loop ends, so i,j can't be accessed, right?
printf("%d, %d", i, j); // won't compile
So, variables in Python will live on even after the for loop ends?
Only functions, modules, and the bodies of class definitions delineate scopes in Python. Other control structures don’t.
Some basic information about this is in the Python Scopes and Namespaces section of the Classes page of the Python Tutorial. One important part: