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
Related
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
The following is a toy example in Python:
a = 2
b= 10
result = a<b and print("Hello")
print(bool(result))
The output is:
Hello
False
Why is the output False instead of True? Since result evaluates to a<b= 2<10 = True then, we have result = True and print() = True and True = True. Can somebody please explain the reason for this answer?
print returns None:
>>> print("Hello") is None
True
and None is a Falsey value:
>>> bool(None)
False
so you have True and False, which evaluates to False.
When I'm trying to check whether a list is empty or not using python's not operator it is behaving in a weird manner.
I tried using the not operator with a list to check whether it is empty or not.
>>> a = []
>>> not (a)
True
>>> not (a) == True
True
>>> not (a) == False
True
>>> True == False
False
The expected output for not (a) == False should be False.
== has higher precedence than not. not (a) == False is parsed as not (a == False).
This is working as expected. Parenthesis added below to clarify how this is being executed:
not (a == True)
# True
not (a == False)
# True
An empty list, a = [], evaluates to False in boolean expressions, but it does not equal False or True. Your expressions in the middle are testing whether it is equal to False or True, so they both evaluate to False, and not False is True.
You can add parenthesis like below to get what you expect:
(not a) == True
# True
(not a) == False
# False
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
I can't figure out why:
f = lambda x: x
In [8]: f is True
Out[8]: False
In [9]: not f is True
Out[9]: True
In [10]: f is False
Out[10]: False
In [11]: f is True
Out[11]: False
In [12]: not f
Out[12]: False
In [13]: not f is True
Out[13]: True
In [14]: not f is False
Out[14]: True
ok. So until now we can imagine that is due to the use of "is" instead of "==". As shown here:
In [15]: 0.00000 is 0
Out[15]: False
In [16]: 0.00000 == 0
Out[16]: True
Ok. But why then if i do it on the function:
In [17]: not f == False
Out[17]: True
In [18]: not f == True
Out[18]: True
In [19]: f ==True
Out[19]: False
In [20]: f ==False
Out[20]: False
In [21]: f
Out[21]: <function __main__.<lambda>>
I was trying to explain it as due to "is" instead of "==" but examples 19 and 20 crushed my logic. Can someone explain?
is tests for object identity. Comparing anything other than True with is True is always going to be false.
Your next set of tests test if not (f == False) or not (f == True); again, boolean objects only test equal against themselves, so anything other than False will test as false when comparing with == False. not False then is true.
You want to use bool() instead to test if something is true or false:
>>> bool(f)
True
>>> bool(0)
False
Don't use equality testing to see if something is true or false.
Note that only numeric 0, empty containers and strings, and False is considered false in Python. Everything else, by default, is considered true in a boolean context. Custom types can implement either the __nonzero__ method (when numeric) or the __len__ method (to implement a container) to alter that behaviour. Python 3 replaced __nonzero__ with the __bool__ method.
Functions do not have a __nonzero__ or __len__ method, so they are always considered true.
If you check the "truthyness" of a function, you will see it is True.
>>> f = lambda x: x
>>> bool(f)
True
You were simply comparing the function itself to True or False Which it would never be, since it is a function.
== checks for equivelency ... is checks identity ...
a function is a non-falsey value however it is not equivelent to True
def xyz():
pass
if xyz:
#this will trigger since a method is not a falsey value
xyz == True #No it is not equal to true
xyz == False #no it is not equal to false
xyz is True #no it certainly is not the same memory location as true
xyz is False #no it is also not the same memory location as false
Your own example shows that f is False is false so I'm confused by your title.
Why would you expect a function to evaluate as equal to either Boolean value? Wouldn't that be kind of weird behaviour?