![]() ![]() ![]() In the code, the start value is 3, and stop value is 10. The last value is always 1 less than the given value i.e. Since the start is not given the start is considered as 0 and the last value is given till 9. The value used in range is 10, so the output is 0 1 2 3 4 5 6 7 8 9 This example shows how to print the values from 0-9 using range(). The code execution is faster using xrange(). The usage of memory is more hence the code execution is slow when working on a huge set of data. ![]() The range() method uses more memory as the list returned has to be stored in comparison to xrange().Īs xrange() returns a generator object, it does not give values instantly and has to be used inside for-loop to get the values. The xrange() function gives a generator object that needs to be looped in a for-loop to get the values. The range() gives the sequence of numbers and returns a list of numbers. Python range() has been introduced from python version 3, before that xrange() was the function.īoth range and xrange() are used to produce a sequence of numbers.įollowing are the difference between range and xrange(): The return value is a sequence of numbers from the given start to stop index. step: (optional).The step value is the number by which the next number is range has to be incremented, by default, it is 1.The last value will be always 1 less than the stop value. It is a mandatory input to range function. stop: The stop index decides the value at which the range function has to stop.start: (optional) The start index is an integer, and if not given, the default value is 0.Example: Get even numbers using range(). ![]() Using floating numbers in Python range().Reverse Range: Decrementing the values using negative step.Incrementing the values in range using a positive step.The Python range()is a very useful command and mostly used when you have to iterate using for loop. In case the start index is not given, the index is considered as 0, and it will increment the value by 1 till the stop index.įor example range(5) will output you values 0,1,2,3,4. Python range() is a built-in function available with Python from Python(3.x), and it gives a sequence of numbers based on the start and stop index given. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |