AND Operator in Python

The Boolean AND operator returns True if both the operands are True else it returns False.

Logical AND operator in Python Examples

Let’s look at some Python AND operator programs, and understand the workings of AND operator.

Example 1: The code initializes variables a, b, and c, then checks if a and b are greater than 0, and prints “The numbers are greater than 0” if true; it also checks if all three variables are greater than 0, printing the same message, otherwise, it prints “At least one number is not greater than 0”.

Python
a = 10
b = 10
c = -10
if a > 0 and b > 0:
    print("The numbers are greater than 0")
if a > 0 and b > 0 and c > 0:
    print("The numbers are greater than 0")
else:
    print("Atleast one number is not greater than 0")

Output

The numbers are greater than 0
Atleast one number is not greater than 0

Example 2: The code checks if all variables a, b, and c evaluate to True, printing a message accordingly.

Python
a = 10
b = 12
c = 0
if a and b and c:
    print("All the numbers have boolean value as True")
else:
    print("Atleast one number has boolean value as False")

Output

Atleast one number has boolean value as False

Note: If the first expression is evaluated to be false while using the AND operator, then the further expressions are not evaluated.

Python Logical Operators

Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss logical operators in Python definition and also look at some Python logical operators programs, to completely grasp the concept.

Similar Reads

Logical Operators in Python

In Python, Logical operators are used on conditional statements (either True or False). They perform Logical AND, Logical OR, and Logical NOT operations....

AND Operator in Python

The Boolean AND operator returns True if both the operands are True else it returns False....

Python OR Operator

The Boolean OR operator returns True if either of the operands is True....

Python NOT Operator

The Boolean NOT operator works with a single boolean value. If the boolean value is True it returns False and vice-versa....

Order of Precedence of Logical Operators

In the case of multiple operators, Python always evaluates the expression from left to right. We can verify Python logical operators precedence by the below example....

Contact Us