Python Forum
Mixing Boolean and comparison operators
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mixing Boolean and comparison operators
#1
I'm reading an intro to Python book and it gives these examples:

>>> (4 < 5) and (5 < 6)
True
>>> (4 < 5) and (9 < 6)
False
>>> (1 == 2) or (2 == 2)
True
Then it says:

> The computer will evaluate the left expression first, and then it will evaluate the
> right expression. When it knows the Boolean value for each, it will then evaluate
> the whole expression down to one Boolean value.

This feels intuitively right to me. However, I thought a while back I asked about some weird results I was getting with this sort of thing involving and/or and the reason it didn't turn out as I expected was because once one part was deemed to be False, it did not process the remainder of the expression. I can't remember if that was dealing with and or or.

Does anyone know what I might be misremembering or confused about here? Thanks!
Reply
#2
Both and and or will short-circuit - that is, they may not evaluate their second expression.

In the case of and if the first expression evaluates to False, there's no need to evaluate the second, because the whole expression will evaluate to False either way.

In the case of or, if the first expression evaluates to True, there's no need to evaluate the second.
Mark17 likes this post
Reply
#3
a or b evaluates to a if a is true-ey, else it evaluates to b. The true-y/false-ey ness of b is not considered, and b is only evaluated if b is returned.

a and b evaluates to a if a is false-ey, else it evaluates to b. b is only evaluated if a is true-ey.

I say true-ey and false-ey because a does not have to be True or False, and the result of "or" and "and" statements might not be True or False. For example, "5 or 3" evaluates to 5, because int 5 is considered true-ey. If you evaluate bool(5) it returns True. Most objects are true-ey. Python objects that are false-ey are; False, None, 0, blank strings and empty container objects (lists, dictionaries, sets).
print("or\n--\n0 or None =", 0 or None)
print("1 or None =", 1 or None)
print("0 or 'Hi!' =", 0 or 'Hi')
print("1 or 'Hi!' =", 1 or 'Hi')
print("[] or [] =", [] or [])
print("[1, 2] or [] =", [1, 2] or [])
print("[] or [3, 4] =", [] or [3, 4])
print("[1, 2] or [3, 4] =", [1, 2] or [3, 4])

print("\nand\n---\n0 and None =", 0 and None)
print("1 and None =", 1 and None)
print("0 and 'Hi!' =", 0 and 'Hi')
print("1 and 'Hi!' =", 1 and 'Hi')
print("[] and [] =", [] and [])
print("[1, 2] and [] =", [1, 2] and [])
print("[] and [3, 4] =", [] and [3, 4])
print("[1, 2] and [3, 4] =", [1, 2] and [3, 4])
Output:
or -- 0 or None = None 1 or None = 1 0 or 'Hi!' = Hi 1 or 'Hi!' = 1 [] or [] = [] [1, 2] or [] = [1, 2] [] or [3, 4] = [3, 4] [1, 2] or [3, 4] = [1, 2] and --- 0 and None = 0 1 and None = None 0 and 'Hi!' = 0 1 and 'Hi!' = Hi [] and [] = [] [1, 2] and [] = [] [] and [3, 4] = [] [1, 2] and [3, 4] = [3, 4]
"if" and "while" statements are fine with "or" and "and" not returning True or False, they understand true-ey and false-ey.
Mark17 likes this post
Reply
#4
(Jul-10-2022, 02:48 PM)Mark17 Wrote: However, I thought a while back I asked about some weird results I was getting with this sort of thing involving and/or and the reason it didn't turn out as I expected was because once one part was deemed to be False, it did not process the remainder of the expression. I can't remember if that was dealing with and or or.

Does anyone know what I might be misremembering or confused about here? Thanks!

Your memory serves you well.

The source of ultimate truth a.k.a. Python documentation: Truth Value Testing and Boolean Operations — and, or, not

Quote:Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

Quote:[or] This is a short-circuit operator, so it only evaluates the second argument if the first one is false.

[and] This is a short-circuit operator, so it only evaluates the second argument if the first one is true.

I don’t know what book you are reading but maybe you should switch to some other book which is truthy about Python.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of if - and operators Pedro_Castillo 1 500 Oct-24-2023, 08:33 AM
Last Post: deanhystad
  Magic Method Arithmetic Operators ClownPrinceOfCrime 3 2,330 Jan-10-2021, 03:24 PM
Last Post: ndc85430
  Class and Operators in Python rsherry8 1 1,997 May-27-2020, 07:09 PM
Last Post: buran
  Trying Comparison Operators PythonGainz 3 2,729 Mar-28-2020, 10:46 AM
Last Post: PythonGainz
  Mathematical Operators in String AgileAVS 1 2,401 Mar-04-2020, 04:14 PM
Last Post: Gribouillis
  A doubt with 'in' and 'not in' operators with strings newbieAuggie2019 7 3,608 Oct-23-2019, 03:11 PM
Last Post: perfringo
  understanding exponential and bitwise operators srm 1 2,061 Jun-15-2019, 11:14 AM
Last Post: ThomasL
  please help with this question about using operators to multiply a string? GilesTwigg 3 4,405 Feb-27-2019, 04:13 PM
Last Post: ichabod801
  Understanding compound operators -= NewatCode 3 3,189 Apr-25-2018, 05:03 PM
Last Post: Larz60+
  Pycharm shortcuts and operators don't run AzD 6 7,955 Oct-17-2017, 05:46 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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