+ 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.

25th Apr 2020, 12:51 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
5 odpowiedzi
+ 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
25th Apr 2020, 1:01 AM
Henri Evjen
Henri Evjen - avatar
+ 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?
25th Apr 2020, 7:56 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 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 :)
25th Apr 2020, 3:00 AM
Schindlabua
Schindlabua - avatar
+ 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`.
25th Apr 2020, 10:39 AM
Schindlabua
Schindlabua - avatar
0
Thanks. Cool eye opener. *Not every programming language though. There are exceptions.
25th Apr 2020, 1:12 AM
Tomiwa Joseph
Tomiwa Joseph - avatar