Home      |       Contents       |       About

Prev: - 'for' loop       |       Next: -

'range'

range

  • 'for' loop requires a powerful iterable structure that can return the needed sequence of values in a fast, reliably and memory efficiently. Such a structure is provided by the range() function

  • The general syntax of the 'for' loop when using a range() is:

      for iterator in range(start, end [,step])  
          block-1  
      else:  
          block-2

How range works

  • 'range' returns (one-by-one) a sequence of values in the interval between 'start' and 'end' values:
    • start: the first value of the sequence; if not defined then '0' is assumed
    • end: the last value of the interval BUT NOT included in the sequence
    • step (optional): if included it defines the step at which the values are produced starting from 'start' and ending at the value closest to 'end' but without exceeding it

'range' in its basic form accepts only integer values as start, end, step arguments. See the examples below:

In [1]:
for i in range(10):
    print(i, end=' ')
0 1 2 3 4 5 6 7 8 9 
In [2]:
for i in range(1,11):
    print(i, end=' ')
1 2 3 4 5 6 7 8 9 10 
In [3]:
for i in range(5,12):
    print(i, end=' ')
5 6 7 8 9 10 11 
In [4]:
for i in range(-5,0):
    print(i, end=' ')
-5 -4 -3 -2 -1 
In [5]:
for i in range(0,10,2):
    print(i, end=' ')
0 2 4 6 8 
In [6]:
for i in range(0,10,3):
    print(i, end=' ')
0 3 6 9 
In [7]:
a=5;b=10;c=3
for i in range(a,b,c):
    print(i, end=' ')
5 8 
In [8]:
a=10; b=100; c=b//a
for i in range(a,b,c):
    print(i, end=' ')
10 20 30 40 50 60 70 80 90 

More examples with range()

  • a range() object can be bound to some name and this name can be used as an iterable at a later time
In [9]:
x = range(1,10)
for i in x:
    print(i, end=' ')
1 2 3 4 5 6 7 8 9 
  • The 'start' value cam be smaller than 'end' provided that 'step' has a negative value
In [10]:
for x in range(10,5,-1):
    print(x, end=' ')  
10 9 8 7 6 
In [11]:
for x in range(10,2,-3):
    print(x, end=' ')
10 7 4 
  • The value of iterator can be changed within the iteration code: This simply means that the iterator name is bound to another object. In the next iteration the next value of the sequence will be assigned to the iterator and the loop will continue without any problem.
In [15]:
for i in range(10):
    i += 10 * i
    print(i, end=' ')
0 11 22 33 44 55 66 77 88 99 

chr() and ord() functions

  • Strings can not be passed as arguments to range()
  • However we can use the following functions to convert between characters and ASCII codes:
    • ord(): returns the ascii integer code of a character
    • chr(): returns the ascii character of a given integer code
  • In the code below, after you understand how it works, make the small adjustment required so that the 'z' character is also printed in the output
In [18]:
for ch in range(ord('a'), ord('z')):
    print(chr(ch), end=' ')
a b c d e f g h i j k l m n o p q r s t u v w x y 

. Free learning material
. See full copyright and disclaimer notice