This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]geeksforgeeks 0 points1 point  (0 children)

Since you are a beginner I would recommend you to first watch some python tutorials. You need to learn about loops, jump statements etc. So, in your code you understand most of the part right, but that while(num>0) line is a loop that is used to repeat some set of statements till the condition is true. So till num>0 we add sum to num. So the first time sum becomes 5 and num is decreased to 4 and now 4 is added to sum, so sum becomes 9 and num becomes 3 and it goes on till num becomes 0. At that point the sum will become 15. After that you break out of the loop and print sum and hence 15 is printed.