Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

The range() Function in Python

Last updated by Ashwin Ramachandran on Sep 25, 2024 at 11:02 PM | Reading time: 5 minutes

The fast well prepared banner

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

The range() Function in Python
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

Python supports object-oriented programming and has a concise, readable, and easy-to-learn syntax. It is no wonder that it is one of the most popular programming languages. An integral part of Python are its built-in functions. 

We've written a series of articles to help you learn and brush up on the most useful Python functions. In this article, we’ll learn about Python's range() function and how to use it. 

If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Python String join() Method, Python Exit commands, and Type and Isinstance In Python for more content on Python coding interview preparation.

Having trained over 10,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!

At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.

Want to nail your next tech interview? Sign up for our FREE Webinar.

In this article, we’ll cover:

  • What Is the range() Function in Python and What Does It Do?
  • The range() Function in Python: Syntax
  • The range() Function in Python: Example
  • FAQs on the range() Function in Python

What Is the range() Function in Python and What Does It Do?

Python range() function is a built-in function we can use to generate a sequence of numbers within a given range. Through the number of arguments passed to the function, we can control where that sequence of numbers will begin and end, along with the difference between two successive numbers in the series. 

Python range() function is often used to iterate through a sequence using a for or while loop.

The range() Function in Python: Syntax


range(stopNumber)
range(startNumber, stopNumber)
range(startNumber, stopNumber, stepValue)

Parameters:

Python’s range() function takes three arguments:

  • startNumber (optional): The sequence of integers returned starts from this integer. If the starting integer is not provided, range() starts the sequence with zero (its default start value).
  • stopNumber: The sequence of integers that range() returns ends before this integer. The integer range ends at stopNumber – 1.
  • stepValue (optional): An integer value indicating the difference between each integer in the sequence. If we don’t pass the step value, then range() works with the default step value, one. The step value can be any integer other than zero. Passing zero as the step value will give an error.

Please note that:

  • When we call range() with one argument, we can choose where the sequence of numbers stops, starting at 0, with step 1 as default.
  • When we call range() with two arguments, we can choose where the sequence of numbers stops and starts. So our sequence doesn’t have to always start at zero. 
  • When we call range() with three arguments, we can decide where the sequence of numbers will start and stop as well as what the difference between two successive numbers in the sequence will be. The step value is a positive number for increments and a negative number for decrements.

Return Value:

Returns a sequence of integers with start, stop, and step values as decided by the parameters passed and defaults (for the parameters not provided).

The range() Function in Python: Example

Here, we take a look at how to use the range() function in Python next time you need it:

Code


# Using the range() function in Python

# With only stop parameter
print("Integers from 1 to 10-1 are: ")
for number in range(10):
    print(number, end=" ")
print()

# Iterating through a sequence using a for loop and len() as the stop parameter in range()
example = [1, 3, 5, 7]
print("All the numbers in the sequence example are: ")
for number in range(len(example)):
    print(example[number], end=" ")
print()

# With start and stop parameter
print("Integers from 5 to 10-1 are: ")
for number in range(5, 10):
    print(number, end=" ")
print()

# Calculating factorial
factorial = 1
for i in range(1, 6):
    factorial = factorial * i
print("The factorial of 5 is:", factorial)

# With start, stop, and step parameter
print("Integers from 1 to 10-1, with a step value of 2 are: ")
for number in range(1, 10, 2):
    print(number, end=" ")
print()

# Printing numbers divisible by 5
print("Integers divisible by 5 in the range [0, 50): ")
for number in range(0, 50, 5):
    print(number, end=" ")
print()

# Using a negative step value
print("Integers from 50 to 0+1, with a step value of -5 are: ")
for number in range(50, 0, -5):
    print(number, end=" ")
print()

# Accessing the proper list returned by range using an index

listReturned= range(50, 0, -5)
print("The number on index 3 in the above example is: ")
print(listReturned[3])

Output


Integers from 1 to 10-1 are: 
0 1 2 3 4 5 6 7 8 9 
All the numbers in the sequence example are: 
1 3 5 7 
Integers from 5 to 10-1 are: 
5 6 7 8 9 
The factorial of 5 is: 120
Integers from 1 to 10-1, with a step value of 2 are: 
1 3 5 7 9 
Integers divisible by 5 in the range [0, 50): 
0 5 10 15 20 25 30 35 40 45 
Integers from 50 to 0+1, with a step value of -5 are: 
50 45 40 35 30 25 20 15 10 5 
The number on index 3 in the above example is: 
35

Found this article helpful? You can learn about more Python functions on the learn page. 

FAQs on the range() Function in Python

Q1. What does the range() function in Python generate?

The Python range() function returns a series of integers with the start, stop, and step values as decided by the parameters passed and defaults (for the parameters not provided). The default start value is zero, and the default step value is one. 

Q2. What is the default start value in the range() function in Python?

The default value of the start parameter is zero in the range() function in Python.

Q3. What is the default step value in the range() function in Python?

The default value of the step parameter is one in the range() function in Python. 

Q4. How do you use the range() function in Python?

The syntax for range() is range(startNumber, stopNumber, stepValue), with startNumber and stepValue being optional parameters.

Q5. Does the end value parameter include itself in the range() function?

No. The range ends at endValue-1 and does not include the endValue itself in the Python range() function.

Ready to Nail Your Next Coding Interview?

Whether you’re a coding engineer gunning for a software developer or software engineer role, a tech lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

Last updated on: 
November 20, 2024
Author

Ashwin Ramachandran

Head of Engineering @ Interview Kickstart. Enjoys cutting through the noise and finding patterns.

Attend our Free Webinar on How to Nail Your Next Technical Interview

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
First Name Required*
Last Name Required*
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

The range() Function in Python

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar
entroll-image