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?
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 :-)
+ 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.
+ 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
+ 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