Expression:
print max(3 for i in range(4))
Prints the result of the max() function applied to what is in parentheses. However, in brackets you have an expression expression that creates 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 array 3 and destroy it after it is no longer needed.
Basically, since you create only equal values ββin brackets, and the max() function returns the largest, you do not need to create multiple elements. Since the number of elements is always equal to one, the max() function becomes unnecessary, and your code can be effectively replaced (at least if you gave) with the following code:
print 3
That's just it;)
To learn more about the differences between an understanding expression and a generator expression, visit this page.
source share