Monday, October 19, 2020

Python Booleans: Optimize Your Code With Truth Values

The Python Boolean type is one of Python’s built-in data types. It’s used to represent the truth value of an expression. For example, the expression 1 <= 2 is True, while the expression 0 == 1 is False. Understanding how Python Boolean values behave is important to programming well in Python.

In this tutorial, you’ll learn how to:

  • Manipulate Boolean values with Boolean operators
  • Convert Booleans to other types
  • Convert other types to Python Booleans
  • Use Python Booleans to write efficient and readable Python code

The Python Boolean Type#

The Python Boolean type has only two possible values:

  1. True
  2. False

No other value will have bool as its type. You can check the type of True and False with the built-in type():

>>>
>>> type(False)
<class 'bool'>
>>> type(True)
<class 'bool'>

The type() of both False and True is bool.

The type bool is built in, meaning it’s always available in Python and doesn’t need to be imported. However, the name itself isn’t a keyword in the language. While the following is considered bad style, it’s possible to assign to the name bool:

>>>
>>> bool
<class 'bool'>
>>> bool = "this is not a type"
>>> bool
'this is not a type'

Although technically possible, to avoid confusion it’s highly recommended that you don’t assign a different value to bool.

Python Booleans as Keywords#

Built-in names aren’t keywords. As far as the Python language is concerned, they’re regular variables. If you assign to them, then you’ll override the built-in value.

In contrast, the names True and False are not built-ins. They’re keywords. Unlike many other Python keywords, True and False are Python expressions. Since they’re expressions, they can be used wherever other expressions, like 1 + 1, can be used.

It’s possible to assign a Boolean value to variables, but it’s not possible to assign a value to True:

>>>
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "<stdin>", line 1
SyntaxError: cannot assign to True

Because True is a keyword, you can’t assign a value to it. The same rule applies to False:

>>>
>>> False = 5
  File "<stdin>", line 1
SyntaxError: cannot assign to False

You can’t assign to False because it’s a keyword in Python. In this way, True and False behave like other numeric constants. For example, you can pass 1.5 to functions or assign it to variables. However, it’s impossible to assign a value to 1.5. The statement 1.5 = 5 is not valid Python. Both 1.5 = 5 and False = 5 are invalid Python code and will raise a SyntaxError when parsed.

Python Booleans as Numbers#

Booleans are considered a numeric type in Python. This means they’re numbers for all intents and purposes. In other words, you can apply arithmetic operations to Booleans, and you can also compare them to numbers:

>>>
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0

There aren’t many uses for the numerical nature of Boolean values, but there’s one technique you may find helpful. Because True is equal to 1 and False is equal to 0, adding Booleans together is a quick way to count the number of True values. This can come in handy when you need to count the number of items that satisfy a condition.

Read the full article at https://realpython.com/python-boolean/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...