Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean Python Value confusion

I'm new to Python and while trying Python logical statements.I came across this which I'm not able to understand.Can anyone tell me whats happening here in Python 2.7.Whats the difference between 0 and False value in Python.

>>> 0 or False
False
>>> False or 0
0

Why the interpreter is giving different answers ?

like image 275
Roshan Mehta Avatar asked Nov 04 '13 22:11

Roshan Mehta


People also ask

How do you check if a boolean value is in if in Python?

Remember that True and False are Booleans in Python. This means that if and other conditional statements will use Boolean math to compute their Boolean state. You should also note the need for a colon ( : ) at the end of the if statement. This is needed at the end of a control flow statement.

How do you assign a boolean value in Python?

If you want to define a boolean in Python, you can simply assign a True or False value or even an expression that ultimately evaluates to one of these values. You can check the type of the variable by using the built-in type function in Python.

Is bool true 1 or 0?

Boolean values and operationsConstant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0. The following table shows comparisons and boolean operations.


2 Answers

You are being confused by the behaviour of the or operator; it returns the first expression that only if it is a true value; neither 0 nor False is true so the second value is returned:

>>> 0 or 'bar'
'bar'
>>> False or 'foo'
'foo'

Any value that is not numerical 0, an empty container, None or False is considered true (custom classes can alter that by implementing a __bool__ method (python 3), __nonzero__ (python 2) or __len__ (length 0 is empty).

The second expression is not even evaluated if the first is True:

>>> True or 1 / 0
True

The 1 / 0 expression would raise a ZeroDivision exception, but is not even evaluated by Python.

This is documented in the boolean operators documentation:

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Similarly, and returns the first expression if it is False, otherwise the second expression is returned.

like image 81
Martijn Pieters Avatar answered Sep 24 '22 18:09

Martijn Pieters


The nature of this behavior is in python's order of expression evaluation. Python evaluates expressions from left to right, and it does it in a lazy manner. This means, that ones interpreter reaches the point, when the value of the expression is True, regardless of the rest of the expression, it will follow the branch of workflow, associated with the expression. If none of the expressions is True, it will simply return the most recent (last one). This gives the benefits of saving computational resources. Consider the following code:

>>>False or False or True or range(10**8) 
True
>>>

Note, that range(10**8) is never called in this case, hence, a lot of time is saved.

like image 36
Alexander Zhukov Avatar answered Sep 23 '22 18:09

Alexander Zhukov