Prof. Neeraj Bhargava
Vishal Dutt
Department of Computer Science, School of
Engineering & System Sciences
MDS University, Ajmer
range() Function
• To loop through a set of code a
specified number of times, we can
use the range() function.
• The range() function returns a
sequence of numbers, starting from
0 by default, and increments by 1.
• ends at a specified number.
Example:
for x in range(6):
print(x)
Output
0
1
2
3
4
5
• The range() function defaults to 0
as a starting value.
• however it is possible to specify
the starting value by adding a
parameter.
Like: range(2, 6)
• It means Loop starts from 2 to 6
(but not including 6)
Example:
for x in range(2, 6):
print(x)
Output
2
3
4
5
The range() function increment the
sequence by 1 by default.
It is possible to specify the
increment value by adding a third
parameter.
Like: range(2, 30, 3)
Example:
for x in range(2, 30, 3):
print(x)
Output
2
5
8
11
14
17
20
23
26
29
Factorial using for loop
num = int(input("enter a number: "))
fac = 1
for i in range(1, num + 1):
fac = fac * i
print("factorial of ", num, " is ", fac)
Factorial using while loop
num = int(input("enter a number: "))
fac = 1
i = 1
while i <= num:
fac = fac * i
i = i + 1
print("factorial of ", num, " is ", fac)
Questions
 Explain range() method in Python?
 Write a Python Script to print the series 2, 3, 4, 5… up to
15?
 Explain the various parameters of the range() with
suitable example.

Python decision making_loops_control statements part9

  • 1.
    Prof. Neeraj Bhargava VishalDutt Department of Computer Science, School of Engineering & System Sciences MDS University, Ajmer
  • 2.
    range() Function • Toloop through a set of code a specified number of times, we can use the range() function. • The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1. • ends at a specified number.
  • 3.
    Example: for x inrange(6): print(x) Output 0 1 2 3 4 5
  • 4.
    • The range()function defaults to 0 as a starting value. • however it is possible to specify the starting value by adding a parameter. Like: range(2, 6) • It means Loop starts from 2 to 6 (but not including 6)
  • 5.
    Example: for x inrange(2, 6): print(x) Output 2 3 4 5
  • 6.
    The range() functionincrement the sequence by 1 by default. It is possible to specify the increment value by adding a third parameter. Like: range(2, 30, 3)
  • 7.
    Example: for x inrange(2, 30, 3): print(x) Output 2 5 8 11 14 17 20 23 26 29
  • 8.
    Factorial using forloop num = int(input("enter a number: ")) fac = 1 for i in range(1, num + 1): fac = fac * i print("factorial of ", num, " is ", fac)
  • 9.
    Factorial using whileloop num = int(input("enter a number: ")) fac = 1 i = 1 while i <= num: fac = fac * i i = i + 1 print("factorial of ", num, " is ", fac)
  • 10.
    Questions  Explain range()method in Python?  Write a Python Script to print the series 2, 3, 4, 5… up to 15?  Explain the various parameters of the range() with suitable example.