Sunday, March 25, 2012

Looping Statement: For Loop

Hello everyone, in this post we will discuss another Looping statement, which is the For Loop.

What is For Loop?

From the last post we know what are Looping statement, For loop statement is similar to while. While constructing a For Loop, you just need to remember this line. "FOR something IN something, keep doing something"

Lets check it out:

First lets make a List,

>>> behavior = ['good', 'not bad', 'crazy', 'not selfish', 'innocent', 'kind to all']
>>> for python in behavior:
             print "I am "+python

I am good
I am not bad
I am crazy
I am not selfish
I am innocent
I am kind to all

What happened exactly?

In the for statement, it says  for python in behavior, here python is a variable that is to be looped with list behavior.

If have done some other other programming language, you will notice that the python for loop statement is little different, but this is how it works, cant help it.

In Python the variable is looped with every element of a List from Left to Right.

Its little confusing, I know, but don't give up on it, practice it, make your own programs with the For Loop and you will get out of confusion.

However the later posts will make the things more clear.

Thank You and Don't forget to check the next post.


Post a Comment