+ 1
Booleans
x = True y = False z = False x or y and z == True. Why is 'and' treated before 'or' ? Is this a python thing? Help me understand.
5 Respostas
+ 3
'and' is before 'or' in every programming language. There is an order of operations just like in math.
https://en.m.wikipedia.org/wiki/Order_of_operations#Programming_languages
+ 2
arr = [1, True, 'a', 2]
print('a' in arr in arr)
Output is false. While:
print(('a': in arr) in arr)
Output is true
Schindlabua Henri Evjen Is python right to left without brackets for 'in' operations?
+ 1
Even cooler: `and` is like multiplication and `or` is like addition in a pretty strict mathematical sense.
true behaves a bit like 1, false like 0:
true and true == true
1 * 1 == 1
false and true == false
0 * 1 == 0
false or false == false
0 + 0 == 0
false or true == true
0 + 1 == 1
(with 1+1 we have to squint a little to see it.)
And of course multiplication goes before addition :)
+ 1
I don't write much python, but looks like it. Right-associativity is not terribly uncommon though. Think of `a ** b ** c`, or in other languages, `a ? b : c ? d : e` or `a = b = c`.
0
Thanks. Cool eye opener. *Not every programming language though. There are exceptions.