0
What’s the point of using order of precedence with bools? (True , false)
I’ve just started learning about it in my python course. I find it more useful for math based things like adding but what’s the point of it in bools?? And, or, not etc
6 Respuestas
+ 1
Its similar to maths. Maths has BODMAS and DMAS and for python its this.
+ 2
The point of it is to keep evaluations of statements consistent, just like in maths.
You know in maths that changing the order of operations can change the result you get from a calculation, so you need BODMAS to have a consistent system for calculating expressions and decide the right answer. The same happens with evaluating statements with Booleans. Consider the following:
not True and False
Not takes precedence over and, so we evaluate the not expression first and then the and:
not True and False
-> False and False
-> False
But if we swap the order so that and takes precedence over not, and evaluate and first, then we get a different answer:
not True and False
-> not False
-> True
So we need the order of operations for Booleans just like in maths to decide which is the right answer (the first).
0
and if math is not a huge part of my code?
0
Some or the other time you will use not, true just like math at that time it comes to help
0
For eg: you have not and 'and' in the same program
0
You will encounter questions like 'what will be executed first?' again and again in your programmer life - there's no getaway from that.
Don't be afraid - you will gradually get a better understanding of it, and your maths will also improve - because you WILL need it here and there if you continue programming.