Monday, October 25, 2021

Using the "not" Boolean Operator in Python

Python’s not operator allows you to invert the truth value of Boolean expressions and objects. You can use this operator in Boolean contexts, such as if statements and while loops. It also works in non-Boolean contexts, which allows you to invert the truth value of your variables.

Using the not operator effectively will help you write accurate negative Boolean expressions to control the flow of execution in your programs.

In this tutorial, you’ll learn:

  • How Python’s not operator works
  • How to use the not operator in Boolean and non-Boolean contexts
  • How to use the operator.not_() function to perform logical negation
  • How and when to avoid unnecessary negative logic in your code

You’ll also code a few practical examples that will allow you to better understand some of the primary use cases of the not operator and the best practices around its use. To get the most out of this tutorial, you should have some previous knowledge about Boolean logic, conditional statements, and while loops.

Working With Boolean Logic in Python

George Boole put together what is now known as Boolean algebra, which relies on true and false values. It also defines a set of Boolean operations: AND, OR, and NOT. These Boolean values and operators are helpful in programming because they help you decide the course of action in your programs.

In Python, the Boolean type, bool, is a subclass of int:

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

class bool(int)
    bool(x) -> bool
    ...

This type has two possible values, True and False, which are built-in constants in Python and must be capitalized. Internally, Python implements them as integer numbers:

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

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

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

Python internally implements its Boolean values as 1 for True and 0 for False. Go ahead and execute True + True in your interactive shell to see what happens.

Python provides three Boolean or logical operators:

Operator Logical Operation
and Conjunction
or Disjunction
not Negation

With these operators, you can build expressions by connecting Boolean expressions with each other, objects with each other, and even Boolean expressions with objects. Python uses English words for the Boolean operators. These words are keywords of the language, so you can’t use them as identifiers without causing a syntax error.

In this tutorial, you’ll learn about Python’s not operator, which implements the logical NOT operation or negation.

Getting Started With Python’s not Operator

The not operator is the Boolean or logical operator that implements negation in Python. It’s unary, which means that it takes only one operand. The operand can be a Boolean expression or any Python object. Even user-defined objects work. The task of not is to reverse the truth value of its operand.

If you apply not to an operand that evaluates to True, then you get False as a result. If you apply not to a false operand, then you get True:

>>>
>>> not True
False

>>> not False
True

The not operator negates the truth value of its operand. A true operand returns False. A false operand returns True. These two statements uncover what is commonly known as the truth table of not:

operand not operand
True False
False True

With not, you can negate the truth value of any Boolean expression or object. This functionality makes it worthwhile in several situations:

  • Checking unmet conditions in the context of if statements and while loops
  • Inverting the truth value of an object or expression
  • Checking if a value is not in a given container
  • Checking for an object’s identity

Read the full article at https://realpython.com/python-not-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...