[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
x = "b" print x == "a" or "b"
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 13
Thread images: 3
File: 1459477554827.png (2 MB, 1200x800) Image search: [Google]
1459477554827.png
2 MB, 1200x800
x = "b"
print x == "a" or "b"

If you execute this code on python, the ouput is "b" instead of True. Can someone explain this to me?
>>
File: image.jpg (26 KB, 300x269) Image search: [Google]
image.jpg
26 KB, 300x269
>2013+3
>still using Python 2
to answer your question, the second statement is basically
print <expresssion>
, where 'expression' in this case is
x == "a" or "b"
. The value of that expression itself is the value of the first operand that is true, which is "b".
>>
order of operations.

"==" is before "or"

So the 2nd line is equal to:

print (x == "a") or "b"


First do the parens

print (false) or "b"


"or" returns the second thing if the first one is false, so

print "b"
>>
>>54961154

instead of shitting all over python 2.7.x be happy you don't have to maintain legacy code
>>
Works just like the ternary operator from other languages

0 ? 0 : "b"
>>
>>54961154
The thing is that I don't get why if

x = "a"

print x == "a" or "b"

outputs True instead of "a" which is the value of the first operand that is true. To me, it should output "a" if x = "a", "b" if x = "b" and False if x is anything else. I still don't get it?

I think im autist /g/
>>
>>54961171
I would go further to define it like so:

print(((x == "a") or "b"))


because OP is an retard.
>>
>>54961345
Look at this answer: >>54961171

Do these substitutions again.

print (x == "a") or "b"

print (True) or "b"

print True


If the first expression is not False, "or" will return that expression.
In this case,
True
>>
>>54961345
I think you want

print x == "a" or x == "b"


or something to that effect. Or if python supports something like this:

print x in ["a", "b"]
>>
File: im in.jpg (51 KB, 612x446) Image search: [Google]
im in.jpg
51 KB, 612x446
OP here. I fixed the code to work like I wanted to:

x = "b"
print x == "a" or x == "b"

Thx everyone. /thread
>>
>>54961345
>I think im autist /g/
You're not. That code actually uses a shorthand that's common in many languages, which is that non-boolean (i.e. non True/False) values are treated as boolean in certain cases. True is obviously true and False is obviously false but other values like 42, "hello", and [1,2,3] can also be treated as true or false.
The value of the first operand in
x == "a" or "b"
is not x but everything preceding 'or', because that expression is equivalent to
<expr1> or <expr2>
. In this case, <expr1> is
x == "a"
, and the value of this is clearly True. However, in the earlier example, "b" is treated as True but its *value* is actually itself.
Confusing, I know.
>>
>>54961345
It is equivalent to this
(I'm simplifying by using False instead of x == "a" because it resolves to False obviously)
print(False if False else "b")
>>
>>54961439

OP here. Hey, looks much more clear now.
Thanks.
Thread replies: 13
Thread images: 3

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.