Python Forum
These boolean statements don't make sense?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
These boolean statements don't make sense?
#1
In trying to diagnose a problem with my code, I have traced the problem to the following result, which makes no sense to me. I was wondering if somebody could make sense of the problem:


'2'.isupper() == False & '2'.islower() == False & '2'.isdigit() == False:
Output:
True
Shouldn't this evaluate to False, since the third and final condition ('2'.isdigit() == False) is False?
When I evaluate the third and final condition by itself without the first two, it evaluates to False:

'2'.isdigit() == False
Output:
False
Reply
#2
The important word in the docs:
Quote:Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise.
is 'cased character' a digit is allowed in a string, but is not a 'cased character'
But see what you're talking about
it is True that
'2'.isupper()= False is True
and it is true that
'2'.islower()= False is True
and it is True that 
'2'.isdigit() = True is True

Therefore, the statement is True & True & True  is  True
Reply
#3
I don't think that operator does what you think it does: https://en.wikipedia.org/wiki/Bitwise_operation#AND

>>> 1 & 2
0
In python, it's "better" to just use the keyword and when you logically are checking if multiple things are true.
Reply
#4
(Oct-02-2017, 09:44 PM)Larz60+ Wrote: The important word in the docs:
Quote:Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise.
is 'cased character' a digit is allowed in a string, but is not a 'cased character'
But see what you're talking about
it is True that
'2'.isupper()= False is True
and it is true that
'2'.islower()= False is True
and it is True that 
'2'.isdigit() = True is True

Therefore, the statement is True & True & True  is  True 

The last statement is '2'.isdigit() == False which evaluates to False. The whole statement is True & True & False. Really doesn't make sense to get True as a final result.
Anyway, he should use 'and' as @nilamo have mentioned.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
You're right! Didn't catch that, looks like a bug
and is better, but still doesn't explain why logic AND doesn't work
Reply
#6
It's an order of operations issue. & is binding before ==, not after it like 'and' would. So it's equivalent to:

('2'.isupper()) == (False & '2'.islower()) == (False & '2'.isdigit()) == (False)
# which resolves to
False == False == False == False
# which is True
Which is why you use 'and' for logic and & for bits.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Makes sense, Got it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can anyone make sense of this? steve_shambles 7 3,166 Apr-19-2020, 11:22 AM
Last Post: steve_shambles
  Re writing poste to make sense please help me Nearrivers 10 5,589 Apr-02-2018, 11:52 PM
Last Post: Nearrivers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020