In [ ]:
# looping or iterations
# for,while,do..while,while..do,until,foreach
# python -> for,while
# for,while -> conditional based operations
# for -> finite loop
# while -> infinite loop

In [1]:
my_string='python'
# loop works on sequences.
# string is a sequence of characters.

for value in my_string:
    print value


p
y
t
h
o
n

In [5]:
for subject in ("python","linux","ruby","django"):
    #print '"{}"'.format(subject)
    print "%r" %(subject)


'python'
'linux'
'ruby'
'django'

In [9]:
# range
print help(range)


Help on built-in function range in module __builtin__:

range(...)
    range(stop) -> list of integers
    range(start, stop[, step]) -> list of integers
    
    Return a list containing an arithmetic progression of integers.
    range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
    When step is given, it specifies the increment (or decrement).
    For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
    These are exactly the valid indices for a list of 4 elements.

None

In [10]:
print range(10)
print range(1,11)
# for(i=2;i<=10;i+2)
print range(2,11,2)


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 6, 8, 10]

In [11]:
for value in range(1,11):
    print value


1
2
3
4
5
6
7
8
9
10

In [12]:
# how exactly a for loop works.
# iteration
m = iter(range(1,6))

In [13]:
print m.next()


1

In [14]:
print m.next()


2

In [15]:
print m.next()


3

In [16]:
print m.next()


4

In [17]:
print m.next()


5

In [18]:
print m.next()


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-18-6ecd064bb0a6> in <module>()
----> 1 print m.next()

StopIteration: 

In [19]:
# iterators vs generators
# iterator:ex: range
# generator:ex: xrange
print range(1,10)


[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [20]:
print help(xrange)


Help on class xrange in module __builtin__:

class xrange(object)
 |  xrange(stop) -> xrange object
 |  xrange(start, stop[, step]) -> xrange object
 |  
 |  Like range(), but instead of returning a list, returns an object that
 |  generates the numbers in the range on demand.  For looping, this is 
 |  slightly faster than range() and more memory efficient.
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |  
 |  __len__(...)
 |      x.__len__() <==> len(x)
 |  
 |  __reduce__(...)
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |  
 |  __reversed__(...)
 |      Returns a reverse iterator.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

None

In [21]:
print xrange(1,11)


xrange(1, 11)

In [22]:
for value in xrange(1,11):
    print value


1
2
3
4
5
6
7
8
9
10

In [ ]:
# for iterator you will run out of memory.
# for generator you will run out of time.