Range Function | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Range Function

Does the range function (in Python) create/ store a list? When you supply the range function with 2 parameters does it increase by one in the output? What's the difference when using one parameter or parameters, because it does the same thing?

3rd May 2018, 9:39 PM
arbaaz
3 Respostas
+ 1
in py2 it creates a list but in py3 it creates an immutable sequence (God knows what that means but hey ho it says that on stack overflow). use 2 parameters when u want the sequence from x to y (say I want the range to go from 5 to 20 and not from 0 to 20) whereas when u use 1 it just creates a range from 0 to that number (not the number u write tho cos py starts from 0 and not one - take that into account)
3rd May 2018, 9:52 PM
annie
annie - avatar
+ 1
Thank you for your answers.
5th May 2018, 8:49 PM
arbaaz
0
1) In Python 2.x there were 2 range functions range() and xrange(). range() returned a list and xrange() used something called a generator which is a special feature which basically returns values without the need of making a list improving the efficiency that also meant the range can be HUGE and the speed should be more or less the same. In Python 3.x range() was removed, xrange() was renamed to range() so no, it doesnā€™t return a list anymore 2) range() function can have up to 3 arguments but it must have at least 1. If you give it 1 arg it will generate numbers between 0 and the number - 1, 2 args will generate numbers between num1 up to num2 - 1 so range(5, 10) would generate 5 to 9, third one is the step which is the difference between the numbers EXAMPLES for i in range(6): print(i) # 0, 1, 2, 3, 4, 5 for i in range(3, 7): print(i) # 3, 4, 5, 6 for i in range(0, 11, 3): print(i) # 0, 3, 6, 9
3rd May 2018, 10:03 PM
TurtleShell
TurtleShell - avatar