The range() function is a very popular and widely used function in Python, especially when you are working with for loops, and sometimes also with while loops. The range() function is worth knowing and mastering because doing so will open a lot of doors: range() is used in everything from controlling the flow of a program to looping through data sets that you are using for data analysis.
The range() function returns a sequence of numbers and is immutable, meaning its value is fixed. The range() function takes one or at most three arguments, namely the start and a stop value along with a step size.
range() was introduced in Python3. In Python2, a similar function, xrange(), was used, which had somewhat different behavior. Among other things, xrange() returned a generator object and consumed less memory, while range(), on the other hand, returns a list or sequence of numbers.
Part of the reason the range() function is useful is because it only stores the start, stop, and step values, so it consumes less memory when compared to a list or tuple.
Python range() Function Syntax
The range() function can be represented in three different ways, or you can think of them as three range() parameters:
range(stop_value): By default, the starting point here is zero.
range(start_value, stop_value): This generates the sequence based on the start and stop value.
range(start_value, stop_value, step_size): It generates the sequence by incrementing the start value using the step size until it reaches the stop value.
Python range() function. Image by Author.
We can also check the type of the range() function by wrapping range() in type().
my_range =range(100)print(type(my_range))# Expected output: <class 'range'>Работает на
Редактор с примерами для обучения, нажмите Tab для ввода. Нажмите Escape, затем Tab или Shift+Tab для навигации.
Let's now take a look at a few examples so you can practice and master using range().
Using Python range() to print a sequence of numbers
Let's start with a simple example of printing a sequence of ten numbers, which will cover your first range() parameter. To achieve this, you will be just passing in the stop value. Since Python works on zero-based indexing, hence, the sequence will start with zero and stop at the specified number, i.e., n-1, where n is the specified number in the range() function.
my_range =range(10)for num in my_range:print(num)# Expected output:# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9
The
range()function is a very popular and widely used function in Python, especially when you are working withforloops, and sometimes also withwhileloops. Therange()function is worth knowing and mastering because doing so will open a lot of doors:range()is used in everything from controlling the flow of a program to looping through data sets that you are using for data analysis.If you are just getting started in Python and would like to learn more, take DataCamp's Introduction to Data Science in Python course.
What is the range() Function in Python?
The
range()function returns a sequence of numbers and is immutable, meaning its value is fixed. Therange()function takes one or at most three arguments, namely thestartand astopvalue along with astepsize.range()was introduced in Python3. In Python2, a similar function,xrange(), was used, which had somewhat different behavior. Among other things,xrange()returned a generator object and consumed less memory, whilerange(), on the other hand, returns a list or sequence of numbers.Part of the reason the
range()function is useful is because it only stores thestart,stop, andstepvalues, so it consumes less memory when compared to a list or tuple.Python range() Function Syntax
The
range()function can be represented in three different ways, or you can think of them as threerange()parameters:range(stop_value): By default, the starting point here is zero.range(start_value, stop_value): This generates the sequence based on the start and stop value.range(start_value, stop_value, step_size): It generates the sequence by incrementing the start value using the step size until it reaches the stop value.We can also check the type of the
range()function by wrappingrange()intype().Python range() Function Examples
Let's now take a look at a few examples so you can practice and master using
range().Using Python range() to print a sequence of numbers
Let's start with a simple example of printing a sequence of ten numbers, which will cover your first
range()parameter. To achieve this, you will be just passing in thestopvalue. Since Python works on zero-based indexing, hence, the sequence will start with zero and stop at the specified number, i.e.,n-1, wherenis the specified number in therange()function.