0

Python challenge question x,y%3,x//2

x= 25 y= 10 x,y=y%3,x//2 print (y-x) THIS GIVES AN OUTPUT =11 CAN YOU PLS EXPLAIN WHAT FORMAT IS BEING USED IN LINE 3. SOMetHing FEELS OFF HERE. HOW IS THIS A CORRECT SYNTAX?

8th Dec 2020, 7:58 AM
Sandhya Raghuraman
4 Réponses
+ 4
Line 3 means, x=y%3 and y=x//2 performed at the same time. If we write y=x//2 after x=y%3 in a separate line , new "x" will be =10%3 which is 1. So y will be =1//2 which is 0. If we write x,y=y%3,x//2 in a single line as above, new "x" will be =10%3 which is 1 and new "y" will be =25//2=12 as both of these are performed at the same time so "x" here will still be 25. That's why the output produces 11. Hope you understand :-)
8th Dec 2020, 9:27 AM
R_A
+ 2
In line 3, x is assigned with y%3, which is 1, and y is assigned x//2, which is 12. Thus, y - x is 11.
8th Dec 2020, 8:11 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
Lets go line by line x,y = y%3,x//2 1)y%3 = 10%3 = 1 (remainder 1) 2)x//2 = 25//2 = 12 (point not calculate due to floor division) Now assume x,y = 1,12 print(y-x) (means(12-1)) =11 is the answer
9th Dec 2020, 5:03 AM
Sayyam Jain
Sayyam Jain - avatar
+ 1
I think this is a special format in python. here x,y=y%3,x//2 is equivalent to: z=x p=y x=p%3 y=z//2 You can only do this with 2 variable with this syntax x,y=y%3,x//2.This is a special format for doing both operation together with memorizing the previous value of x and y.Thus x becomes 1 and y becomes 12 so printing(y-x)=11
8th Dec 2020, 8:17 AM
The future is now thanks to science
The future is now thanks to science - avatar