+ 5

Can someone explain me how this code works?

sum = 0 x = 10 while x > 0: sum += x x -= 1 print(sum)

3rd Nov 2022, 4:47 PM
Engineer X
Engineer X - avatar
23 Respuestas
+ 2
In loop “while” - x is decreasing by 1 each iteration from 10 to 1 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 When x becomes 0 - loop quits, as x > 0 is False. Each iteration sum is increasing +x. So sum = 0 +10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 55
5th Nov 2022, 1:44 PM
Stas
Stas - avatar
+ 9
Sum =0 Then while loop runs ...with X value 10 So sum =sum+X --->0+10 Then sum =10 now, After that X become 9 #decremented So 10+9=19--->sum Like this it will add ...at last you get 55...as output
3rd Nov 2022, 5:47 PM
Riya
Riya - avatar
+ 4
Sure Nesar Ahmad Yawari While loop will fail when the X value become 0 until it become 0 it will be true... Now you got it..
3rd Nov 2022, 5:05 PM
Riya
Riya - avatar
+ 3
Nesar Ahmad Yawari here, Till the while loop is true it will add the X value with sum and X value get decremented till the while loop fails and finally it prints the whole sum value...
3rd Nov 2022, 4:55 PM
Riya
Riya - avatar
+ 2
Nesar Ahmad Yawari add the X value one by one with sum you get 55 as output...
3rd Nov 2022, 5:38 PM
Riya
Riya - avatar
+ 2
Hi Riya, I just got introduced to while loop and I just know what while can do but what is the purpose of sum here? What is sum doing here? I will be glad if you can write me an answer.
3rd Nov 2022, 5:43 PM
Engineer X
Engineer X - avatar
+ 2
Hi Riya, So at first 10 minus 1 aquals to 9 because we have sum+=x and then we will minus 10 by 1 till 1 because we have while x > 0: and then we have: 10 9 8 7 6 5 4 3 2 1 And we sum them, right?
3rd Nov 2022, 8:13 PM
Engineer X
Engineer X - avatar
+ 2
Close. 10 minus 1 because x -= 1 Then 10 + 9 because sum += x Sum will be 55 when finished. 10+9+8+7...1 = 55
4th Nov 2022, 12:21 AM
PythonER
PythonER - avatar
+ 2
The code will add 10 to 1. So 10 + 9 + 8 + 7 ... + 1 sum += x 0 += 10 10 += 9 19 += 8 27 += 7 34 += 6 40 += 5 45 += 4 49 += 3 52 += 2 54 += 1 sum = 55 The sum is the initial value (which is 0). And the purpose of it is to add every x into it. And think of the *while* loop as a limit or when the *while loop* stops. In your example, x > 0, which means from the value of x until 1 (excluding 0).
4th Nov 2022, 8:40 AM
JJ Pags
JJ Pags - avatar
+ 2
Thanks MorpheusGranger
4th Nov 2022, 8:42 AM
Engineer X
Engineer X - avatar
+ 2
It takes the sum of all integers from 10 to 1 using 1 decrement in a while loop
4th Nov 2022, 1:44 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Thanks a lot saikumat chinna. Actually the Sololearn community is pretty friendly and helpful.
4th Nov 2022, 6:25 PM
Engineer X
Engineer X - avatar
+ 2
the loop is going to iterate from 10 to 1 but since the print function is outside while loop the variable 'sum' will be equal to 10+9+8+7+6+5+4+3+2+1, that means the print function will not be looped, so you will have only one output of the sum which is equal to 55
4th Nov 2022, 9:08 PM
Daudasaeed
Daudasaeed - avatar
+ 2
Thanks Daudasaeed, And good morning Tahirssk Tahirssk.
5th Nov 2022, 8:38 AM
Engineer X
Engineer X - avatar
+ 1
Hi Riya, Ok so till the while loop is True it will add 10 which is x value with 0 which is sum and 10 which is x gets decremented till the while loop fails? like how it fails? I would be glad if you explain a bit more. Thanks
3rd Nov 2022, 5:02 PM
Engineer X
Engineer X - avatar
+ 1
I see, so thats how it works. Thanks a lot Riya
3rd Nov 2022, 5:07 PM
Engineer X
Engineer X - avatar
+ 1
Sorry for bothering Riya, But the result is 55 after I hit debug, can you please explain how it equals to 55? I will appreciate an answer.
3rd Nov 2022, 5:13 PM
Engineer X
Engineer X - avatar
+ 1
Thanks PythonER.
4th Nov 2022, 4:03 AM
Engineer X
Engineer X - avatar
+ 1
Engineer X Simple explanation...Thanks
4th Nov 2022, 1:54 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Loops are used to repeatedly execute a block of program statements. The basic loop structure in Python is while loop. Here is the syntax.
4th Nov 2022, 4:36 PM
saikumar chinna