In [ ]:
# looping
# for,while,foreach,do..while,while..do,until
# Python - for,while
# for -  finite loop
# while - infinite loop

In [2]:
# for always works on sequence
my_string = "python"  # sequence of characters
for value in my_string:
    print value


p
y
t
h
o
n

In [3]:
# a sequence as a list or tuple.
# list or tuple is a sequence of elements.
for training in ('python','django','linux','openstack','devops'):
    print training


python
django
linux
openstack
devops

In [4]:
for training in 'python','django','linux','openstack','devops':
    print training


python
django
linux
openstack
devops

In [5]:
# numbers
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 [8]:
print range(5)
print range(1,11,1)
print range(2,11,2)


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

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


1
2
3
4
5

In [10]:
# iteration
m = iter(range(1,6))
print m,type(m)


<listiterator object at 0x7fe9408f6150> <type 'listiterator'>

In [11]:
print m.next()


1

In [12]:
print m.next()


2

In [13]:
print m.next()


3

In [14]:
print m.next()


4

In [15]:
print m.next()


5

In [16]:
print m.next()


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

StopIteration: 

In [17]:
# iterators and generator
# ex:
print help(range)
print help(xrange)


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
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 [ ]:
# range(100000000) -> 1number - 4kb -> 4kb * 1cr
# xrange(100000000) -> number - during the whole operation we will be using only 4kb

In [21]:
print range(5)
print xrange(5),type(xrange(5))


[0, 1, 2, 3, 4]
xrange(5) <type 'xrange'>

In [20]:
for value in xrange(5):
    print value


0
1
2
3
4

In [ ]:
# range(100000000) -> you run out memory.
# xrange(100000000) -> you run out of time.