+ 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.
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
+ 2
THANKS DEAR I GOT IT.