Why this code "False==False or True" is true but "False==(True or False) is not true? | SoloLearn: Learn to code for FREE!

+13

Why this code "False==False or True" is true but "False==(True or False) is not true?

4/13/2016 9:41:00 PM

Real Coder

12 Answers

New Answer

+14

The "or" operator means if one of the sides is True then the whole expression is True. Your first expression "False == False or True" is equivilent to "(False == False) or True" as python braeks the calculation into steps by location as so: 1. (False == False) or True 2. True or True 3. True When you add the precedense by using parentheses like in the expression "False == (True or False)" then you hint python the order of calculating the expressions as so: 1. False == (True or False) 2. False == True 3. False hope it helps ;)

+17

Python solves parenthesis first then == then or. So, in the first example false==false is true so true or true is true. Now, in the second example the parenthesis is solved first so: true or false is true but, false == true is false hence false.

+8

good question😁

+6

(True or false) by some reason evaluates to True. And then, False == True will obviously return false.

+3

Simple. Mathematical Logical. For the expression will be correct, it must be correct in all cases. Possibilities: False == (False or True) False == False AND False == True False == True is FALSE so the result is FALSE.

+3

Operator precedence in python: 1. () -parantheses 2. +a, -a, ~x -unary plus, unary minus, bitwise NOT (a is a variable. ) 3. *, /, // , % -multiplication, division, floor division, modulus 4. +, - -mathematical addition and subtraction 5. << , >> -bitwise shift operators 5. & -bitwise AND 6. ^ -bitwise XOR 7. | -bitwise OR 8. ==,!=,>,>=,<,<=,is,is not,in,not in - equals to, not equals to, greater than, greater than or equal to, lesser than, lesser than or equal to, membership operators(is, is not, in , not in) 9. not -logical NOT 10. and -logical AND 11. or -logical OR NOTE : List is in descending order. No. 1 has the highest precedence and No 11 has the lowest precedence.

+2

what is the meaning of (True or False) ? why it's result is True because operator .or. says either answer be True or answer is False anything.

+2

Similar to math, it is helpful to simplify the statement in steps, following order of operations/operator precedence False == False or True ..... False == False, evaluates to True True or True ...... either value is True, evaluates to True False == (True or False) ..... evaluate parenthesis first. either value is True, evaluates to True False == True ...... both value are not equal, evaluate to False Note: Many other languages consider Boolean operators to be of the same precedence and evaluate left to righ, but Python considers "==" to be of higher precedence than "or". Example: True or False == False evaluates to True in Python, but False in other languages

+1

so python, when given an opening defaults to true? (True or False)== True

+1

so you can say after solving the paranthesis it doesn't "exist" anymore? still a bit confused, sorry

+1

Alexander: I don't think it defaults to anything, it's like a light switch, either in the on or off position. it evaluates based on the order of operations only, solving each piece as a separate equation. In your example: (True or False)==True 1. (True or False) returns True, as this is a logically true statement (any argument will be either True or False) 2. now we have True==True, which returns True, as it is logically true that True and True are equivalent. just remember order of operations and operator precedence and your evaluations will be correct. Hope this helps. :)

+1

its all because of operator precedence in d first line of code the absence of the parentheses, which helps to prioritize operations, allows False==False or True to be true, because"==" precedes "or" operator, so that False == False runs first which evaluates to True , then we are left with "True or True" which Of course is "True", The second code evaluates to false due to d presence of the parentheses which prioritized the or operator so it runs before the == operator, so that False == (False or True) False == ( True) which is false.. The or operator runs first because it is enclosed within the parentheses, and evaluates to true as above. False is on the other hand not equal to true so d ans becomes false......