[Solved] x = y = z = 0 | Sololearn: Learn to code for FREE!

+2

[Solved] x = y = z = 0

I ran across a practice test question that displayed this code: x, y, z = 5, 42, 17 x = y = z = 0 Then asked what the result was. True False 0 64 (I can't remember if the = signs were assignment or comparison [= or ==] and I know that makes a difference, but I was hoping it is obvious to someone.). I guessed at False as I have never seen a line with multiple = signs like that. What is that line doing? Can python assign multiple variables like that or do a comparison? I am almost positive 64 is not possible, but I am a little taken back that I can't find info anywhere. I am still searching, but wanted to ask here before I find wrong info somewhere. Please only answer if you know. Thanks.

6/13/2020 3:04:51 PM

Kode Krasher

4 Answers

New Answer

+8

"=" is an assignment operator. Here first "z" is assigned to 0 then "y" is assigned the value of z which is 0 and so on thus at the end all the variables (i.e. x,y,z ) are assigned to 0.

+5

It is a pretty common practice in almost every programming language where you have to assign a single value to multiple variables. Arsenic has explained it well. Let us know if you need any other details regarding the same.

+2

#test x, y, z = 5, 42, 17 print(x, y, z) print(x == y == z == 0) x = y = z = 0 print(x, y, z) # output # 5 42 17 # False # 0 0 0 Ok. got it. just have to pay attention. very interesting?! Arsenic Avinesh Thanks for answering. Feel kind of stupid now, but at least I won't forget it?! I guess I just needed to give myself a few minutes to let my brain start working again. cheers! 🍻

+1

Arsenic Avinesh ok. I suspected that was the case for assignment, but I never saw it written like that. unfortunately, I am not positive if I recall correctly. Is there a case for comparison? x == y == z == 0 is that valid as well? and given the scenario above, that would equal False? or is the comparison syntax just not possible. ...I guess I should try it in code playground? huh? 🤦🏻‍♂️ I am a little overwhelmed at the moment. lol. Practice test was too fast with less than a minute per question, so my brain is worked. lol