x=9 x%=2 x+=3 print(x) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

x=9 x%=2 x+=3 print(x)

I don’t get what’s going on here I tried for like 10 minutes all I know is the answer is 4 but I don’t understand the equation

11th Sep 2021, 1:47 PM
Dima Melnichuk
51 Answers
+ 18
x=x%2 x=9%2=1 x=1 x=x+3 x=1+3 x=4
11th Sep 2021, 1:53 PM
Abhay
Abhay - avatar
+ 5
Dima Melnichuk, "%" is the modulus operator which finds the remainder from dividing the two numbers u specify i.e. dividing the number on the left by the number on the right
11th Sep 2021, 2:11 PM
Tanvir Salehin
Tanvir Salehin - avatar
+ 5
Dima Melnichuk, x = 9 assigns 9 to x. So x %= 2 or x = x % 2 is actually x = 9 % 2. 9 % 2 is 1. So, x = 1 So, the value of x is 1 and then u add 3 to it so it becomes 4...
11th Sep 2021, 2:21 PM
Tanvir Salehin
Tanvir Salehin - avatar
+ 5
Here the value of x is given as 9 initially! Step 1 : This says u do modulus operation with 2, i.e x = x % 2 (modulus gives u the remainder) hence x = 1. Step 2 : the x value gets changes in Step 1 now x value is 1 In this Step add x value to 3 i.e x = x + 3, hence x value becomes 4 And finally ur printing that value.. I hope this explanation may help you ☺️
12th Sep 2021, 11:42 AM
GURURAJ KL
GURURAJ KL - avatar
+ 4
The remainder is 1. 9 % 2 ---------- The Quotient is 4 and the remainder is 1. Thats how "%" works If you did x = 9 / 2, x would be 4.5 Weird, isn't it?? ;)
11th Sep 2021, 2:30 PM
Tanvir Salehin
Tanvir Salehin - avatar
+ 3
x =9 x%=2 // x=x%2 x+=3 // x=x+3 print(x) // it will print 4
11th Sep 2021, 4:33 PM
Pramol Manik Bhosale
Pramol Manik Bhosale - avatar
+ 3
x%=2 means x=x%2. We have the value of x here as 9, so on dividing 9 with 2 we get 1. Thus the new value of x becomes 1. Now, the equation x+=3 means x=x+3. Previously we had the value of x=1. Therefore, x=1+3, i.e., 4. Hope you find this useful!
12th Sep 2021, 11:51 AM
Rishav
Rishav - avatar
+ 2
Dima Melnichuk, the x = 9 ASSIGNS 9 to x, meaning that the value of x will be 9
11th Sep 2021, 2:17 PM
Tanvir Salehin
Tanvir Salehin - avatar
+ 2
Thanks for helping me out man
11th Sep 2021, 2:34 PM
Dima Melnichuk
+ 1
Dima Melnichuk it's called module operator and used for remainder.
11th Sep 2021, 2:03 PM
Rupali Haldiya
Rupali Haldiya - avatar
+ 1
Look, x%2 returns the REMAINDER of x / 2 (x divided by 2). So that is 1 (obviously). Then, x + 3 is 1 + 3 which is 4. Note: x %= 2 means x = x % 2 And x += 3 means x = x + 3 Hope you got ur answer. Thank you. <3
11th Sep 2021, 2:09 PM
Tanvir Salehin
Tanvir Salehin - avatar
+ 1
What does the x=9 do then
11th Sep 2021, 2:15 PM
Dima Melnichuk
+ 1
Yeah i know that but it the equation of x%=2 where will the 9 go
11th Sep 2021, 2:18 PM
Dima Melnichuk
+ 1
2 times 4 is 8 and you cant do 10 because it is past the number so 9-8 is 1
11th Sep 2021, 2:34 PM
Dima Melnichuk
+ 1
Exactly
11th Sep 2021, 2:34 PM
Tanvir Salehin
Tanvir Salehin - avatar
+ 1
You are welcome <3
11th Sep 2021, 2:34 PM
Tanvir Salehin
Tanvir Salehin - avatar
+ 1
Mind talking in the dm..??
11th Sep 2021, 2:40 PM
Tanvir Salehin
Tanvir Salehin - avatar
+ 1
x=9 x=x%2 # here x=1 x=x+3 #now x=1+3=4
12th Sep 2021, 7:04 AM
Rahul Yadav
Rahul Yadav - avatar
+ 1
9%2=1 1+3=4 Print (x) Output:4
12th Sep 2021, 7:17 AM
Amirreza
Amirreza - avatar
+ 1
x=x%2 x=9%2=1 x=1 x=x+3 x=1+3 x=4
12th Sep 2021, 9:16 AM
Ahmed Abouawaja