Why does print(range(3,8)) doens't prints 3,4,5,6,7 and prints range(3,8) , I haven't marked any quotation then also 🙁. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does print(range(3,8)) doens't prints 3,4,5,6,7 and prints range(3,8) , I haven't marked any quotation then also 🙁.

Range -- Python

3rd Jun 2021, 3:03 PM
Aman sharma
6 Answers
+ 11
In order to output the range as a list, we need to explicitly convert it to a list, using the list() function. Aman sharma I think no, we can't :( print(list(range(3,8))) https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2434/
3rd Jun 2021, 3:12 PM
Simba
Simba - avatar
+ 4
#You can use Unpacking operator * print(*range(3,8)) #output : 3 4 5 6 7 https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-kwargs-and-args/#:~:text=In%20short%2C%20the%20unpacking%20operators,only%20be%20used%20on%20dictionaries.
3rd Jun 2021, 3:28 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
Thanks simba , but I came from this lesson only 😂 I just wanted to know what about the quotations to print strings as it printed range(3,8) as if it's a string
3rd Jun 2021, 3:30 PM
Aman sharma
+ 1
that's because since python3 range objects are generators: it consume ressources only when using it as iterating (basically it just store the arguments to defer/postpone the computation at time of use it...) so, once you have iterated once, it cannot be iterated again (it is consumed)... and you probably don't want that if you log it ^^ however, you can consume it and store the result to be iterared again: that doesn't make big time difference in your script, only require more memory (as opposite if you use it only once, memory requirement is limited to one value at a time).
3rd Jun 2021, 6:36 PM
visph
visph - avatar
0
Can't it simply just output that without conversion
3rd Jun 2021, 3:13 PM
Aman sharma
0
Thanks ratan , can you give me the link of lesson in which we can learn about these operators 😁
3rd Jun 2021, 3:32 PM
Aman sharma