In [ ]:
# looping
# for,while .. do,do.. while,until,foreach  # other programmign languges
# for,while # python
# for is a finite loop.
# while is not a finite loop,depends on condition.

In [1]:
# A for/while loop works on sequences
# A string is a sequence of characters.

my_string="python"

for value in my_string:
    print value


p
y
t
h
o
n

In [2]:
for training in ['perl','shell','django','ruby']:
    print training


perl
shell
django
ruby

In [3]:
# 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 [9]:
print range(5) # 0 till 4 , 5 not included
# for(i=1,i<=5,i++)
print range(1,5,1)
# for(i=2,i<=10,i+2)
print range(2,10,2)

for value in range(1,6):
    print "hey hello"


[0, 1, 2, 3, 4]
[1, 2, 3, 4]
[2, 4, 6, 8]
hey hello
hey hello
hey hello
hey hello
hey hello

In [10]:
# iteration
print help(iter)


Help on built-in function iter in module __builtin__:

iter(...)
    iter(collection) -> iterator
    iter(callable, sentinel) -> iterator
    
    Get an iterator from an object.  In the first form, the argument must
    supply its own iterator, or be a sequence.
    In the second form, the callable is called until it returns the sentinel.

None

In [12]:
print range(1,6),type(range(1,6))
m = iter(range(1,6))
print m,type(m)


[1, 2, 3, 4, 5] <type 'list'>
<listiterator object at 0x7f1d945c6250> <type 'listiterator'>

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 [20]:
# iterator and generator.
print help(range)
# Iterator ex: range
print range(1,5)


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
[1, 2, 3, 4]

In [21]:
# generator ex: xrange
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 [23]:
print xrange(5),type(xrange(5))


xrange(5) <type 'xrange'>

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


0
1
2
3
4

In [ ]:
# iterator : memory consuming,run out of memory
# generator : time consuming,run out of time

In [ ]:
my_string="python"

# my_string = object/instance
# my_string.<anythong> = method(function)/data(variabel)