Home Python Python Range Tutorial: Step-by-Step Guide for Beginners

Python Range Tutorial: Step-by-Step Guide for Beginners

If you have to repeat something a certain number of times, then you can use loops and with loops, you can add range to your arsenal to do lots of other cool things with it. This article is all about a built-in function (class) called range in python.

Hello Guys,

Welcome to this new article on Python Range. In this article, we are going to deep dive into the range function in python.

This article is a one-stop guide for everything related to python range. Let us first start by discussing the python range a little bit.

What Will I Learn?šŸ’ show

What is Python Range?

Python range is a Class that generates a sequence of numbers starting from some number (say a) all the way to some number (say b) incremented by some number (say c). Hence, in other words, the generated sequence of numbers would look like a, a + c, a + 2c, ā€¦ā€¦

It will go until the result exceeds the upper number b.

Version Difference

This same function range was called xrange in the python 2 while in python 3, it is named this. But the syntax of this function was and always has been the same despite the name difference.

Let us now see how to use range in python. Let us take an example where we would like to generate a sequence from 1 to 20. To generate that, let us perform the following steps:

First, start by opening the python IDLE and then in the python shell type the following commands:

Python Range

The above command starts by first typing the name of the class ā€œrangeā€ and then followed by parenthesis which is then given two numbers as an argument called 1 and 21.

Here, the first argument marks as the ā€œstartā€ of the sequence while 21 marks as the end of the argument. But the result doesnā€™t seem promising. The returned result is a range object which can then be used for all sorts of different purposes.

One such purpose is to iterate through the range and print the numbers one by one. We can do that quite easily in python as follows:

Python Range Tutorial: Step-by-Step Guide for Beginners

Here, first, we used a for loop to access the numbers in the range object one by one and print them one by one. There are a few things worth noting here.

First, if you look closely at the syntax of the for loop, you would find out that the syntax of looping through is similar to the way we loop through a list.

If we just replace the range object with the name of any list, then the above code will work just in a similar manner but then, in that case, it would print the elements of the list.

So, in that sense, a natural question one might ask is whether range objects and list are both similar in some way. The answer to this question is ā€œYesā€. They are similar because both of them are what I call ā€œiterableā€ in python.

Iteratable in python is a type of data structure that can be easily looped. The examples of iterable are Lists, sets, tuples, and what we have just learned, range objects.

Then, the second thing that we should notice is that the last number (which is 21) is not there in the final print statement. The reason for this is because in python, rangeā€™s last value is always excluded.

Hence, if you want values till 5, then you should pass 6 as the last value. I hope you get the idea.

Now, let us call the python rangeā€™s Class once again in the for loop but this time without the first argument (which is 1). Let us see what the result looks likeā€¦

Python Range

Now, here the only difference in the output is that the printing starts from 0 instead of 1. The reason earlier one appeared because we explicitly passed 1 to the start argument of the range object.

But, here, we didnā€™t specify any start argument and we just passed one argument to the range constructor. When we do that, range assumes that the start is 0 and the number passed is ended. Also, the number passed will be excluded.

Now, there might be yet one another question.

If the range object is also an iterator just like a list, then why the results are different when we print range object and list.

When we print a range object, the print is just a range object as we have seen in one of the earlier screenshots.

You may like to read our guide on Python Functions: The Definitive Guide.

How can I print a range object as a list?

This is actually quite simple. We just need to pass the range object as an argument to the list constructor function. This has been demonstrated in the following screenshotā€¦

Python Range Tutorial: Step-by-Step Guide for Beginners

Here, we first created a range object and used that object as an argument to the list constructor, and the result we can clearly see a list starting from 1 all the way to 20.

What if there was no range class in Python?

If there was no range class in python, then we would have implemented it ourselves whenever we needed that.

Say, for example, assuming that there is no range class in python, that we need to generate a sequence of numbers starting from 1 to 20. Then, we might be able to do that using the following codeā€¦..

Python Range Tutorial: Step-by-Step Guide for Beginners

We can clearly see that we have to write around 6-7 lines of code just to create a sequence of numbers starting from 1 to 21. We can easily achieve this same task by just using the inbuilt range class as followsā€¦.

Python Range

The highlighted code is what we need to generate numbers starting from 1 all the way to 21. So, obviously this range class does help.

