Python bool + bool returns value 2 [duplicate] - python

This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 1 year ago.
When I execute this piece of code in python3
x= bool(input())
y= bool(input())
print(x+y)
and when I give input as True True or False False or True False,
I am getting output as 2.
Why is it so? I expected 1 and 0 as the output.

input() in Python 3 returns a string. No data conversions are performed. So the inputs you get are, literally, the strings "True" and/or "False". Then bool() applied to a non-empty string returns True.
>>> bool("True")
True
>>> bool("False")
True
>>> bool("just about anything")
True
>>> bool("") # except an empty string
False
That's why you always get 2 no matter what you enter. Both inputs become True, which equals 1 in a numeric context.
>>> True == 1
True
While people will yell at you if you do this, eval() is the most convenient (although dangerous!) way to evaluate a string as if it were Python code:
>>> eval("True")
True
>>> eval("False")
False
It's dangerous because eval() will evaluate any Python expression, so if you don't trust your input it can trick your program into doing just about anything.

Input converts your input to string format!
bool(str) gives you True state! And then you're trying to do True + True so you get 2!
x = bool(input()) #--> x= True
y = bool(input()) #--> y= True
print(x+y) #--> True + True = 2

Related

Python type conversion int <=> bool [duplicate]

This question already has answers here:
Why does 1 == True but 2 != True in Python? [duplicate]
(8 answers)
Closed 1 year ago.
It is in the REPL
>>> 1 == True
True
>>> 2 == True
False
>>> 3 == True
False
>>> -1 == False
False
but
if 3:
print('yes') #prints yes
if not -1:
print('yes') #prints nothing
Why are positive integers not evaluated as True when using ==, but they are evaluated as True in if-statements? 2) Why are negative integers not evaluated as False when using ==, but they are also not evaluated as False in if-statements?
What is the underlying rule, where is it written down?
Every integer except 0 evaluates to True. The reason why your REPL was giving you different results than your if statements is because you aren't evaluating the same expression.
In your REPL you are asking is A equal to B. It's only true for False == 0 or True == 1. In your code you aren't doing a comparison. You are just checking that the variable is not something that could be considered "empty/nonexistent" (so to speak).
in short:
if a == True: and if a: are not the same thing.
If it helps, you can look at it like this:
if changeForADollar == myChange:
vs
if anyChangeAtAll:

Understanding precedence of `not` operator [duplicate]

This question already has answers here:
Priority of the logical operators (order of operations) for NOT, AND, OR in Python
(8 answers)
Closed 2 years ago.
I tried to test empty string in Python (empty string should be "Falsy" as they write in: How to check if the string is empty?). However I used a little bit different syntax and get weird result in the following comparison:
not(''); # result: True
not('hello'); # result: False
not('hello') == False; # result: True
not('hello') == True; # result: True - how is this result possible? (False == True: result must be False)
Thank you for the answer!
The precedence here is not ('hello' == False). 'hello' equals neither True nor False, so both 'hello' == True and 'hello' == False are False, which is then negated by not.
>>> 'hello' == False
False
>>> 'hello' == True
False
>>> not ('hello' == True)
True
Truthiness isn't the same as being equal to True. A string can be truthy (i.e. you can decide whether it's more of a "yes" or a "no"), but at the same time not be equal to a boolean value (because a string is a string and a boolean is a boolean).
It's important to understand that not is an operator, not a function. The parenthesis do nothing for the expression, here's how it is read:
not('hello') == True
# is the same as
not 'hello' == True
# which is the same as
not ('hello' == True)
# which is equivalent to
not False
# which is
True
Which happens to evaluate to the same as the expression above (for the same reason that 'hello' == False is False.
The correct way of enforcing precedence with not would be to do
(not something) == True

not understanding boolean output when comparing true and false statements

for the below code, I am not understanding how this is working. I am trying to learn the basics online and no matter what I cannot break the below. but if the flag value is originally false, then essentially line four is saying false = false or false....which is TRUE
def any_lowercase4(s):
flag = False
for c in s:
flag = flag or c.islower()
return flag
print(any_lowercase4('TT'))
It will then print False
Actually False or False is False (not True as you propose)
You can see this with this simple example:
>>> x = False
>>> y = False
>>> print (x or y)
False
>>> z = True
>>> print (x or z)
True
>>>
The complete truth table for or is:
F or F = F
T or F = T
F or T = T
T or T = T
where T = True and F = False
print(any_lowercase4('TT'))
essentially says please check if any character is lower,
which is not.
So, either check for Tt, which outputs True.
In Python, islower() is a built-in method used for string handling.
The islower() methods returns “True” if all characters in the string are lowercase, Otherwise, It returns “False”.
b='Tt'
c='tt'
print (b. islower())
print (c. islower())
for i in b:
print (b. islower())
Outputs
False
True
False
False

Why does [] == False evaluate to False but 0 == False to True in python? [duplicate]

This question already has answers here:
Empty list boolean value
(3 answers)
Closed 4 years ago.
I am relatively new to Python and don't understand the following behavior:
Why does the statement
[] == False
evaluate to false, eventhough an empty list is falsy?
Here are more examples - in many other cases it seems that the empty list does behave in a falsy way, just not in [] == False...
>>> 0 == False # what I'd expect
True
>>> not [] # what I'd expect
True
>>> bool([]) # what I'd expect
False
>>> [] == True # what I'd expect
False
>>> [] == False # unexpected
False
>>> bool([]) == False # why does it evaluate to True again now?
True
>>> ([]) == (bool([])) # unexpected
False
>>> (not []) == (not bool([]) # apparently adding 'not' makes it behave as expected again - why?
True
Can somebody please explain this to me? How are these statements internally evaluted? I have a feeling it might be related to chaining comparisons (see e.g. here), but cannot really understand if that's correct and why.
Because falsy isn't False. Falsy just means
bool(some_object) is False
So,
>>> bool([]) is False
True
>>> bool({}) is False
True
>>> bool(0) is False
True

Empty String Boolean Logic

I just stumbled across this and I couldn't find a sufficient answer:
x = ""
Why then is:
x == True
False
x == False
False
x != True
True
x != False
True
Am I supposed to conclude that x is neither True nor False?
to check if x is True of False:
bool("")
> False
bool("x")
> True
for details on the semantics of is and == see this question
Am I supposed to conclude that x is neither True nor False?
That's right. x is neither True nor False, it is "". The differences start with the type:
>>> print(type(""), type("x"), type(True), type(False))
builtins.str, builtins.str, builtins.bool, builtins.bool
Python is a highly object oriented language. Hence, strings are objects. The nice thing with python is that they can have a boolean representation for if x: print("yes"), e. g.. For strings this representation is len(x)!=0.
In a Boolean context, null / empty strings are false (Falsy). If you use
testString = ""
if not testString:
print("NULL String")
else:
print(testString)
As snakecharmerb said, if you pass the string to the bool() function it will return True or False based
>>> testString = ""
>>> bool(testString)
False
>>> testString = "Not an empty string"
>>> bool(testString)
True
See this doc on Truth Value Testing to learn more about this:
Python 2:
https://docs.python.org/2/library/stdtypes.html#truth-value-testing
Python 3:
https://docs.python.org/3/library/stdtypes.html#truth-value-testing
In python '==' tests for equality. The empty string is not equal to True, so the result of your comparison is False.
You can determine the 'truthiness' of the empty string by passing it to the bool function:
>>> x = ''
>>> bool(x)
False

Categories

Resources