I found this simple code snippet from somewhere, but I don't understand how come this sum() syntax works.
total = sum(1 for _ in [1,2,3,4,5])
print(total) # 5
for _ in [1,2,3,4,5]
is nothing but the looping five times.
So the code snippet loops five times and add 1 for each loop so becomes 5 I guess.
I'm not sure about while looping five times in for _ in [1,2,3,4,5]
what's happening then with 1?
According to the syntax of sum(iterable, start), the first argument should be iterable, but 1 is int. How come this works based on the sum syntax. How this code internally works? I'm confused.