Python

range() function in Python

Click for All Topics

range() function in Python

The range() function returns a list/sequence of numbers. It usually starts from 0 and goes up by 1 until it’s almost at a certain number you choose, either positive or negative. 

range() is used to create a sequence of numbers and by default it starts with 0 but we can change this default value. You tell it where to start, where to end, and how to step(step size).

Syntax :

range(start, stop, step)

Parameter:
  • start- the initial value of the series.(optional).
  • stop-  the next value after the final value of the series.
  • step-  value that indicates the difference between the numbers of the series.(optional)
 
Why:

We use the range() function in programming to easily create a series of numbers that we can use for various tasks. It’s particularly useful in loops when we want to repeat an action a specific number of times( or iterate through a sequence of numbers). This function helps us avoid writing out each number individually and makes our code more organized and efficient.

Example- range(stop)

				
					# welcome to edSlash.
# range(stop)
for num in range(6):
    print(num)
    
# Output -
# 0
# 1
# 2
# 3
# 4
# 5
				
			

Example-  range(start,stop)

				
					
# range(start,stop) 
for num in range(2,8):
    print(num)

# Output - 
# 2
# 3
# 4
# 5
# 6
# 7
				
			

Example –

				
					# How the same code will be written without range() function
start = 2
end = 8
step = 1

n = start
while n < end:
    print(n)
    n += step

# Output -
# 2
# 3
# 4
# 5
# 6
# 7
				
			
Step size:

Step size is a  parameter that lets you define the interval between consecutive values in the sequence.

 

💡Note: range(2,7,3) here 3 is step size which is generally 1 by default.

				
					# with step size 1 which is the default step size
for num in range(2,8):
    print(num)

# Output -
# 2
# 3
# 4
# 5
# 6
# 7
				
			
				
					# range(start,stop,step)
# with step size 2.
for num in range(2,8,2):
    print(num)

# Output -
# 2
# 4
# 6
				
			

Various scenarios in which range() is helpful:

Accessing List Elements: You can use the range() function to generate indices and then use those indices to access elements in a list or other data structures.

				
					data = [10, 20, 30, 40, 50]
for i in range(len(data)):
    print(data[i])

# Output -
# 10
# 20
# 30
# 40
# 50
				
			

While Loops: You can use the range() function with a while loop to perform actions a specific number of times or until a certain condition is met.

				
					count = 0
while count < 5:
    print(count)
    count += 1

# Output -
# 0
# 1
# 2
# 3
# 4
				
			

Conditional Checks: You can use the range() function to generate a sequence of numbers and then check if a particular value falls within that range.

				
					number = 15
if number in range(10, 20):
    print("Number is between 10 and 20")
else:
    print("Number is not between 10 and 20")

# Output -
# Number is between 10 and 20
				
			

Indexing: In certain situations, you might use the range() function to generate a sequence of indices that correspond to specific positions in a sequence.

				
					data = [10, 20, 30, 40, 50]
for i in range(len(data)):
    print(data[i])

# Output -
# 10
# 20
# 30
# 40
# 50
				
			

Creating Data: It can be used to generate data points for creating graphs, plots, or other visualizations.

				
					import matplotlib.pyplot as plt

x_values = list(range(0, 11))
y_values = [x**2 for x in x_values]

plt.plot(x_values, y_values)
plt.show()
				
			

💡Note: the primary purpose of the range() function is to generate sequences of numbers, and it can be applied creatively in various programming scenarios beyond just the common use with for loops.