Tuesday, 6 November 2012

For Loop

The For statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.
here is the simple example of for loop:


for i in range(1,11):
    print i

This is simple code of For loop and this will print the first ten natural numbers.
Here," for" is the keyword "in range()" is the function for the range of loop. Here the range of loop is 1 to 11 because for loop work with only up to n-1. Here n can be a number which stores the rang of loop given by user.

No comments:

Post a Comment