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”.
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.
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.
Contact Us