Python loop count area

I am trying to understand how this code works. How is i available outside the for loop?

 # Palindrome of string str=raw_input("Enter the string\n") ln=len(str) for i in range(ln/2) : if(str[ln-i-1]!=str[i]): break if(i==(ln/2)-1): ## How is i accessible outside the for loop ? print "Palindrome" else: print "Not Palindrome" 
+6
source share
1 answer

This is part of Python. Variables declared inside loops (including loop counts) will not decay until they completely leave the scope.

Take a look at this question:

Show in Python for loops

From the answers:

 for foo in xrange(10): bar = 2 print(foo, bar) 

The above will print (9.2).

+1
source

All Articles