in Education by
Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about for i in range(0, 20): for i in xrange(0, 20): Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
range() and xrange() are two functions that can be used to iterate a certain number of times in a for loop in Python. xrange()is only used in, Python 2. So to run any code in both Python 2 and Python 3 you should use range() function. range() – It will create a list of values from depending on the range specified xrange() – It returns xrange objects that can be used to display numbers only by looping. The only a particular range is displayed on demand and hence called “lazy evaluation“. Now the point is why xrange() is faster as compared to range(). The reason is, at the time of the memory allocation range() takes more amount of memory as compares to xrange (). The basic reason for this is the return type of range() is a list which is large in size and the return type of xrange() is xrange() object which needs less amount of memory. An example that illustrates all the above points:- import sys a = range(1,10000) x = xrange(1,10000) print ("The size allotted using range() is:") print (sys.getsizeof(a)) print ("The size allotted using xrange() is : ") print (sys.getsizeof(x)) To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
    What makes using range() to make the process of generating a quadrillion values in a instant of time, I am amazed by ... o += 1 return Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Is there a way to step between 0 and 1 by 0.1? I thought I could do it like the following, but it failed: ... , which I did not expect. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    What is the difference between xrange and range in Python?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    I think the slice notation are sort of powerful but I don't know much about it, So can anyone give me a ... Python's slice notation? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have a list of objects in Python and I want to shuffle them. I thought I could use the random.shuffle method, ... (b) This will fail. Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    How can someone access the index itself for a list like the following : intg = [8, 23, 45, 12, 78, 90] When ... 1 to 6 in this case? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    Our core application changed from Python 2.6 to Python 2.7 maybe to Python 3 in later release. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    ___ and ___ functions used to find the minimum and maximum value in a range. 1. Min , Max 2. Fast , Slow 3. ... , Filter 4. Less , More Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    _____and _____ functions used to find the minimum and maximum value in a range. Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    Predicting y for a value of x that's outside the range of values we actually saw for x in the original ... Regression of R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    write a program in Python to print the sum of the following series: s=1+x+x^2+x^3+…..+x^n Select the correct answer from above options...
asked Dec 9, 2021 in Education by JackTerrance
0 votes
    I've spent the past few hours reading around in here and elsewhere, as well as experimenting, but I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle ... x += 1 print(Comm) Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    It appears that "if x" is almost like short-hand for the longer "if x is not None" syntax. Are they ... would be great to know. Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
...