Range Function()
What is range()Function in Python:
range() function is built-in function in Python is used when user wants to perform an action a specific number of times. It is used in for loop to generate a sequence of numbers.
for example
range(100)
will generate numbers from 0 to 99.
In range () we can also give start, stop and step_size.
Syntax:
1. range(stop) # takes one argument
Example:
for i in range(5):
print(i)
o/p:
0
1
2
3
4
In the above example only stop value is used here total 4 numbers will display starting from 0.
2. range(start, stop) # takes two arguments
Example:
for i in range (100,105):
print(i)
O/P:
100
101
102
103
104
In the above example at 0th index first number i.e. start number 100 will store and on 4th index the last number 104 will store because range ends on 105.
# To display the sum of first 10 natural even numbers using for loop
sum=0
for i in range(1, 10 ):
if i%2==0:
sum=sum+i
print(sum)
o/p:
0
2
2
6
6
12
12
20
20
3. range (start, stop, step_size) #takes three arguments
start means starting value of sequence at 0th index, stop means end value and step_size means increment value.
range starting by 0 by default and increments by 1 by defaults if not any start and step size is mentioned
Example:
for i in range(5, 55, 5):
print(i)
O/P:
5
10
15
20
25
30
35
40
45
50
In above example the start value is 5 , end value is 55 and increment value is 5. It will display starting from 5 and ends on 5o because in last increments the number will be 55 but our end number is 55. So it will not display.
# Program to display Multiplication table
n=int(input("Which table you want to display\t"))
print(n,"Table is :\n")
for i in range(1,11,1):
product=n*i
print(product)
o/p:
Which table you want to display 11
11 Table is :
11
22
33
44
55
66
77
88
99
110
0 टिप्पण्या
कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