+ 3

What does this code actually do? a=[2*i for i in range(10**100)]

I understand it doesnt work with list comprehension due to MemoryError and it works with some concept called generators, which is in the next module, as per the note given on this slide. But what exactly does this code mean?

3rd Sep 2016, 9:59 AM
Eashwar Balaji
Eashwar Balaji - avatar
8 odpowiedzi
+ 2
Ohh cool
3rd Sep 2016, 11:01 AM
Eashwar Balaji
Eashwar Balaji - avatar
+ 1
The list starts with 0, 2, 4, 6, 8, 10,... You can try it yourself lowering the upper bound of range, like range(10)
3rd Sep 2016, 10:59 AM
trueneu
trueneu - avatar
+ 1
and 10**100 is 10 raised to the power 100?
3rd Sep 2016, 11:02 AM
Eashwar Balaji
Eashwar Balaji - avatar
+ 1
Yes, 10**100 is 10 raised to the power of 100. As you can imagine, it's a pretty big number.
3rd Sep 2016, 11:23 AM
trueneu
trueneu - avatar
+ 1
yeah
3rd Sep 2016, 11:30 AM
Eashwar Balaji
Eashwar Balaji - avatar
0
It creates a list consisting of doubled (2*i) for each i from 0 to 10**100 - 1. The list just happens to be too long to fit in memory. Generator won't return whole list at once, but rather 1 value at a time when asked, thus it's a viable workaround.
3rd Sep 2016, 10:50 AM
trueneu
trueneu - avatar
0
Okay so can you tell like few numbers of this particular list like four or five numbers
3rd Sep 2016, 10:52 AM
Eashwar Balaji
Eashwar Balaji - avatar
0
nice
12th May 2017, 5:23 PM
Saradha
Saradha - avatar