How does sum function work in python with for loop
问题 I was using sum function in pyhton, and i am clear about it's general structure sum(iterable, start) , but i am unable to get the logic behind the following code test = sum(5 for i in range(5) ) print("output: ", test) output: 25 Please can anyone describe what is happening here, basically here 5 is getting multiplied with 5, and same pattern is there for every sample input. 回答1: Your code is shorthand for: test = sum((5 for i in range(5))) The removal of extra parentheses is syntactic sugar: