#### World's Best AI Learning Platform with profoundly Demanding Certification Programs

Designed by IITian's, only for AI Learners.

Download our e-book of Introduction To Python

about installing the softwere Exception Type: JSONDecodeError at /update/ Exception Value: Expecting value: line 1 column 1 (char 0) How to leave/exit/deactivate a Python virtualenvironment What is the difference between covariance and correlation? TypeError: 'int' object is not subscriptable Which are different modes to open a file ? How to handle imbalanced data and achieve good performance? What does it mean to cross-validate a machine learning model? Join Discussion

4 (4,001 Ratings)

218 Learners

Pallavi Dhotre

6 months 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.