## Introduction

Logical operators are an essential part of any programming language and play a fundamental role in decision making for the program. In Python, there are three main logical operators: ` and `

, ` or `

, and ` not `

. These operators allow us to combine multiple conditions and create complex logical expressions. Understanding how to use these logical operators in Python is crucial for any software developer who wants to write efficient and effective code. Throughout this tutorial, we will explore the concepts of logical operators in Python and demonstrate how to use them in practical coding examples.

Table of Contents :

*and*operator in python*or*operator in python*not*operator in python- Precedence of Logical Operators
- Using
*any()*and*all()*built-in functions in Python

- Logical operators are also called Boolean operators.
- Python uses English words to denote logical operators
- Logical operators are used to evaluate multiple logical conditions simultaneously
- Logical operators combine multiple boolean expressions and values to form one elaborate expression.
- Python has three logical operators:
- and
- or
- not

## The *and *operator in python :

`and`

- The operands of
`and`

- It is used to evaluate if the two operands are both True simultaneously or not.
- The
`and`

**operator for conjunction**. - Return values of
`and`

- it returns True if both conditions are True.
- it returns False if one of the conditions is False.
- If one or more operands of
`and`

**Code Sample :**

```
# If both conditions are True
# it returns True
x = True
y = True
res = x and y
print(res)
# Output
# True
# If one of the conditions is False
# it returns False
x = True
y = False
res = x and y
print(res)
# Output
# False
# If one or more operands is an arithmetic value,
# it returns the second value
x = True
y = 100
res = x and y
print(res)
# Output
# 100
# If one or more operands is an arithmetic value,
# it returns the second value
x = 100
y = True
res = x and y
print(res)
# Output
# True
```

- Below is the truth table for the
`and`

operand-1 | operand-2 | operand-1 and operand-2 |
---|---|---|

True | True | True |

True | False | False |

False | False | False |

False | True | False |

## The *or *operator in python :

`or`

- The operands of
`or`

- Similar to the
`and`

`or`

- The
`or`

**operator for disjunction** - Return values of
`or`

- it returns True when at-least one conditions is True
- it returns False if both the conditions are False.
- If one or more operands of
`or`

**Code Sample :**

```
# When at-least one conditions is True
# it returns True
x = True
y = True
res = x or y
print(res)
# Output
# True
# When at-least one conditions is True
# it returns True
x = True
y = False
res = x or y
print(res)
# Output
# True
# When both the conditions are False
# it returns False
x = False
y = False
res = x or y
print(res)
# Output
# False
# If one or more operands is an arithmetic value,
# it returns the first value
x = True
y = 100
res = x or y
print(res)
# Output
# True
# If one or more operands is an arithmetic value,
# it returns the first value
x = 100
y = True
res = x or y
print(res)
# Output
# 100
```

- Below is the truth table for the
`or`

operand-1 | operand-2 | operand-1 or operand-2 |
---|---|---|

True | True | True |

True | False | True |

False | False | False |

False | True | True |

## The *not *operator in python :

- The
`not`

- The operands of
`not`

- The
`not`

**operator for negation** - Return values of
`not`

- If the operand is True it returns False
- If the operand is False it returns True
- If the operand of
`not`

- If the operand of
`not`

**Code Sample :**

```
# If the operand is True it returns False
x = True
res = not x
print(res)
# Output
# False
# If the operand is False it returns True
x = False
res = not x
print(res)
# Output
# True
# If the operand is a nonzero arithmetic value
# it returns False
x = 100
res = not x
print(res)
# Output
# False
# If the operand of not operator is zero it returns True
x = 0
res = not x
print(res)
# Output
# True
```

- Below is the truth table for the
`not`

operand | not operand |
---|---|

True | False |

False | True |

## Precedence of Logical Operators

- When we use more than one operators in a single expression, Python uses operator precedence to evaluate the expression.
- Same is the case with logical operators.
- The precedence of logical operators is :
**not > and > or** - To evaluate an expression Python first groups the operands of the operator with the highest precedence, then it groups the operands of the operator with the lower precedence and so on.
- If an expression has more than one logical operators with the same precedence Python evaluates them from the left to right
- Evaluation of some logical expressions as per precedence :
**a****or****b****and****c :**a or (b and c)**a****and****b****or****c****and****d :**(a and b) or (c and d)**a****and****b****and****c****or****d :**((a and b) and c) or d**not****a****and****b****or****c :**((not a) and b) or c

## Using *any() *and *all() *built-in functions in Python

**Note # **This is an intermediate level topic and should be studied only after studying the topic Functions in Python

- Python has two built-in functions called
- any()
- all().

*any()* built in function in python

- any() takes one or more arguments.
- It returns True if any of the arguments are True.
- If all of the arguments are False, it returns False.
- Here are some examples:

```
any([True, False, False]) returns True
any([False, False, False]) returns False
any(["a", "", "b"]) returns True
any(["", "", ""]) returns False
```

*all()* built in function in python

- all() takes one or more arguments.
- It returns True if all of the arguments are True.
- If any of the arguments are False, it returns False.
- Here are some examples:

```
all([True, True, True]) returns True
all([True, False, True]) returns False
all(["a", "b", "c"]) returns True
all(["a", "", "b"]) returns False
```

Prev. Tutorial : Relational operators

Next Tutorial : Membership operators