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]

1 comment: