Loops From Python

     In Python, There are loops like for and while, Let's See them in detail.

for Loop

     The for statement from python iterates over any sequence it may be string or list.
Syntax:
    for iterator in sequence:
        statement1
        statement2
        ...
Example:
>>> a = 3
>>> for char in 'python':
...     print char
... 
p
y
t
h
o
n

while Loop

     The while statement from python is used to check condition and then iterate if condition evaluates True.
Syntax:
    while condition:
        statement1
        statement2
        ...
Example:
     Let's we will write function that calculates Fibonacci series.
>>>def fibo(limit):
...     a, b = 0, 1
...     while a < limit:
...         print a,
...         a, b = b, a + b
...
>>>fibo(10)
0 1 1 2 3 5 8
     Get in touch for more fun, Keep Following.

Comments

Popular posts from this blog

Getting Started With Python

Python List

The dir Function