I've been reading through this introductory book on Python, and I've been following along fairly well. Compared to C++, Python seems to make more sense to me. That is, until I reached a section on Logical Operators. I don't completely understand what I just typed in to IDLE, and the results that were returned. Here's what I read:

Python provides three logical operators: and, or, and not. Both and and or use
short-circuit logic and return the operand that determined the result—they do
not return a Boolean (unless they actually have Boolean operands). Let’s see
what this means in practice:
>>> five = 5
>>> two = 2
>>> zero = 0
>>> five and two
2
>>> two and five
5
>>> five and zero
0
If the expression occurs in a Boolean context, the result is evaluated as a
Boolean, so the preceding expressions would come out as True, True, and False
in, say, an if statement.
>>> nought = 0
>>> five or two
5
>>> two or five
2
>>> zero or five
5
>>> zero or nought
0
The or operator is similar; here the results in a Boolean context would be True,
True, True, and False.
The not unary operator evaluates its argument in a Boolean context and
always returns a Boolean result, so to continue the earlier example, not
(zero or nought) would produce True, and not two would produce False.

I don't understand why "five or two" outputs the value of 5, or any of the "or" examples, or why using a Boolean would return True or False. In this example, is the "or" operator comparing the two variables? I'm sorry, I'm probably missing something very simple, but this section of the book made me feel very stupid, lol.
Thanks in advance for any and all help.

Recommended Answers

All 5 Replies

or is shortcut version and in this case it uses Lisp like philosophy of returning something usefull found instead of True or False.
As or is true apon first non-zerolike value, it returns that desiding point's value and does not evaluate rest of the expressions, as they can not make the value of or expression False.

First, every value in python is either True or False in a "boolean context".
A boolean context occurs in "if", "while" or other statements

if myvalue:   # <-- boolean context.
    print("value was true")
else:
    print("value was false")

The expression

bool(myvalue)

is either True or False and this tells us whether myvalue is considered true
or false. For example

print( bool(5), bool(2), bool(None), bool(0) )

will print True, True, False, False.

Now

A or B

has value A if bool(A) is True, and value B otherwise.

A and B

has value A if bool(A) is False and value B otherwise.

ok, that makes a little more sense. Thank you very much, that clarifies the matter a great deal.

A and B
has value A if bool(A) is False and value B otherwise.

Sorry, but it has Value B if bool(A) is True (decision Ok this far in A, B decides the value) and value A otherwise (one False-like value).

Sorry, but it has Value B if bool(A) is True (decision Ok this far in A, B decides the value) and value A otherwise (one False-like value).

It is exactly the same thing as bool(A) is False if and only if it is not True.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.