• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please explain the line from this little program

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in this line



is list a keyword? i dont really understand this lines function

thanks
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't Java. What language is it? (I guessed Python and moved it to the Python forum)
 
Jeanne Boyarsky
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
range() is Python. But you can loop through the result directly so no reason to cast it to list().
 
Greenhorn
Posts: 2
C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A list is basically a data structure in python. When you use range(1,11) it returns a list of numbers from 1 to 10 however, in order to store them as a list, list() is used. Think of it as a casting method.
Just like you cast datatype from one to other(Say int to float).
>>> listRange = range(1,11)  #Without using list()
>>> print(listRange)
       range(1, 11)
>>> listRange = list(range(1,11))  #Converting to list using list()
>>> print(listRange)
       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Hope it helps. Let me know if you have any questions.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch and thank you for answering that question.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Python 3, range returns a range literal. That probably doesn't say a lot, but you can consider it as an iterator or stream. It doesn't store the entire sequence of numbers in the range. (In Python 2, xrange is the Python 3 range equivalent; Python 2's range does store the numbers.) When putting this in a list it will create a list with the same elements.

If you only want to iterate over the range, you shouldn't use list because that only requires more memory. Only turn it into a list if you actually need it to be a list.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic