AKP2002
AKP2002

Reputation: 103

Boolean Operations on Non Boolean variables

a = 5
b = -1
c = 2

print((c) and (b<0) and (a>0))

The output of the following code is True. But what is meant by (c) and (b<0) ain't and and or used for Boolean type variables that c is a integer ?

Upvotes: 0

Views: 1507

Answers (3)

chepner
chepner

Reputation: 532218

In languages like C, there is no Boolean type or values; rather, integers are used in their place. Zero is false, all other integers are true.

Python generalizes this to all values. True, there is a Boolean type, but it's a subclass of int, and True and False are literally the values 1 and 0, respectively. Any value can be converted to a Boolean value by calling bool on it.

>>> bool(5)
True
>>> bool(0)
False
>>> bool("foo"), bool([1,2,3])
True, True
>>> bool(""), bool([])
False, False

The "truthiness" of a value depends on its type, but roughly speaking, zeros and empty container-like values are False, and all other values are True. Further, and and or don't necessarily return a Boolean value:

x and y == y if bool(x) else x
x or y == x if bool(x) else y

bool is used to determine if x is true or not, but the result is one of the two actual values x or y.

Using the equivalence for and, we can evaluate your expression as

c and b<0 and a>0 == (b < 0 if bool(c) else c) and a > 0
                  == b < 0 if bool(2) else c) and a > 0
                  == (b < 0 if True else c) and a > 0
                  == b < 0 and a > 0
                  == a > 0 if bool(b < 0) else b < 0
                  == a > 0 if bool(-1 < 0) else b < 0
                  == a > 0 if bool(True) else b < 0
                  == a > 0 if True else b < 0
                  == a > 0
                  == 5 > 0
                  == True

I didn't substitute the values for a, b, and c all at once because and, or, and the conditional expression are all lazy; they don't evaluate an operand unless it's absolutely necessary, as we'll see in a moment.

If you change c = 0, the evaluation process produces the value of c, not the Boolean value of c. Note that we need to look at the value of a, because a > 0 is never evaluated.

c and b<0 and a>0 == (b < 0 if bool(c) else c) and a > 0
                  == (b < 0 if bool(0) else c) and a > 0
                  == (b < 0 if False else c) and a > 0
                  == c and a > 0
                  == a > 0 if bool(c) else c
                  == a > 0 if bool(0) else c
                  == a > 0 if False else c
                  == c
                  == 0

Upvotes: 2

Gustavo Tino
Gustavo Tino

Reputation: 11

When a variable is not a bool and you check if it's True or False, python check if this variable is None or not.

For exemple:

    a = 'any_value'
    print(a == True)

This statement prints True.

But:

    b = None
    print(a == True)

Results in False.

Upvotes: 1

Chris
Chris

Reputation: 23179

In python, any object can be tested for a truth value. In the case of numeric types, any non-zero value is considered true. You can see the truth values of different types here.

Upvotes: 1

Related Questions