[Tutor] Usage of for loop

jadrifter jadrifter at gmail.com
Tue Jan 6 00:07:07 CET 2009

On Mon, 2009-01-05 at 18:49 +0530, vanam wrote:
> Hi all,
> i am beginner to this python language and slowing learning the
> language by referring docs.I am trying to understand the for loop
> i.e., usage of for loop in python,unlike c where i can give condition
> in python it is simple iterating over sequence.
> I am trying tounderstand the below lines of code but of no avail. 
> a = ["cat", "window","defenestrate"]
> for x in a:
>      print x, len(x)
> i cant understand what x is doing here and what is the purpose of it 
> can anyone help me out here?
> -- 
> Raghavendra  Vanam
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

x is a new variable and when used this way it holds one of the members
of "a" for each trip through the loop.

After your for loop is done there will be two new names in a "dir()"
listing if you're using the interactive python shell.  There will be a
name "a" and a  name "x".  If you type "print x" it will output
"defenestrate" because it still holds the last value of a from the for

John Purser

More information about the Tutor mailing list