Python AND Operator - Examples
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.
Operand1 | Operand2 | Return Value |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
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)
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)
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.