Python Logical AND Operator

Python – and

To perform logical AND operation in Python, use and keyword.

In this tutorial, we shall learn how and operator works with different permutations of operand values, with the help of well detailed example programs.

Syntax of and Operator

The syntax of python and operator is:

result = operand1 and operand2

and operator returns a boolean value: True or False.

Truth Table

The following table provides the return value for different combinations of operand values.

Operand1Operand2Return Value
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Examples

1. Python and operator with boolean values

In the following example, we take different sets of boolean values into two variables and perform logical and operation between them.

Python Program

# True and True
a = True
b = True

c = a and b
print(a,'and',b,'is:',c)

# True and False
a = True
b = False

c = a and b
print(a,'and',b,'is:',c)

# False and True
a = False
b = True

c = a and b
print(a,'and',b,'is:',c)

# False and False
a = False
b = False

c = a and b
print(a,'and',b,'is:',c)
Run Code Copy

Output

True and True is: True
True and False is: False
False and True is: False
False and False is: False

2. AND Operator with non-boolean operands

When using and operator in boolean expressions, you can also use non-zero numbers instead of True and 0 instead of False.

In the following example, we shall explore the aspect of providing integer values as operands to and operator.

Python Program

# True and True
a = 5
b = 3

c = a and b
print(a,'and',b,'is:',c)

# True and False
a = 8
b = 0

c = a and b
print(a,'and',b,'is:',c)

# False and True
a = 0
b = -8

c = a and b
print(a,'and',b,'is:',c)

# False and False
a = 0
b = 0

c = a and b
print(a,'and',b,'is:',c)
Run Code Copy

Output

5 and 3 is: 3
8 and 0 is: 0
0 and -8 is: 0
0 and 0 is: 0

Summary

In this tutorial of Python Examples, we learned how to use and keyword to perform Logical AND Operation in Python.

Related Tutorials

Quiz on

Q1. Which of the following keyword is used for Logical AND Operation in Python?

Not answered

Q2. What is the result of following boolean expression?

True and True and False
Run Code Copy
Not answered

Q3. What is the result of following Python program?

print(1 and 2 and 3)
Run Code Copy
Not answered

Q4. What is the result of following Python program?

print(0 and 1)
Run Code Copy
Not answered
Code copied to clipboard successfully 👍