From c056fc18dd8f0af914610c4d6af3508c5c8c14de Mon Sep 17 00:00:00 2001 From: Pravin Pratap Date: Thu, 1 Nov 2018 09:41:43 +0530 Subject: [PATCH] Added difference in range() between Python 2 and 3 (#20629) Added the differences between the working methods of the range() function in Python 2 and 3, including the explanation of the "lazy" method of generating a list of numbers. --- guide/english/python/range-function/index.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/guide/english/python/range-function/index.md b/guide/english/python/range-function/index.md index 15f992c05d..2ed6071cc6 100644 --- a/guide/english/python/range-function/index.md +++ b/guide/english/python/range-function/index.md @@ -39,3 +39,11 @@ for i in range(3, 12, 2): 9 11 ``` + + #### Notes + In Python 2, there are 2 functions for going through a range of numbers: range() and xrange(). + Out of these functions, xrange() is the "lazy" function, meaning it generates numbers as necessary instead of actually creating + a list of numbers and iterating through them. range(), on the other hand, makes an entire list of numbers and iterates through + this list. This makes it a strain on the memory in the case of really long lists. + + In Python 3, the range() function mimics xrange() as the "lazy" variant, and xrange() itself has been removed.