The expression:
print max(3 for i in range(4))
is printing the result of the max()
function, applied to what is inside the parentheses. Inside the parentheses however, you have a generator expression creating something similar to an array, with all elements equal to 3
, but in a more efficient way than the expression:
print max([3 for i in range(4)])
which will create an array of 3
s and destroy it after it is no longer needed.
Basically: because inside the parentheses you will create only values that are equal, and the max()
function returns the biggest one, you do not need to create more than one element. Because with the number of elements always equal to one, the max()
function becomes not needed and your code can be effectively replaced (at least in the case you have given) by the following code:
print 3
That is simply all ;)
To read more about differences between comprehension and generator expression, you can visit this documentation page.