How can range objects be used to iterate through a list?

Now, let us understand how we can use range objects to iterate through a list.

There is already a very simple way of iterating through a list in python and that is using the ā€œfor-inā€ keyword in python. One very simple example of that is displayed hereā€¦

Python Range Tutorial: Step-by-Step Guide for Beginners

Now, the question is how can I use a range object to accomplish the same task. It is quite easy. We just need to remember the fact that the listā€™s indexing starts from 0 and it goes all the way to the length of the list subtracted by 1.

Owing to these facts, the following is the code for iterating through a list just by using range objectā€¦.

Python Range

Here, first, we accessed the length of the list and then created a range object using that length. Then, we looped through this range object and accessed each value by I which will serve as the index of the list and this index has been used as a filtering index for the list.

Syntax of the range class

The range class in python has three (including nonoptional) parameters. Those parameters are ā€œstartā€, ā€œendā€ and the ā€œstepā€.

The start parameter is an optional parameter that is not required as we have already seen in the tutorial. If this value is not set, then the default value of 0 is taken.

The end parameter is a parameter which is always required. So, we need to specify exactly where the range object will end. Now, there is one more optional argument called step which takes a default value of 1 which means the sequence will have an interval of 1. But just like other arguments, we can change this step argument a little bit.

Let us say for example we want to generate a sequence of all the odd and even numbers between 1 and 100. How would we go about it?

The following code shows how we might approach this problem using the range object in python.

Python Range Tutorial: Step-by-Step Guide for Beginners

All the other things are the same, we just changed the start argument of the range and the remaining things are just the same. We took a step length of 2. For generating odds, we took it as 1 while for generating Even, we took it as 2.

Now, there is one more thing that we should know with steps and it is that it can be negative too. Let us take an example where the step is negative.

Python Range Tutorial: Step-by-Step Guide for Beginners

Here, I have printed the even numbers in a reverse order. So, the start was 100 and the end was 1 while the step length is -2.

Range doesnā€™t work with floats

So, far we have discussed a lot of things about this range function. We saw that it has three different types of arguments called ā€œstartā€, ā€œendā€ and ā€œstepā€. The one important thing is that this function never accepts any float argument.

You can try out this function with any float value but this function will simply not work and will throw an error.

But there might be some real situations where you actually need a range with float, then in those scenarios, you would be better off by using some external library functions such as numpyā€™s arange function which is just like range but works just as efficiently with float values too.

Just to show all of you guys, that when the range function is used with float value, what type of output do we getā€¦.

Python Range Tutorial: Step-by-Step Guide for Beginners

Now, you can clearly see from the above screenshot that when we use float input to the range, then the python throws an error.

You may like to read our guide on 25 Top Python Blogs To Follow To Stay Updated.

Underscore keyword in Python for storing variables

Python range is often used for repeating a certain portion of code a certain number of times. In those cases, when we use range, instead of using a variable such as i, many a times, an underscore (_) character is used. Let us quickly see an example of this:

Python Range Tutorial: Step-by-Step Guide for Beginners

Here, we just wanted to repeat a piece of code a certain number of times (10 in this case).

Another way of accessing the range values

Earlier we discussed how range objects and lists are very similar to each other. We also showed how a range object can be converted to a list.

But what if you donā€™t want to convert range objects to a list but rather be interested in directly accessing the range values. Is there any way to do this?

Yes, there is.

We can access values of range objects just as similarly as we do this for lists. We can access those using the indexing method. The following screenshot demonstrates that onlyā€¦.

Python Range Tutorial: Step-by-Step Guide for Beginners

In the above example, we first created a range object storing values from 0 to 10 and then accessed them using the square bracket indexing method of python. In particular, we assessed values at the last index and 2nd index.

Using range to loop over a string character

The string in python is also considered as an iterable just like a list. The individual characters of that string can be accessed using a loop as follows.

Python Range Tutorial: Step-by-Step Guide for Beginners

Now, the same can be accomplished using the range as follows:

Python Range Tutorial: Step-by-Step Guide for Beginners

Here, we used the range with lento produce that same exact result.

Okay, now we are almost done with this article. We only need to discuss a few more use cases and problems based on range.

Problem. 1 You have been given a list of values such as 76,23,45,45,89,100. Calculate the sum of these values using the range function.

Answer Code: Here is the answer code for this particular problem.

python range function

Problem. 2 Use range to print individual words of the text ā€œHe is a great guyā€.

Answer Code: Here is the answer code of the above problem.

Python Range Tutorial: Step-by-Step Guide for Beginners

Here, we first stored the text in a separate variable and used a split method of string to split the text into words separated by space. Then, finally, we used range to loop through the indexes and then printed the words in the text.

Now itā€™s time to explore our Python Range() Function video tutorial guide.

Python Range Function(): The Definitive Beginners Guide

šŸ“—FAQā€™s

What does range () do in Python?

range() is a built-in function in Python that returns a sequence of numbers, starting from 0 by default and incrementing by 1 (also by default), and stopping before a specified number.

How do you write a range in Python?

To write a range() in Python, you can use the syntax range(start, stop, step) where start is the starting value (inclusive), stop is the stopping value (exclusive), and step is the increment between each value (default is 1).

How do you start a range at 1 in Python?

To start a range at 1 in Python, you can use the syntax range(1, stop) where 1 is the starting value (inclusive) and stop is the stopping value (exclusive).

What are the 3 values in range Python?

The three values in a range() function in Python are start, stop, and step.

Why do we use range () function?

We use the range() function in Python to generate a sequence of numbers that can be used in loops or other iterations. It is a convenient way to generate a sequence of numbers without having to type them all out manually.

What a range () function does give an example?

An example of range() function in Python is range(0, 10, 2), which generates the sequence of even numbers from 0 to 8 (inclusive), incrementing by 2 each time.

Does range () return a list?

No, range() does not return a list in Python. It returns a sequence object that can be used in a for loop or converted to a list using the list() function.

How do I calculate range?

To calculate the range of a set of numbers, subtract the lowest number from the highest number. For example, the range of the set {1, 2, 3, 4, 5} is 5-1=4.

How do you range from 1 to 10 in Python?

To range from 1 to 10 in Python, you can use the syntax range(1, 11), which generates a sequence of numbers from 1 to 10 (inclusive).

What is string [- 1 out of range in Python?

The error message ā€œstring index out of rangeā€ in Python occurs when you try to access a character in a string using an index that is greater than or equal to the length of the string.

How do you print a range of numbers in Python?

To print a range of numbers in Python, you can use a for loop in combination with the range() function. For example:

for i in range(1, 11):
print(I)

This will print the numbers 1 through 10 (inclusive) on separate lines.

What does += mean in Python?

In Python, += is an assignment operator that adds the value on the right to the value on the left and then assigns the result back to the value on the left. For example, x += 1 is equivalent to x = x + 1.

What is the range of int in Python?

In Python, int is an object that represents an integer, and it can have a range of values from -2147483648 to 2147483647 (inclusive) on most platforms.

What is range of object Python?

The range of an object in Python depends on the specific type of object. For example, the range of a list is determined by the number of elements in the list.

What does for _ in range mean in Python?

The _ in a for loop in Python is a variable that is used to indicate that the loop variable is not needed. In the case of for _ in range(), it is a way to generate a sequence of numbers without using the loop variable.

Is range a data type in Python?

No, range() is not a data type in Python. It is a built-in function that returns a sequence of numbers.

How do you check if a number is in range in Python?

To check if a number is in a range in Python, you can use the in keyword. For example:

if x in range(1, 11):
print(ā€œx is in rangeā€)

This will print ā€œx is in rangeā€ if x is between 1 and 10 (inclusive).

What is a simple example of range?

A simple example of range() in Python is range(5), which generates the sequence of numbers from 0 to 4 (inclusive).

How do you index a range in Python?

You cannot index a range() object directly in Python because it is not a list or tuple. However, you can convert it to a list or tuple and then index that object. For example:

my_range = range(1, 11)
my_list = list(my_range)
print(my_list[0]) # prints 1

What is the difference between range and function?

range() is a built-in function in Python that generates a sequence of numbers, while a function is a block of code that performs a specific task.

How does range work?

range() in Python generates a sequence of numbers starting from a given start value (inclusive) and stopping before a given stop value (exclusive), incrementing by a given step value (default is 1).

How do you turn a range into a list?

To turn a range() into a list in Python, you can use the list() function. For example:

my_range = range(1, 11)
my_list = list(my_range)
print(my_list) # prints [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

What is the difference between range and list?

range() in Python generates a sequence of numbers, while a list is a collection of values. A range() object is more memory-efficient than a list because it generates the numbers on the fly as they are needed.

How do you find the range of data types?

The range of a data type in Python depends on the specific type of data. For example, the range of an integer is determined by the number of bits used to represent it.

What is the range of the values?

The range of values in Python depends on the specific type of data being used. For example, the range of integers is determined by the number of bits used to represent them.

What is the range of the data set?

The range of a dataset in Python is the difference between the highest and lowest values in the dataset.

What does range 5 return in Python?

range(5) in Python returns a sequence of numbers from 0 to 4 (inclusive).

What is class range Python?

range is a built-in class in Python that generates a sequence of numbers. It is often used in loops and other iterations.

What does 1 for I in range ()) mean in Python?

1 for i in range() is not a valid statement in Python. It should be `1 for i in range

How do you use a range in a string?

You cannot use a range() directly in a string in Python, but you can use it to generate a sequence of numbers that can be used to index the characters in a string. For example:

my_string = ā€œhelloā€
for i in range(len(my_string)):
print(my_string[I])

This will print each character in the string on a separate line.

What is the range of string data?

The range of string data in Python depends on the specific string being used. The range is determined by the number of characters in the string.

What is string range limit?

There is no specific ā€œstring range limitā€ in Python. The range of a string is determined by the number of characters in the string.

How do I print a specific range?

To print a specific range of numbers in Python, you can use a for loop in combination with the range() function and an if statement. For example:

for i in range(1, 11):
if i >= 5 and i <= 8:
print(I)

This will print the numbers 5 through 8 (inclusive) on separate lines.

How do you print a range of values?

To print a range of values in Python, you can use a for loop in combination with the range() function. For example:

for i in range(1, 11):
print(I)

This will print the numbers 1 through 10 (inclusive) on separate lines.

How do you print a range in Python without a loop?

To print a range in Python without a loop, you can convert the range() object to a list and then use the join() method of the string object to concatenate the values. For example:

my_range = range(1, 11)
my_list = list(my_range)
print(ā€ ā€œ.join(str(x) for x in my_list))

This will print the numbers 1 through 10 (inclusive) separated by spaces.

What does += and -= mean in Python?

+= and -= in Python are assignment operators that add or subtract the value on the right to the value on the left and then assign the result back to the value on the left. For example, x += 1 is equivalent to x = x + 1, and x -= 1 is equivalent to x = x - 1.

What does == mean in Python?

== in Python is a comparison operator that checks if two values are equal. For example, x == y returns True if x and y have the same value.

What is += 1 in Python?

+= 1 in Python is a shorthand way of adding 1 to a variable. It is equivalent to x = x + 1.

What is integer range?

Integer range refers to the range of values that can be represented by an integer data type in Python. The range is determined by the number of bits used to represent the integer.

What is the example of range in Python?

An example of range() in Python is range(1, 11, 2), which generates the sequence of odd numbers from 1 to 9 (inclusive), incrementing by 2 each time.

What is range vs Len in Python?

range() generates a sequence of numbers, while len() returns the number of elements in a collection such as a list or string.

What does for i in range 5 mean?

for i in range(5) in Python is a loop that iterates 5 times

What does _ and __ mean in Python?

In Python, _ and __ are special variables that have specific meanings. _ is commonly used as a placeholder for values that are not needed or used later in the code. __ is used to indicate private attributes or methods in classes.

Is range in Python only for integers?

Yes, range() in Python is only used for generating sequences of integers.

Is a range a variable?

range() in Python is a function that generates a sequence of numbers. It is not a variable, but the sequence it generates can be stored in a variable.

How do you see if a number is in a range?

To check if a number is in a range in Python, you can use the in keyword. For example:

if x in range(1, 11):
print(ā€œx is in rangeā€)

This will print ā€œx is in rangeā€ if x is between 1 and 10 (inclusive).

How do you check if a number is in a given range?

To check if a number is in a given range in Python, you can use the in keyword. For example:

if x in range(start, stop):
print(ā€œx is in rangeā€)

This will print ā€œx is in rangeā€ if x is between start and stop (inclusive).

How do you read the range of a line in Python?

To read the range of a line in Python, you need to know what the range refers to. If it refers to a range of integers, you can use the range() function to generate the sequence of numbers. If it refers to a range of characters in a string, you can use slicing to extract the characters in that range.

Where do we use range?

range() in Python is commonly used in loops and other iterations where a sequence of numbers is needed.

Is range an input or output?

range() in Python is an output because it generates a sequence of numbers as output.

Where is range used?

range() in Python is used in loops and other iterations where a sequence of numbers is needed.

How do you create a set range in Python?

To create a set range in Python, you can use the set() function in combination with the range() function. For example:

my_range = range(1, 11)
my_set = set(my_range)
print(my_set)

This will create a set of the numbers 1 through 10 (inclusive).

How do you set an array range in Python?

To set an array range in Python, you can use the array() function in the array module. For example:

import array
my_array = array.array(ā€˜iā€™, range(1, 11))
print(my_array)

This will create an array of the numbers 1 through 10 (inclusive).

Whatā€™s an example of a range of a function?

An example of the range of a function in Python is the function f(x) = x^2. The range of this function is all non-negative real numbers because the square of any real number is non-negative.

Why is range function used?

The range() function in Python is used to generate a sequence of numbers for use in loops and other iterations.

How does range work in Python?

range() in Python generates a sequence of numbers starting from a given start value (inclusive) and stopping before a given stop value (exclusive), incrementing by a given step value (default is 1).

What are the parameters of range in Python?

The parameters of range() in Python are:

  • start (optional): the start value of the sequence (default is 0)
  • stop: the stop value of the sequence (exclusive)
  • step (optional): the increment between each number in the sequence (default is 1)

Does Python range return a list?

No, range() in Python returns a range object, which is a sequence of numbers.

Does Python range create a list?

No, range() in Python does not create a list by default. However, you can convert the range object to a list using the list() function.

Is range() a list in Python?

No, range() in Python is not a list. It is a sequence of numbers that can be converted to a list using the list() function.

What is the difference between range and array in Python?

range() in Python generates a sequence of numbers, while array() in Python creates an array of a specific data type with a specific number of elements.

Can you slice a range in Python?

No, you cannot slice a range in Python. However, you can convert the range to a list and then slice the list.

What data type is number range in Python?

The data type of a range in Python is range. It is a sequence of numbers that can be used in loops and other iterations.

What does range tell you about data?

range() in Python tells you the sequence of numbers that can be used in loops and other iterations. It gives you information about the range of integers that are available to use.

How do you solve range?

ā€œRangeā€ can mean different things in different contexts, so itā€™s difficult to provide a specific answer to this question without more information. If you are referring to the range of a dataset, you can calculate it by subtracting the minimum value from the maximum value.

What does value range mean in data type?

The value range of a data type refers to the range of values that can be stored in that data type. For example, the value range of the int data type in Python is -2^31 to 2^31-1.

How do you find the domain and range?

To find the domain and range of a function in Python, you need to define the function and then generate a sequence of inputs and outputs. The domain is the set of all possible inputs, and the range is the set of all possible outputs.

How do you find the range of a dataset in Python?

To find the range of a dataset in Python, you can use the min() and max() functions to find the minimum and maximum values, respectively, and then subtract the minimum from the maximum.

What does range 1 10 2 return?

range(1, 10, 2) in Python returns a sequence of odd numbers from 1 to 9 (inclusive), incrementing by 2 each time.

What sequence is generated by range 1 10 3?

range(1, 10, 3) in Python returns a sequence of numbers starting from 1 (inclusive) and stopping before 10 (exclusive), incrementing by 3 each time. The sequence is 1, 4, 7.

What is the output of range 3 15 3?

range(3, 15, 3) in Python returns a sequence of numbers starting from 3 (inclusive) and stopping before 15 (exclusive), incrementing by 3 each time. The output is the sequence of numbers 3, 6, 9, 12.

So, with this last discussion, I will stop right here and I hope that you have been able to follow through this tutorial effectively.

Thank you so much for reading the article. šŸ™‚