In [ ]:
# Recursion/Looping
# other programming languages:for,foreach,do..while,while..do,until
# python: for,while
# for - finite loop
# while - infinite loop - conditions

In [1]:
# for work on sequences
my_string="python"
for value in my_string:
    print value


p
y
t
h
o
n

In [2]:
# ('linux','python','mysql','django') - tuple
# ['linux','python','mysql','django'] - list

for value in ('linux','python','mysql','django'):
    print value


linux
python
mysql
django

In [4]:
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 [22]:
print range(5)
print range(1,5) # 1 is start and 5 is end.
# for(i=1;i<10,i+2)
print range(1,10,2)
print range(1,10)
print range(1,10,1)
print range(1,2)
print range(-4,-1)
print range(10,1,-1)


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

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


1
2
3
4
5
6
7
8
9
10

In [9]:
m = iter(range(1,6))

In [10]:
print m


<listiterator object at 0x7fd07043f250>

In [11]:
m.next()


Out[11]:
1

In [12]:
m.next()


Out[12]:
2

In [13]:
m.next()


Out[13]:
3

In [14]:
m.next()


Out[14]:
4

In [15]:
m.next()


Out[15]:
5

In [16]:
m.next()


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

StopIteration: 

In [ ]:
# iterator vs generator
# iterator : you get a whole dump of data at single point of time.
# range

In [2]:
# iterator : you get a whole dump of data at single point of time.
# range
print help(range)
range(1,10)


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
Out[2]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]:
# generator - yield and functions
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 [6]:
print xrange(1,11)
print type(xrange(1,11))
m = xrange(1,11)


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

In [10]:
print m,type(m),dir(m)


xrange(1, 11) <type 'xrange'> ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

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


1
2
3
4
5
6
7
8
9
10

In [ ]: