Python AND Operator

In Python, Logical AND Operator takes two boolean values or expressions as operands and returns the result of their logical AND operation. The logical AND operator returns True if both the operands are true, else it returns False.

Syntax

The syntax to find the logical AND of two values: x and y using AND Operator is

</>
Copy
x and y

The above expression returns a boolean value.

Examples

1. Logical AND operation of two values

In the following program, we take two boolean values in x, y; and find the result of their logical AND operation.

main.py

</>
Copy
x = True
y = True
result = x and y
print(result)

Output

True

2. AND Operation for all combinations of operand values

In the following program, we take two boolean values in x, y; and find the result of their AND operation, for all the combinations of the operand values.

main.py

</>
Copy
#both operands are True
x = True
y = True
result = x and y
print(result) #True

#one of the operands is False
x = True
y = False
result = x and y
print(result) #False

x = False
y = True
result = x and y
print(result) #False

#both operands are False
x = False
y = False
result = x and y
print(result) #False

Output

True
False
False
False

3. Logical AND operation of two boolean expressions

In the following program, we take two integer values in x, y, z; and find the largest of these three numbers. In the if-condition, we will use logical AND operator to check if x is greater than y and also x is greater than z.

main.py

</>
Copy
x = 5
y = 2
z = 4

if x > y and x > z :
    print('x is the largest.')

Output

x is the largest.

Conclusion

In this Python Tutorial, we learned about Logical AND Operator, its syntax, and usage, with examples.