Pages

Monday, 24 June 2013

how to use range function in Python

range function is used to create a list (not a tuple) of integer


to create a list, there are several ways

range(integer value) makes a list of integer (0 based to the integer entered)

input : range(10)
output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]




range(start, stop, step) makes a list of integer from start to stop, and +step
input : range(1,10,2)
output : [1, 3, 5, 7, 9]



range(start, stop) makes a list of integer from start up to stop but not including( same as range(start,stop,1) )

input : range(1,10)
output : [1, 2, 3, 4, 5, 6, 7, 8, 9]

Sunday, 23 June 2013

how to use for statement in Python

usually for statement is used with range (and xrange)

for var in list or tuple (-> range or xrange)
      statement




for example, to print out
*
**
***
****
  .
  .
*. . . . .****
as many as we want


to do this,
we need two for statement.

=============================================
intvalue = input("Enter integer value :")
"""intvalue = int(raw_input("Enter integer value :"))"""

for firstfor in range(intvalue):
    for secondfor in range(firstfor+1):
        print "*",
    print

==============================================