In [ ]:
# looping - iterations/generators
# repeating a block of code n number of times
# for,do..while,while..do,until,foreach
# python - for,while
# for - finite loop
# while - infinite loop

In [1]:
# for
# iterators work on sequence.
my_string = "python" # sequence of characters

for value in my_string:
    print "value - {}".format(value)


value - p
value - y
value - t
value - h
value - o
value - n

In [2]:
for training in ('python','linux','django','flask'):
    print training


python
linux
django
flask

In [3]:
# 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 [7]:
print range(5) # 0 till 5 ; 5 not included.
print range(1,6)
print range(1,10,2)
print range(0,10,2)


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

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


1
2
3
4
5
6
7
8
9
10

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


<listiterator object at 0x7f2c452d5310>

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]:
# iterator
print range(1,6)


[1, 2, 3, 4, 5]

In [18]:
# generator
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 [20]:
print xrange(1,6)
print type(xrange(1,6))


xrange(1, 6)
<type 'xrange'>

In [21]:
for value in xrange(1,6):
    print value


1
2
3
4
5

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

In [ ]:
# https://projecteuler.net/archives
# https://github.com/sambapython/python