Monday, September 20, 2021

Using the "and" Boolean Operator in Python

Python has three Boolean operators, or logical operators: and, or, and not. You can use them to check if certain conditions are met before deciding the execution path your programs will follow. In this tutorial, you’ll learn about the and operator and how to use it in your code.

In this tutorial, you’ll learn how to:

  • Understand the logic behind Python’s and operator
  • Build and understand Boolean and non-Boolean expressions that use the and operator
  • Use the and operator in Boolean contexts to decide the course of action of your programs
  • Use the and operator in non-Boolean contexts to make your code more concise

You’ll also code a few practical examples that will help you understand how to use the and operator to approach different problems in a Pythonic way. Even if you don’t use all the features of and, learning about them will allow you to write better and more accurate code.

Working With Boolean Logic in Python

Back in 1854, George Boole authored The Laws of Thought, which contains what’s known as Boolean algebra. This algebra relies on two values: true and false. It also defines a set of Boolean operations, also known as logical operations, denoted by the generic operators AND, OR, and NOT.

These Boolean values and operators are pretty helpful in programming. For example, you can construct arbitrarily complex Boolean expressions with the operators and determine their resulting truth value as true or false. You can use the truth value of Boolean expressions to decide the course of action of your programs.

In Python, the Boolean type bool is a subclass of int and can take the values True or False:

>>>
>>> issubclass(bool, int)
True
>>> help(bool)
Help on class bool in module builtins:

class bool(int)
    ...

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

>>> isinstance(True, int)
True
>>> isinstance(False, int)
True

>>> int(True)
1
>>> int(False)
0

As you can see in this code, Python implements bool as a subclass of int with two possible values, True and False. These values are built-in constants in Python. They’re internally implemented as integer numbers with the value 1 for True and 0 for False. Note that both True and False must be capitalized.

Along with the bool type, Python provides three Boolean operators, or logical operators, that allow you to combine Boolean expressions and objects into more elaborate expressions. Those operators are the following:

Operator Logical Operation
and Conjunction
or Disjunction
not Negation

With these operators, you can connect several Boolean expressions and objects to build your own expressions. Unlike other languages, Python uses English words to denote Boolean operators. These words are keywords of the language, so you can’t use them as identifiers.

In this tutorial, you’ll learn about Python’s and operator. This operator implements the logical AND operation. You’ll learn how it works and how to use it either in a Boolean or non-Boolean context.

Getting Started With Python’s and Operator

Python’s and operator takes two operands, which can be Boolean expressions, objects, or a combination. With those operands, the and operator builds more elaborate expressions. The operands in an and expression are commonly known as conditions. If both conditions are true, then the and expression returns a true result. Otherwise, it returns a false result:

>>>
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False

These examples show that an and expression only returns True when both operands in the expressions are true. Since the and operator takes two operands to build an expression, it’s a binary operator.

The quick examples above show what’s known as the and operator’s truth table:

operand1 operand2 operand1 and operand2
True True True
True False False
False False False
False True False

This table summarizes the resulting truth value of a Boolean expression like operand1 and operand2. The result of the expression depends on the truth values of its operands. It’ll be true if both are true. Otherwise, it’ll be false. This is the general logic behind the and operator. However, this operator can do more than that in Python.

In the following sections, you’ll learn how to use and for building your own expressions with different types of operands.

Using Python’s and Operator With Boolean Expressions

You’ll typically use logical operators to build compound Boolean expressions, which are combinations of variables and values that produce a Boolean value as a result. In other words, Boolean expressions return True or False.

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


[ 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...