Why this code "False==False or True" is true but "False==(True or False) is not true? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 12

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

13th Apr 2016, 9:41 PM
Real Coder
Real Coder - avatar
13 Réponses
+ 25
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 ;)
1st Jul 2016, 2:48 PM
Matan Zohar Waksman
Matan Zohar Waksman - avatar
+ 19
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.
23rd Jun 2016, 7:49 PM
Tiago Reiser
Tiago Reiser - avatar
+ 8
good questionūüėĀ
18th Jun 2019, 12:40 PM
Ali Badri
Ali Badri - avatar
+ 5
(True or false) by some reason evaluates to True. And then, False == True will obviously return false.
24th Mar 2016, 5:20 AM
Aryan - avatar
+ 4
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.
29th Aug 2020, 11:09 AM
Vidhi Mishra
Vidhi Mishra - avatar
+ 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.
27th Jul 2016, 10:17 PM
José Antonio Rodríguez Gómez
José Antonio Rodríguez Gómez - avatar
+ 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.
7th Jul 2016, 6:27 PM
Rajat Dhingra
Rajat Dhingra - avatar
+ 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
21st Jul 2016, 9:27 PM
+ 1
so python, when given an opening defaults to true? (True or False)== True
24th Jun 2016, 4:59 PM
Alexander Sousa
Alexander Sousa - avatar
+ 1
so you can say after solving the paranthesis it doesn't "exist" anymore? still a bit confused, sorry
30th Jun 2016, 4:24 PM
+ 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. :)
6th Jul 2016, 12:40 PM
Dustin Jones
Dustin Jones - avatar
+ 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......
11th Jul 2016, 11:22 PM
Minutes Play
Minutes Play - avatar
+ 1
look it is simple==> false==false >>> is true >>>true or true >>>is true because for OR one of them be true is enough but true or false >>> is true >>>false == true >>> obviously true is not equal to false
26th Sep 2020, 7:37 PM