In python, in what order would x = y == "true" evaluate

Question:

I’ve come across instances where someone is assigning some variable x to another variable y, followed by == "true".

Do I set x to equal y if y equals "true"?

If it was just x = y = z, I would assume that both x and y are being set to the value of z. but a == outside of a conditional is throwing me

Asked By: Derek1st

||

Answers:

It is more obvious if you add parentheses according to operator precedence:

x = (y == "true")

y == "true" is an expression that evalutates to a bool, so it will be True or False. That value is then assigned to x.

Or in more words:

if y == "true":
    x = True
else:
    x = False
Answered By: user2390182

x = something assigns the value "something" to x. y == something evaluates to True if the value of y is equal to "something", and evaluates to False if y is not equal to "something".

So, x = y == "true" means "set x to True if y is equal to the string "true". Otherwise set x to False".

Answered By: Random Davis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.