mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-06 14:56:54 +02:00
Update python.html.markdown
range() is not a generator in python 2.x
This commit is contained in:
@@ -523,11 +523,13 @@ def double_numbers(iterable):
|
|||||||
# Instead of generating and returning all values at once it creates one in each
|
# Instead of generating and returning all values at once it creates one in each
|
||||||
# iteration. This means values bigger than 15 wont be processed in
|
# iteration. This means values bigger than 15 wont be processed in
|
||||||
# double_numbers.
|
# double_numbers.
|
||||||
# Note range is a generator too. Creating a list 1-900000000 would take lot of
|
# Note xrange is a generator that does the same thing range does.
|
||||||
# time to be made
|
# Creating a list 1-900000000 would take lot of time and space to be made.
|
||||||
_range = range(1, 900000000)
|
# xrange creates an xrange generator object instead of creating the entire list like range does.
|
||||||
|
_xrange = xrange(1, 900000000)
|
||||||
|
|
||||||
# will double all numbers until a result >=30 found
|
# will double all numbers until a result >=30 found
|
||||||
for i in double_numbers(_range):
|
for i in double_numbers(_xrange):
|
||||||
print(i)
|
print(i)
|
||||||
if i >= 30:
|
if i >= 30:
|
||||||
break
|
break
|
||||||
|
Reference in New Issue
Block a user