sds2019
sds2019 copied to clipboard
add list() to Box 6 under section 0.3?
Hi
To illustrate the example with ranges, did you intend to write the following?
print("Range from 0 to 100, step=1:", list(range(100))) print("Range from 0 to 100, step=2", list(range(0, 100, 2))) print("Range from 10 to 65, step=3", list(range(10, 65, 3)))
No.
While list(range(100))
is superficially similar to range(100)
the two are not the same. When you call range(100)
you get a generator back. Generators are lazily evaluated so only one value of the sequence is stored in memory at any given time. Calling list(range(100))
sticks every element of the sequence into a list which is kept in memory. This means you can look at the entire sequence at once, but also means you're storing 100 values in memory.
That being said generators and lists are practically interchangeable in situations where code performance and/or memory constraints are irrelevant.
Yes, I agree with you. Thank you for the clarification. I then misunderstood what was intended to be shown. I thought you meant to show the final output (i.e the range of numbers generated), rather than what to type to generate the range.