0

why is the answer true ?

Hello :) i have been playing around with this question: if (1==1) and (2+2>3): print("true") else: print("false) i altered the Second Expression to (1+1>3) , and the it returns 'false' , so far so good. However once i use a parenthesis like this: (1+(1>3)) it returns 'true'. i really can't figure out How this Works. any help is much appreciated, thx

20th Oct 2016, 12:20 PM
tobi
5 Respostas
+ 7
I guess you can understand what happened at the first 2 cases so I'm not going to explain them in python boolean values as integers-> True is equal to 1 False is equal to 0 integers as booleans-> 0 is equal to False any other value is equal to True so... bool(2)=True int(True)=1 (1+(1>3)) -------> A 1>3 = False so A is equal to (1 + False) python converts False to integer to add the values so A = (1+0) ;because False=0 A=1 you have used this in a place where you have to put a condition. Because 1 is True as a boolean value, the second part is also True True and True = True
20th Oct 2016, 12:39 PM
Sunera
Sunera - avatar
+ 1
in any kind of variable or value if it is empty it is False as a boolean if it is not empty then it is True eg: "hello"=True ""=False [ ]= False ["d",5]=True 5=True 0=False here = means the value as a boolean ,not that "hello" and True are the same To get the boolean value of anything use bool(value) how ever this is not the same in the other way eg:list(False) is not [ ] , it is [False] booleans are specially represented as numbers using 0,1 so as you asked float(True)=1.0 float(False)=0.0 bool(0.0)=False bool(1.0)=True bool(6437.6789)=True. any non zero value in float is also True sorry my English is not good
20th Oct 2016, 1:12 PM
Sunera
Sunera - avatar
0
thank you very much Sunera! sorry to Keep asking but dies this also apply to floats ?
20th Oct 2016, 12:57 PM
tobi
0
Let's break this down: (1+(1>3)) (1+False) (1+0) With the parentheses as you arranged them, 1>3 is a comparison, not an arithmetic operation, so the result is the boolean False. Now the term for the AND logic is no longer a condition, just an integer, which is why the result is True. P.S. Don't forget your second " in line 4! ;-)
20th Oct 2016, 2:17 PM
Scivias
Scivias - avatar
0
Try it with floats ;-) Lets shorten this: yes, it works
16th Nov 2016, 3:43 PM
Sebastian Rösner
Sebastian Rösner - avatar