# Basic Operators in Python

Operators are used to manipulate variables and values by performing operations on them. They are an essential part of any programming language. Python has the following types of operators:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators

Let’s look at the details of these operator types one by one.

## Arithmetic operators

These are used to perform simple mathematical operations on numerals.

**Note:** Let’s assume x = 20 and y = 40 for examples.

Operator | Name | Description | Example |

+ | Addition | Adds two numbers | x + y = 60 |

– | Subtraction | Subtracts two numbers | x – y = -20 |

* | Multiplication | Multiplies two numbers | x * y = 800 |

/ | Division | Divides two numbers | y / x = 2 |

% | Modulus | Divides left side operand by right side operand and returns remainder | y % x = 0 |

** | Exponentiation | Returns left hand operator raised to the power right hand operator | x ** y = 20 raised to the power 40 |

// | Floor division | If the answer of division is a decimal number, it returns its floor (the lowest possible number after round-off) | 7 // 3 = 2, -7 // 3 = -3 |

## Assignment operators

These are used to assign values to variables.

Operator | Description | Example |

= | Assigns right side operand to left side | x = 5 |

+= | Adds right side operand to left side and stores result in left side operand | x += 3 |

-= | Subtracts right side operand to left side and stores result in left side operand | x -= 3 |

*= | Multiplies right side operand by left side and stores result in left side operand | x *= 3 |

/= | Divides left side operand by right side and stores result in left side operand | x /= 3 |

%= | Performs modulus between both operands and stores result in left side operand | x %= 3 |

//= | Performs floor division between both operands and stores result in left side operand | x //= 3 |

**= | Performs exponentiation between both operands and stores result in left side operand | x **= 3 |

## Comparison operators

These are used to compare two variables and are commonly used in while loops or conditionals.

**Note:** Let’s assume x = 20 and y = 40 for examples.

Operator | Description | Example |

== | Returns true if the values of two operands are equal | (x == y) is not true |

!= | Returns true if values of two operands are not equal | (x != y) is true |

<> | Returns true if values of two operands are not equal | (x <> y) is true |

> | Returns true if the value of left operand is greater than the value of right operand | (x > y) is not true |

< | Returns true if the value of left operand is less than the value of right operand | (x < y) is true |

>= | Returns true if the value of left operand is greater than or equal to the value of right operand | (x >= y) is not true |

<= | Returns true if the value of left operand is less than or equal to the value of right operand | (x <= y) is true |

## Logical operators

Following logical operators are supported by Python:

**Note:** Let’s assume a = True and b = False for examples.

Operator | Description | Example |

and | Returns True if both statements are true | (a and b) is False |

or | Returns True if one of the statements is true | (a or b) is True |

not | Reverses the result | not(a and b) is True |

## Identity operators

These are used to compare the objects returning whether they are actually the same object with the same memory location.

Operator | Description | Example |

is | Returns True if both variables are the same object | (x is y) is False |

is not | Returns True if both variables are not the same object | (x is not y) is True |

## Membership operators

These are used to test if an element is present in a sequence (list, array, tuple) or not.

**Note:** Let’s assume p = 2 and q = [0, 2, 4, 6, 8] for examples.

Operator | Description | Example |

in | Returns True if it finds a variable in the specified sequence | (p in q) is True |

not in | Returns True if it doesn’t find a variable in the specified sequence | (x not in y) is False |

## Bitwise operators

These operators work on bits and perform bit by bit operation on binary numbers.

**Note:** Let’s assume u = 0011 1100 and v = 0000 1101 for examples.

Operator | Name | Description | Example |

& | AND | Sets each bit to 1 if both bits are 1 | u & v = 0000 1100 |

| | OR | Sets each bit to 1 if one of two bits is 1 | u | v = 0011 1101 |

^ | XOR | Sets each bit to 1 if only one of two bits is 1 | u ^ v = 0011 0001 |

~ | NOT | Inverts all the bits | ~ u = 1100 0011 |

<< | Left shift | Shift left by pushing zeros in from the right and removes the leftmost bits | u << 2 = 1111 0000 |

>> | Right shift | Shift right by pushing copies of the leftmost bit in from the left, and removes the rightmost bits | u >> 2 = 0000 1111 |

## Operator Precedence

The following table lists operators in the order of their precedence in Python:

No. | Operator |

1 | Exponentiation |

2 | Complement, unary plus and minus |

3 | Multiply, divide, modulo and floor division |

4 | Addition and subtraction |

5 | Right and left bitwise shift |

6 | Bitwise ‘AND’ |

7 | Bitwise exclusive ‘OR’ and regular ‘OR’ |

8 | Comparison operators |

9 | Equality operators |

10 | Assignment operators |

11 | Identity operators |

12 | Membership operators |

13 | Logical operators |