Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the practical application of bool() in Python?

When it is being used in everyday coding? I am learning Python using this tutorial. What am I referring to is described here (middle of the page), but I can't get it. I understand the principles of using True and False, but I don't get when (or do) we actually use the bool() function in practice while writing our code. It would help me if you give the everyday, practical example of bool() in code.

like image 887
Muhamed Huseinbašić Avatar asked Jul 21 '14 14:07

Muhamed Huseinbašić


People also ask

What is the use of bool function in Python?

Python bool() function is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.

Why do we use bool?

Boolean variables in C++ convey these types of statements in code. Simply put, a Boolean variable can only have two possible values: true or false. In C++, we use the keyword bool to declare this kind of variable.

What is the use of true in Python?

Definition and Usage The True keyword is a Boolean value, and result of a comparison operation. The True keyword is the same as 1 ( False is the same as 0).

What is bool object in Python?

Python uses the bool class to represent boolean values: True and False . True and False are instances of the bool class. In fact, they're singleton objects of the bool class. Every object has a boolean value, which can be True or False .


2 Answers

It lets you convert any Python value to a boolean value.

Sometimes you want to store either True or False depending on another Python object. Instead of:

if python_object:
    result = True
else:
    result = False

you simply do:

result = bool(python_object)

How Python objects are converted to a boolean value, all depends on their truth value. Generally speaking, None, numeric 0 and empty containers (empty list, dictionary, set, tuple, string, etc.) are all False, the rest is True.

You use it whenever you need an explicit boolean value. Say you are building an object tree, and you want to include a method that returns True if there are children in the tree:

class Tree(object):
    def __init__(self, children):
        self.children = children

    def has_children(self):
        return bool(self.children)

Now Tree().has_children() will return True when self.children is not empty, False otherwise.

like image 77
Martijn Pieters Avatar answered Nov 15 '22 20:11

Martijn Pieters


To understand what bool() does we need to first understand the concept of a boolean.

A boolean variable is represented by either a 0 or 1 in binary in most programming languages. A 1 represents a "True" and a 0 represents a "False"

The number 1 is different from a boolean value of True in some respects. For example, take the following code:

>>> 1 is True
False

Notice that 1 is different than True according to Python. However:

>>> bool(1) is True
True

When we use the bool() function here, we convert 1 to a boolean. This conversion is called "casting". Casting 1 to boolean returns the value of "True".

Most objects can be cast to a boolean value. From my experience, you should expect every standard object to evaluate to True unless it is 0, None, False or an empty iterable (for example: "", [], or {}). So as an example:

>>> bool({})
False
>>> bool({"":False})
True
>>> bool(None)
False
>>> bool("")
False
>>> bool("hello")
True
>>> bool(500)
True
>>> bool(0)
False
>>> bool(False)
False
>>> bool(-1)
True

Lastly, a boolean prints as either "True" or "False"

>>> print bool(1)
True
like image 30
mgoldwasser Avatar answered Nov 15 '22 18:11

mgoldwasser