Logical Operators in Python with Examples

Introduction:

In this tutorial, we are learning about the Logical Operators in Python with Examples. Python logical operators are used to create Boolean expressions. Each operation of these logical operators is itself a Boolean operator. Operators are used to perform functions of values and variables. These are special characters for arithmetic and logical computations. The values that the operator operates on are called operands. Here, we look at some Python logical operator methods to understand their meaning.

Example:

Besides false expressions, Python falsely interprets zero integers of all types, empty sequences (strings, definitions, names), empty dictionaries, and empty sets as false. All other values are assumed to be True. There are 3 logical operators in Python. These are "and", "or" and "not". They must be represented in lowercase.

Logical operator:

In Python, the Logical operator is used for conditional statements (True or False). They can perform logical AND, logical OR, and logical NOT operations. The chart of these operators is given below -

OperatorReturn ValueSyntaxExample
logical ANDIf both the operands are true, then it returns true; otherwise, it returns false.a and ba>3 and b>5
logical ORIf one of the operands is true, then it returns true. Otherwise, it returns false.a or ba>3 or b<5
logical NOTIf both the operands are false, then it returns true. Otherwise, it returns false.not anot(a<3 and b<5)

The truth table of the logical operator:

aba and ba or bnot anot b
111100
100101
010110
000011

Logical AND operator in Python:

For a compound Boolean expression to be true, both operands must be true. If one or both operands are evaluated as False, the expression returns False.

Logical Operators in Python with Examples

Program Code 1:

Here, we give a program code of logical "and" operators in Python. The code is given below -

Output:

Now, we run the above code and find the result. The result is given below -

The numbers of two operands are greater than 10
At least one number of the operand is not greater than 10

Program Code 2:

Here, we give another program code of logical "and" operators in Python. The code is given below -

Output:

Now, we run the above code and find the result. The result is given below -

At least the value of two operands is true

Logical OR operator in Python:

The variable or operator returns true if any of its operands are true. For a compound Boolean expression to be False, both operands must be False.

Logical Operators in Python with Examples

Program Code 1:

Here, we give a program code of logical "or" operator in Python. The code is given below -

Output:

Now, we run the above code and find the result. The result is given below -

One of the operands is greater than 10
Not any operand is greater than 10

Program Code 2:

Here, we give another program code of logical "or" operator in Python. The code is given below -

Output:

Now, we run the above code and find the result. The result is given below -

At least one number has a Boolean value is True

Program Code 3:

Here, we give another program code of logical "or" operator in Python. The code is given below -

Output:

Now, we run the above code and find the result. The result is given below -

a or b: 5
c or a: 20
b or c: 20

Logical NOT operator in Python:

This is a unary operator. The state of subsequent Boolean operands is reversed. Therefore, no true will be false, and no false will be true.

Logical Operators in Python with Examples

Program Code 1:

Here, we give a program code of the logical "not" operator in Python. The code is given below -

Output:

Now, we run the above code and find the result. The result is given below -

The boolean value of x is divisible by either 2 or 6

Program Code 2:

Here, we give another program code of the logical "not" operator in Python. The code is given below -

Output:

Now, we run the above code and find the result. The result is given below -

not (a+b > 20): True

Use of multiple operators:

In the case of multiple operators, firstly, Python always evaluates the given expressions from left to right. We can understand the importance of Python logical operators from the examples below.

Program Code 1:

Here we give a program code of multiple operators in Python. The code is given below -

Output:

Now, we run the above code and find the result. The result is given below -

x > 0 and y < 10 and z > 0: False

Program Code 2:

Here we give another program code of multiple operators in Python. The code is given below-

Output:

Now, we run the above code and find the result. The result is given below -

The value of the method called is: 2
At least one of the operands is positive