Python range() function

range() function

We can generate a sequence of numbers using range() function.
range(10) will generate numbers from 0 to 9 (10 numbers).

Syntax:
We can also define the start, stop and step size as
range(start,stop,step_size)
step_size defaults to 1 if not provided.

This function does not store all the values in memory, it would be inefficient. So it remembers the start, stop, step size, and generates the next number on the go.

Example:1

>>> range(10)

Output:

range(0, 10)

* In the above examples we have displayed only the range of numbers, not the numbers.
* In order to print the numbers in the given range we can use list sequence.

To force this function to output all the items, we can use the function list().

Example:2
Using range print number from 0 to 9
#it prints 0 to n-1

>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example:3
Using range print number from 0 to 10
Sol:

>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example:4
Using range print number from 0 to 14
Sol:

>>> print(list(range(15)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> 

Example:5
Using range print numbers from 0 to 20
Sol:

>>> print(list(range(21)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Example:6
print numbers from 1 to 10
Sol: (method:1)

>>> print(list(range(1,21)))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>> 

Example:7
print numbers from 1 to 10 (Using for loop)
Sol: (Method:2)

n=list(range(1,21))
for i in n:
     print(i)

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>> 
n=list(range(1,21))
for i in n:
     print(i,end=' ')

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
>>> 

Examples using start ,end and step options of range function

Example:8
print 1 to 20 in reverse
Sol:

print(list(range(20,0,-1)))

Example:9
print even numbers up to 20 (Method:1 )
Sol:

print(list(range(20,0,-2)))

Example:10
print even numbers upto 20 (Method:2 )
Sol:

n=list(range(2,21,2))
for i in n:
     print(i,end=' ')
     

Output:

2 4 6 8 10 12 14 16 18 20 
>>> 
n=list(range(20,1,-1))
for i in n:
     if(i%2==0):
          print(i,end=' ')

Example:11
print even numbers up to 20 (Method:3 )
Sol:

n=list(range(1,21))
for i in n:
     if(i%2==0):
          print(i,end=' ')

Example:12
print even numbers up to 20 in reverse order (Method:1 )
Sol:

print(list(range(20,1,-2)))

Example:13
print even numbers up to 20 in reverse order (Method:2 )
Sol:

n=list(range(20,1,-2))
for i in n:
     print(i,end=' ')

Example:14
print even numbers up to 20 in reverse order (Method:3 )
Sol:

n=list(range(20,1,-1))
for i in n:
     if(i%2==0):
          print(i,end=' ')

Example:15
print odd numbers up to 20 (Method:1 )
Sol:

print(list(range(1,20,2)))

Example:16
print odd numbers up to 20 (Method:2 )(Using for loop)
Sol:

n=list(range(1,20,2))
for i in n:
     print(i,end=' ')

Example:17
print odd numbers up to 20 (Method:3 )
Sol:

n=list(range(1,20))
for i in n:
     if(i%2==1):
          print(i,end=' ')

Example:18
print odd numbers up to 20 in reverse order (Method:1 )
Sol:

print(list(range(19,0,-2)))

Example:19
print odd numbers up to 20 in reverse order (Method:2 )
Sol:

n=list(range(19,0,-2))
for i in n:
     print(i,end=' ')

Example:20
print odd numbers up to 20 in reverse order (Method:3 )
Sol:

n=list(range(19,0,-1))
for i in n:
     if(i%2==1):
          print(i,end=' ')

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling