| by Arround The Web | No comments

How to Use Xrange in Python

The “xrange()” function is useful when you want to perform a particular task a specific number of times. The “for” loop is normally used with the “xrange()” function to iterate through a series/sequence of numbers. The “xrange()” function is used in Python 2.x which is replaced by the “range()” function in the later versions instead.

A detailed description of how to use “xrange()” in Python will be discussed in this post. Let’s begin with the following contents:

What is the “xrange()” Function in Python?

The “xrange()” function retrieves a range object (a type of iterable) that represents a sequence of numbers. It can be used in a “for” loop to execute a code block for each value in the sequence.

Syntax

xrange(start, end, step)

 

In the above syntax:

  • The optional parameter named “start” indicates the starting point of the sequence(default value is 0).
  • The mandatory parameter “end” specifies the stopping point of the sequence.
  • The optional parameter “step” is used to indicate the interval between numbers in the sequence (default value is 1).

Note: Python 3 does not have an “xrange()” function, but the “range()” function behaves like “xrange()” in this version. In order to write code that runs/executes on both Python 2 and Python 3, you should utilize the “range()” function instead of “xrange()” function.

Demonstration of “xrange()” in Python 3

Let’s overview the following code implementing the “xrange()” function:

Here, it can be analyzed that the error is encountered upon utilizing the “xrange()” function in Python 3. To streamline the limitation in this version, use the “range()” function instead.

Example 1: Applying the “range()” Function by Using the Start and End Parameters

The below example code is utilized to generate/create the sequence within a specified range:

for i in range(0,5):
 print(i)

 

In the above code, the “for” loop is used to iterate over the specified range and display the sequence using the “print()” function.

Output

The specified range has been displayed successfully.

Example 2: Applying the “range()” Function by Using the End Parameter Only

This example is used to generate the range by taking only the end parameter value such that the loop iterates and displays the values till the specified end parameter, thereby excluding it:

for i in range(5):
 print(i)

 

In the above code, the “for” loop iterates through the range and displays each of the values till the specified end parameter(excluded).

Output

As seen, the sequence has been created in accordance with the specified end parameter.

Example 3: Applying the “range()” Function by Using the Start, End, and Step Parameters

The below code generates the range by taking the start, end, and step values as function parameters:

for i in range(0, 20, 3):
 print(i)

 

In the above lines of code, the “for” loop iterates over the specified range and displays the sequence with step value using the “print()” function.

Output

This output signifies that the specified range is generated in accordance with the provided steps.

Conclusion

The “xrange()” function in Python 2.x or “range()” function in Python 3.x is used for efficient iteration over a range of values. The “range()” or “xrange()” functions can increase the code’s performance when dealing with large datasets or when dealing with loops that require many iterations. This post explained a comprehensive guide on how to use “xrange()” in Python using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply