The xrange function from Python 2 was renamed range in Python 3 and range from Python 2 was deprecated. The xrange function in Python 2 returned items through lazy evaluation, meaning that numbers were generated only when required, which used less memory. This process, thus, occupied a significant chunk of memory for large list sizes. The range function in Python 2 generated a list of numbers that you could iterate through. This tutorial focuses on Python 3, but if you’ve worked with Python 2 before some explanation is needed because the meaning of range changed between these two versions. If you’ve never worked with Python before, we’d recommend starting with this interactive Python fundamentals course first. Although range is useful for a broad variety of Python programming tasks, this guide will conclude with a couple of data science use cases for the range function.įor the purposes of this tutorial, we do assume you have at least some knowledge of Python syntax. In this detailed guide, we will walk you through the workings of the range function using examples, and discuss its limitations and their workarounds. In Python, an important component of loops is the built-in range function. Looping is an integral part of any programming language. So what's the difference? Well, in Python 2.x range() produced a list, and xrange() returned an iterator - a sequence object.OctoPython Range Tutorial: Learn to Use This Helpful Built-In Function This is a function that is present in Python 2.x, however it was renamed to range() in Python 3.x, and the original range() function was deprecated in Python 3.x. You may have heard of a function known as xrange(). If you're a little confused, for reference see the Wikipedia article. So take it down, pass it around, no more bottles of beer on the wall!īrilliant! Finally you can see the true power of Python :). So take one down, pass it around, 1 more bottle of beer on the wall!ġ bottle of beer on the wall, 1 bottle of beer! So take one down, pass it around, 2 more bottles of beer on the wall!Ģ more bottles of beer on the wall, 2 more bottles of beer! So take one down, pass it around, 96 more bottles of beer on the wall!ģ bottles of beer on the wall, 3 bottles of beer! So take one down, pass it around, 97 more bottles of beer on the wall!ĩ7 bottles of beer on the wall, 97 bottles of beer! So take one down, pass it around, 98 more bottles of beer on the wall!ĩ8 bottles of beer on the wall, 98 bottles of beer! Print(' more bottles of beer on the wall!'.format(i - 1))ĩ9 bottles of beer on the wall, 99 bottles of beer!
Print('So take one down, pass it around, 1 more bottle of beer on the wall!') Print('2 more bottles of beer on the wall, 2 more bottles of beer!') Print('So take it down, pass it around, no more bottles of beer on the wall!') Print('1 bottle of beer on the wall, 1 bottle of beer!') Python's range() Function Examples Simple Usage
For example range(0, 5) generates integers from 0 up to, but not including, 5. Therefore the last integer generated by range() is up to, but not including, stop. The syntax to access the first element of a list is mylist. range() (and Python in general) is 0-index based, meaning list indexes start at 0, not 1.All parameters can be positive or negative.step: Difference between each number in the sequence.stop: Generate numbers up to, but not including this number.start: Starting number of the sequence.stop: Number of integers (whole numbers) to generate, starting from zero.The range() function has two sets of parameters, as follows: The range() function works a little bit differently between Python 2.x and 3.x under the hood, however the concept is the same. Other times you may want to iterate over a list (or another iterable object), while being able to have the index available. Often you will want to use this when you want to perform an action X number of times, where you may or may not care about the index. But what does it do? In a nutshell, it generates a list of numbers, which is generally used to iterate over with for loops. Last Updated: Thursday 27 th January 2022 What is Python's range() Function?Īs an experienced Python developer, or even a beginner, you've likely heard of the Python range() function.