Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean testing: Python prints '1' or 'True' [duplicate]

I did a simple test like below:

>>> valsH["S0"] = 1
>>> valsH["I1"] = 0
>>> valsH["I2"] = 1
>>> valsH["I0"] = 1
>>> """original position of: not valsH["I1"]"""
>>>
>>> valsH["I0"] and not valsH["I1"] and valsH["I2"] and valsH["S0"]
1
>>> """After moving: not valsH["I1"] to the end of the expression"""
>>>
>>> valsH["I0"] and valsH["I2"] and valsH["S0"] and not valsH["I1"]
True
>>> 

So it seems that depending on where

not valsH["I1"]

is, the value of the Boolean equation is printed as '1' or 'True'.

Why is this so?

like image 577
user3602207 Avatar asked Mar 12 '26 19:03

user3602207


1 Answers

This is by how and works. Let's take the following example:

p and q

First evaluate p, if p is true, evaluate q. If q is true, it returns True but of whatever type q. So:

1 and True # True
True and 1 # 1 

For or it goes the other way around. Since only one is required to be true, if the first is true, it returns true of that type. So:

1 or True # 1
True or 1 # True
like image 69
Lucas Avatar answered Mar 15 '26 08:03

Lucas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!