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)
Explanation of the Code:
- First Case (True and True): The result is
True
because both operands areTrue
. - Second Case (True and False): The result is
False
because one of the operands isFalse
. - Third Case (False and True): The result is
False
because one of the operands isFalse
. - Fourth Case (False and False): The result is
False
because both operands areFalse
.
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)
Explanation of the Code:
- First Case (5 and 3): The result is
3
because both operands are non-zero, and the and operator returns the second operand if the first is true. - Second Case (8 and 0): The result is
0
because the second operand is0
, which is treated asFalse
, so the result isFalse
. - Third Case (0 and -8): The result is
0
because the first operand is0
, which is treated asFalse
, so the result isFalse
. - Fourth Case (0 and 0): The result is
0
because both operands are0
, which is treated asFalse
.
Output
5 and 3 is: 3
8 and 0 is: 0
0 and -8 is: 0
0 and 0 is: 0
3. AND Operator with Strings
The and operator can also be used with strings. If the first string is non-empty (evaluates to True
), Python will return the second string. Otherwise, it will return the first string.
Python Program
#Non-empty string and empty string
str1 = 'Hello'
str2 = ''
result = str1 and str2
print('Result of non-empty string and empty string is:', result)
#Empty string and non-empty string
str1 = ''
str2 = 'Python'
result = str1 and str2
print('Result of empty string and non-empty string is:', result)
Explanation of the Code:
- First Case (non-empty string and empty string): The result is the empty string (
''
) because the first operand is non-empty, and the and operator returns the second operand only if the first is true. - Second Case (empty string and non-empty string): The result is the empty string (
''
) because the first operand is empty, so Python returns the first operand.
Output
Result of non-empty string and empty string is:
Result of empty string and non-empty string is:
Summary
In this tutorial, we learned how to use the and keyword to perform Logical AND operations in Python with different data types such as boolean values, integers, and strings.