Created
November 24, 2017 10:19
-
-
Save MishraKhushbu/7e7569c51c2f3504be287350180f54b1 to your computer and use it in GitHub Desktop.
24/nov/2017 Range/xrange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python2 --Range | |
1.It produces a list for itertions. | |
For x in range(3): | |
print x | |
O/p: | |
0 | |
1 | |
2 | |
y = range(5) | |
type(y) | |
<type 'list'> | |
x = [0,1,2,3,4] | |
=============================================== | |
Python2 --Xrange | |
for x in xrange(5): | |
print x | |
Output: | |
0 | |
1 | |
2 | |
3 | |
4 | |
//as if now working same as for range | |
y = xrange(3) | |
print y | |
>>output: | |
xrange(3) | |
->xrange function returns you a sequence object which have | |
to be iterated if it is required to be used. | |
>>> it = iter(y) | |
>>> it.next | |
<method-wrapper 'next' of rangeiterator object at 0x0000000001D95F50> | |
>>> it.next() | |
0 | |
>>it.next() | |
1 | |
>>> it.next() | |
2 | |
>>> it.next() | |
Traceback (most recent call last): | |
File "<pyshell#12>", line 1, in <module> | |
it.next() | |
StopIteration ### when it reaches beyond the last iteration | |
**xrange doesn't create the list at once , it makes one by one. | |
**mainly benificial when lack of memory , the range creates a list all | |
together(for e.g for iterations over million of no.s e.g cell phone) | |
whereas xranges generators iterations when asked for. | |
============================================== | |
Python3 range | |
IN Python 3 | |
type(range) | |
<type range> | |
============== | |
Python3 xrange | |
IN Python 3 | |
type(range) | |
<type range> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment