## Introduction

Back in the Python Basics chapter, we saw how to add together two numbers using the `+`

symbol, how to subtract them using the `-`

symbol, and so on.

These symbols, known as operators, along with the function they perform on given values, known as operands, is what we will study in this chapter. We'll explore a wide variety of operators in Python, see what are they used for, and finally consider examples for each set of operators.

## What are operations?

Understanding what exactly is an operation is very easy if we take the example below:

Here as you can see, two numbers are being added. What's happening is an operation known as addition.

Every operation has a corresponding operator that operates on given operands. To understand this we first need to understand what exactly is meant by the terms 'operator' and 'operands'.

Taking the example above, `3 + 5`

represents the addition operation. Here `+`

is the operator of the addition operation, whereas `3`

and `5`

are the operands of this operation.

**symbol**, or

**keyword**, that represents a given operation.

**value**on which a given operation takes place.

Let's consider another example.

Below shown is the logical OR operation. It returns `True`

if either of the operands is `True`

, and `False`

otherwise.

`True or False`

Can you name the operator and operands over here?

`True`

and `False`

are the operands on which the logical OR operation takes place, while the keyword `or`

is the operator that represents the logical OR operation.

## Operators in Python

In Python, there are a handful of operators as we shall see in the sections that follow, each serving a specific job. We've divided the operators based on the type of operation they perform.

This gives us the following categories of operators:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
- The concatenation operator

### Arithmetic operators

Arithmetic operators perform mathematical operations on numbers such as addition, subtraction, exponentiation and so on.

Operation | Operator | Syntax | Purpose |
---|---|---|---|

Addition | `+` |
| Adds the numbers and `x` `y` |

Subtraction | `-` |
| Subtracts from `y` `x` |

Multiplication | `*` |
| Multiplies with `x` `y` |

Division | `/` |
| Divides by `x` ; returns a float`y` |

Floor Division | `//` |
| Divides by `x` ; returns the floor of the result`y` |

Exponentiation | `**` |
| Raises to the power of `x` `y` |

Modulo | `%` |
| Returns the remainder when is divided by `x` `y` |

### Assignment operators

Assignment operators assign a value to an identifier in Python. The best example is of the `=`

equals sign.

Compound assignment operators perform an operation on the value and then assign it to the given identifer. They are frequently used to shorten reassignment statements.

Operation | Operator | Syntax | Purpose |
---|---|---|---|

Assignment | `=` |
| Assigns to `value` `identifier` |

Addition-assignment | `+=` |
| Evaluate , then assign to `x` |

Subtraction-assignment | `-=` |
| Evaluate , then assign to `x` |

Multiplication-assignment | `*=` |
| Evaluate , then assign to `x` |

Division-assignment | `/=` |
| Evaluate , then assign to `x` |

Exponentiation-assignment | `**=` |
| Evaluate , then assign to `x` |

Modulo-assignment | `%=` |
| Evaluate , then assign to `x` |

### Comparison operators

Comparison operator are used throughout Python to compare two given values together.

The most basic one of comparison operators is the equality operator, denoted by `==`

(double equals sign). It compares two values and returns `True`

if they both are equal to one another; and `False`

otherwise.

Operation | Operator | Syntax | Purpose |
---|---|---|---|

Equality | `==` |
| Returns `True` if is equal to `x` `y` , and `False` otherwise. |

Negated equality | `!=` |
| Returns `True` if `x` is not equal to `y` , and `False` otherwise. |

Greater than or equal to | `>=` |
| Return `True` if is greater than or equal to , and `False` otherwise. |

Lesser than or equal to | `<=` |
| Return `True` if is lesser than or equal to , and `False` otherwise. |

Greater than | `>` |
| Return `True` if is greater than , and `False` otherwise. |

Lesser than | `<` |
| Return `True` if is lesser than , and `False` otherwise. |

### Logical operators

Logical operators evaluate given Boolean values and return `True`

or `False`

based on their truthy/falsy values.

Logical operators are frequently used in conditional statements to combine the results of two or more Boolean values.

Operation | Operator | Syntax | Purpose |
---|---|---|---|

Logical NOT | `not` | `not ` | Returns `True` if `x` is `False` , and otherwise `False` |

Logical OR | `or` |
| Returns `True` if , or `x` or both are `y` `True` , and otherwise `False` |

Logical AND | `and` |
| Returns `True` if and `x` are both `y` `True` , and otherwise `False` |

*'not'*,

*'or'*and

*'and'*, head over to Propositional Operators.

### Bitwise operators

Bitwise operators evaluate given values and process their corresponding bits to return a given value.

Operation | Operator | Syntax | Purpose |
---|---|---|---|

Bitwise NOT | `~` | `~` | Inverts each bit in and returns the result.`x` |

Bitwise OR | `|` |
| Processes corresponding bits in and `x` using the OR operation, and returns the result.`y` |

Bitwise AND | `&` |
| Processes corresponding bits in and `x` using the AND operation, and returns the result.`y` |

Bitwise XOR | `^` |
| Processes corresponding bits in and `x` using the XOR operation, and returns the result.`y` |

Bitwise left-shift | `<<` |
| Shifts each bit in to the left by `x` places.`y` |

Bitwise right-shift | `>>` |
| Shifts each bit in to the right by `x` places, adding zeroes to the right.`y` |

*'not'*,

*'or'*,

*'xor'*and

*'and'*, head over to Propositional Operators.

### Concatenation operator

There is one operator that's used to join together given sequences, that's also used in arithmetic operations. It's the `+`

symbol.

When used on strings, for instance, `+`

serves to join together the strings into one single string. Similarly, when used on lists, `+`

adds together the given lists and returns back the result.

In programming terminology, this is referred to as ** concatenation**. Concatenation is to add together two sequences to produce a single sequence.

Operation | Operator | Syntax | Purpose |
---|---|---|---|

Concatenation | `+` |
| Concatenates the sequences and `a` together.`b` |