Python For Statement
For a statement in Python, C or Pascal differ slightly from what you can use. Instead of running more than always like the arithmetic progression of numbers (in Pascal), or giving the user the ability to define both iteration steps and pausing positions (as in C), any sequence for Python's statements (A string) iterates over the item of a list), in the order in which they appear. For example (no punishment intended):
>>> for i in range (5):
print(i)
It will print 0 to 4. by the default value of i is 0(zero ) and maintain sequence up to 4.
>>> for i in range (5,10):
print(i)
It will start with 5 and print up to 9.
>>> for i in range (10,1,-1):
print(i)
It will start with 10 and print up to 1 by decrementing way.
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print(w, len(w))
...
cat 3
window 6
defenestrate 12
No comments: