pepperjack
pepperjack

Reputation: 67

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

Upvotes: 0

Views: 106

Answers (2)

user4865106
user4865106

Reputation:

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

Upvotes: 0

GMc
GMc

Reputation: 1774

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

Upvotes: 1

Related Questions