Python REPL where you run the code, outputs the evaluated result of the code you gave it.
So:
if I give it a list comprehension like:
[i for i in range(0,3)]
even though I wont explicitly print it the python REPL will print the evaluated result as
[0,1,2]
Now you get confused because you use print(i)
function with a list comprehension.
So print(i)
prints the i
but also its return value (which is None
) is used to create the list through list comprehension:
[print(i) for i in range(0,3)]
so the output is the output of the print(i)
PLUS the REPL output of the evaluated list which is a list of None
's
0
1
2
[None, None, None]
In other words you created a list of None
's but along the way you also printed some values. if you only want to loop over some values and print then (and NOT create a list) then do:
for i in range(0, 3): print(i)