Download our e-book of Introduction To Python

Matplotlib - Subplot2grid() FunctionDiscuss Microsoft Cognitive ToolkitMatplotlib - Working with ImagesMatplotlib - PyLab moduleMatplotlib - Working With TextMatplotlib - Setting Ticks and Tick LabelsCNTK - Creating First Neural NetworkMatplotlib - MultiplotsMatplotlib - Quiver PlotPython - Chunks and Chinks View More

How can I write Python code to change a date string from "mm/dd/yy hh: mm" format to "YYYY-MM-DD HH: mm" format? Which sorting technique is used by sort() and sorted() functions of python? How to use Enum in python? Can you please help me with this error? I was just selecting some random columns from the diabetes dataset of sklearn. Decision tree is a classification algo...How can it be applied to load diabetes dataset which has DV continuous Objects in Python are mutable or immutable? How can unclassified data in a dataset be effectively managed when utilizing a decision tree-based classification model in Python? How to leave/exit/deactivate a Python virtualenvironment Join Discussion

Pallavi Dhotre

2 years ago

Introduction

Boolean Values in python

Python Boolean operators

- The ‘and’ Operator

- The ‘or’ operator

- The ‘not’ operator

Summary

In this article, we will learn about the Python Boolean operators and the types of Boolean operators. There are two Boolean values in python (True, False), and these values are also known as a Boolean data type in python. Therefore, the expression which results in a Boolean value is known as a Boolean expression in python. In general, expression is a combination of two or more operands with one or more operators. For example, a+b in this expression, a and b are operands and ‘+’ is an operator. Similarly, if the relationship between two or more operands is denoted with the help of relational operators, then the expression is known as a Boolean expression. For example, a > b, where ‘>’ is a relational operator. So, a>b is a Boolean expression.

True and False these two values are known as boolean values in python. Python Boolean variable can have one of these values. For example flag=True

Logical operators denote the relationship between two Boolean expressions in python. Logical operators are also referred to as python Boolean operators. Two or more conditions can be combined using Boolean operators. Conditions are commonly referred to as a Boolean expression in python. The syntax for using Boolean operator is:

Boolean_Expression1 Boolean_operator Boolean_Expression 2

Following are the logical operators used in python

Let’s have a brief look at the Boolean operators

'and' is very important and primarily used among all three python Boolean operators. 'and' checks that all the conditions in the Boolean expressions are fulfilling. For example, the table given below shows different conditions in which the outcome/result is decided.

The easy trick to remember this table – Only when all the Boolean expressions are true, then the final result is true; otherwise the result is false.

See the following code for a better understanding

```
a=10
b=5
c=10
d=5
print(a>b and c>d) # both conditions are true
print(a<b and c>d) #1st condition is true second is false
print(a>b and c<d) #1st condition is false second is True
print(a<b and c<d) #both conditions are false
```

Output

```
True
False
False
False
```

It is another most commonly used Boolean operator in python, which is used for connecting more than one Boolean expression.

An easy trick to remember this: Only when both the Boolean expressions are false, then the result is false otherwise true. In other words, if any of the expressions is True, the overall result is True.

See the following code for a better understanding

```
a=10
b=5
c=10
d=5
print(a>b or c>d) # both conditions are true
print(a<b or c>d) #1st condition is true second is false
print(a>b or c<d) #1st condition is false second is True
print(a<b or c<d) #both conditions are false
```

Output

```
True
True
True
False
```

The ‘not’ operator is used to negate the expression.

When you use a Boolean Expression (BE) preceded by a ‘not’ operator, it means that the whole complex Boolean expression is True when the Boolean expression BE is False and vice versa.

See the following code for a better understanding

```
a=10
b=5
c=10
d=5
print(not(a>b)) # actual result of a>b is true
print(not(c<d)) # actual result of c<d is false
```

Output:

```
False
True
```

In this article, we have learned about Boolean values in python, Boolean expression in python, python Boolean operators, and types of Boolean operators in python. Boolean data type in python can have one of the two values (True, False). Logical operators ‘and’, ‘or’, not are also called python Boolean operators. Boolean operators denote the relationship between two Boolean expressions. And the result of the python boolean expressions is always boolean values in python We hope you enjoyed the article. If you have any related queries regarding python Boolean operators, feel free to ask in the comment section below.

Like the Blog, then Share it with your friends and colleagues to make this AI community stronger.

To learn more about nuances of Artificial Intelligence, Python Programming, Deep Learning, Data Science and Machine Learning, visit our insideAIML blog page.

Keep Learning. Keep Growing.