Python Programming – The Range ( ) Function

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better.

Python Programming – The Range ( ) Function

Python range ( ) function generates a list of numbers, which is generally used to iterate over with for loops. Python range ( ) function examples:
>>> # One parameter
>>> for i in range(5):
. . . print(i)
. . .
0
1
2
3
4
>>> # Two parameters
>>> for i in range(3, 6):
. . . print(i)
. . .
3
4
5
>>> # Three parameters
>>> for i in range(4, 10, 2):
. . . print(i)
. . .
>>> # Going backwards
>>> for i in range(0, -10, -2)
. . . print(i)
. . .
0
-2
-4
-6
-8

Let’s Try

What is the output of the following code fragments?

>>> for i in range(10):
. . . print(i)
>>> for i in range(0, -12, -2):
. . . print(i)
sum = 0
for i in range(16,2,-2):
sum+ = i
print (sum)
for x in range(-300; 300, 100):
print(x)

Python range ( ) vs range ( ) Functions
You may have heard of a function known as a range( ). This is a function that is present in Python
2. x; however, it was renamed to range ( ) in Python 3. X, and the original range() function was deprecated in Python 3.x. In Python 2.x, range ( ) produced a list, while range( ) returned an iterator – a sequence object.

The for Loop and the range( ) Built-in Function

The for statement acts more like a traditional loop; that is, a numerical counting loop.
>>> for eachNum in [0, 1, 2, 3, 4, 5]:
print(eachNum)
0
1
2
3
3
5
Within our loop, eachNum contains the integer value that you are displaying, and you can use it in any numerical calculation you wish. Since your range of numbers may differ, Python provides the range( ) built-in function to generate such a list for you. It does exactly what you want, taking a range of numbers and generating a list.
>>> for eachNum in range(6):
print(eachNum)

0
1
2
3
4
5
Note that range(n) generates integers 0, 1, 2, …, n-1. For example,
range(start, stop, step) generates a sequence of integers as a start, start+step, start+2*step, and so on, but not including stop. For example, range(2, 8, 3) returns 2 and 5 (and not 8), while range ( ), 11, 2) returns 1, 3, 5, 7, 9. range(start, stop) is the same as range(start, stop, 1).
A for loop over integers is written as: for i in range(start, stop, step):
The following program writes the numbers from 1 to 100:

for number in range( 1 , 101 , 1 ):
print(number)
An integer to be added to the current number to generate the next number is called a step. If left out, it defaults to 1. So, the previous program can be written as:
for number in range(1,101):
print(number)

Simple Programs

Example 26.
Program to print numbers 1 to 10 using for loop.

# A loop to print values 1 to 10
for i in range(1,11,1):
print(i, end =” “)RUN
>>>
1 2 3 4 5 6 7 8 9 10
>>>

Example 27.
Program to print even number series up to 20.

# A loop to print even number series up to 20
for i in range(0,21,2):
print (i, end =” 11)RUN
>>>
0 2 4 6 8 10 12 14 16 18 20
>>>

Example 28.
Program t0 print sum of even number series up to 10.

# Print sum of even number series up to 10
sum=0
for i in range(0,11,2) :
sum +=i
print(“Sum of even numbers <= ” , i , “is” , sum )RUN
>>>
Sum of even numbers <= 0 is 0
Sum of even numbers <= 2 is 2
Sum of even numbers <= 4 is 6
Sum of even numbers <= 6 is 12
Sum of even numbers <= 8 is 20
Sum of even numbers < = 10 is 30
>>>

Let’s Try

Write a program to print the sum of the odd number series up to 11.

Example 29.
Program to generate multiplication table of the entered number.

# Generation of Multiplication Table
num =int(input(“Enter number for which you want table : “))
for i in range(1,11):
print(num, ‘X’, i,’=’, num * i )RUN
>>>
Enter number for which you want table : 5
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
>>>

Example 30.
Program to print letters of a word.

# Printing letters of word
letter = input(“Enter word : “)
for word in letter:
print(word)RUN
>>>
Enter word: Python
P
y
t
h
o
n
>>>

Example 31.
Program to print ASCII code for the entered message.

# Printing ASCII code
message = input(“Enter message to encode : “)
print(“ASCII code for message :”)
for ch in message:
print(ord(ch),end =” “) # ord function returns the numeric codeRUN
>>>
Enter a message to encode: Apple
ASCII code for the message :
65 112 112 108 101
>>>

Example 32.
Program to find sum of natural numbers up to specified number.

# Program to input number and find sum of all natural numbers up to given number num = int(input(“Enter any number : “))
sum= 0
for i in range(l,num+l):
sum = sum+i
print(“Sum of all natural numbers up to “,num ,” is “/Sum)RUN
>>>
Enter any number: 5
The Sum of all natural numbers up to 5 is 15
>>>

Example 33.
Program to check whether the entered number is prime or not.

# Program to check number is prime or not
num = int(input(“Enter any number: “))
# prime number is always greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
print(num, “is not a prime number”)
break
else:
print(num, “is a prime number”)# if number is less than or equal to l,then it is not prime number
else:
print(num, “is not a prime number”)RUN
>>>
Enter any number: 5
5 is a prime number
>>>
Enter any number: 8
8 is not a prime number
>>>

Example 34.
Program to print all the Prime Numbers within a given range.

# Program to print all the Prime Numbers within a given range
num=int(input(“Enter upper limit: “))
for a in range(2,num+1):
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k<=0):
print(a)RUN
>>>
Enter upper limit: 17
2
3
5
7
11
13
17
>>>

Example 35.
Program to display first ten Mersenne numbers. Numbers in the form 2n-1 are called Mersenne numbers. For example, the first Mersenne number is 21-1 = 1, the second Mersenne number is 22-1 = 3, etc.

# Program to display first ten Mersenne numbers
print(“First ten Mersenne numbers are :”)
for n in range (1,11):
mersenne = 2**n-1
print(mersenne, end=’ ‘)RUN
>>>
First ten Mersenne numbers are :
1 3 7 15 31 63 127 255 511 1023
>>>

Leave a Reply

Your email address will not be published. Required fields are marked *