Boolean Operators in Python
Table of content
Introduction
Boolean Values in python
Python Boolean operators
- The ‘and’ Operator
- The ‘or’ operator
- The ‘not’ operator
Summary
Introduction
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.
Boolean Values in python
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
Python Boolean operators
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
logical operators in python | insideaiml
Let’s have a brief look at the Boolean operators
The ‘and’ Operator
‘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.
and operator | insideaiml
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
The ‘or’ operator
It is another most commonly used Boolean operator in python, which is used for connecting more than one Boolean expression.
or operator | insideaiml
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
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.
not operator | insideaiml
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
Summary
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.
To know more about the python programming language follow the insideaiml youtube channel.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.