Why it is False


#1

Hi people,
why this print out False?

x = 6
print x == (5 or 6)

From my understanding this sentence means in plain English “whether x is 5 or 6”.
Then clearly when x is 6 then this sentence is true.

How python works with the parenthesis or True/False sentence?

THanks


#2
a = 2 or 3  # what value should a get assigned to? both, somehow?

What’s going to happen there is that the expression there (2 or 3) will get evaluated, and then the variable gets assigned to the result

I suppose you could create some data type that is undecided on which value it is, but the or-operator does not create such a value, it already has a different behaviour… A simpler approach is to use a list containing all the candidates, but again, (2 or 3) isn’t the syntax for a list, and == doesn’t compare a single value to multiple values in a list

What parentheses do in expressions is to determine the order that expressions are evaluated, so in your case (5 or 6) is evaluated before ==


#3

Thank you.
It is still kind of confusing. But seems like clearer a bit.
In that case my question diverge to:
Why python can’t make their language similar to most intuitive way of expression?
Is it technically possible? Or just the way it is as first group of developer’s convention?

Thanks alot :slight_smile:


#4

What’s that? English? You’re not writing instructions to a human, and even if you were, you wouldn’t want that human to guess what you mean, you’d want to write something that has one single exact meaning.

Writing something and expecting it to do what you want is a backwards way to go about it here, instead you need to use what has already been agreed upon, that is what a language is.


#5

The question is really if you yourself can describe that behaviour to sufficient detail that it is executable. That is how things generally go with programming. There’s no magic, you simply have to think things through to sufficient detail and then write those details down in a format that can be executed (code)

If you’re using words like “intuition” then you can probably forget about it right there, because that’s not something that can be executed (you can’t describe it as a series of steps to carry out)
(It suggests general intelligence, and that might defeat most needs of programming languages, you’d just say what you want: Siri. Make facebook.)

If it doesn’t involve intelligence, then it may be something that could be used in a new language (again, you’d have to describe it, whatever it is) or to be used in already existing languages (by creating objects and functions with the behaviour you want, and then using that behaviour)

…In the meantime, Python already expresses a whole lot of things well enough:

print 5 in [5, 6]

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.