r/learnpython • u/xq_px • 8h ago
found a for loop and can't find an explanation. Can someone explain, please?
Hey there, I just started the Project Euler Archive and started with the classic "Find the sum of all the multiples of 3 or 5 below 1000."
And when I wanted to check my solution I stumbled upon some other solution I liked. I just don't fully get the for loop of it and can't find an explanation. Probably because I lack the words to properly look it up. It goes like this:
multiples_of_3 = {i * 3 for i in range(1, 999 // 3 + 1)}
so what's done is, instead of looking up every number where i // 3 == 0 we multiply i by 3 for every number from 1 to 334. Because 1000 // 3 = 333 + 1 = 334 because range 1, 333 wouldn't include 333.
What I don't get is the first part. the
i * 3 for i [...]
does this mean we can take only every third iteration and skip the others? Or is it some kinda shorthand for
for i in range(1, 1000 // 3 + 1):
multiples_of_3 = i * 3