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.

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)

Explanation of the Code:

  1. First Case (True and True): The result is True because both operands are True.
  2. Second Case (True and False): The result is False because one of the operands is False.
  3. Third Case (False and True): The result is False because one of the operands is False.
  4. Fourth Case (False and False): The result is False because both operands are False.

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:

  1. 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.
  2. Second Case (8 and 0): The result is 0 because the second operand is 0, which is treated as False, so the result is False.
  3. Third Case (0 and -8): The result is 0 because the first operand is 0, which is treated as False, so the result is False.
  4. Fourth Case (0 and 0): The result is 0 because both operands are 0, which is treated as False.

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:

  1. 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.
  2. 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.


Python Libraries