+ 1

Help me to understand this code

nums = [1,2,3,4] p = 0 for x in nums: if(x%2 == 0): continue else: p += x print(p) Here output is 4. Can anyone help me how and why output is 4.

2nd Feb 2022, 3:58 PM
IDIOT KID
IDIOT KID - avatar
2 Answers
+ 5
p is initialised to 0. for loop iterates over all elements of `nums` list. `x%2 == 0` checks whether element is even or not. when it is even `continue` is executed which causes the loop to skip rest of the statements and start next iteration. If element is odd it's added to variable p. In short, this gives you sum of all odd numbers in list
2nd Feb 2022, 4:11 PM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 2
THANKS DEAR I GOT IT.
2nd Feb 2022, 4:40 PM
IDIOT KID
IDIOT KID - avatar