# Complete Guide to Using the Various Python Operators

## Operators in Python

Operators in programming are symbolic constructs used to perform different operations on variables and values.

These operations can be arithmetic, comparison, assignment, logical, etc.

For example:

```
x = 1
y = 4
z = x + y
```

In the example above, `x`

and `y`

are variables assigned 1 and 4 respectively using the `=`

operator. Afterward, we use the `+`

operator to add together `x`

and `y`

and assign the outcome to `z`

.

## Types of Operators in Python

In Python programming language, operators are grouped into different categories based on their functions.

Arithmetic operators

Bitwise operators

Assignment operators

Comparison operators

Logical operators

Special operators

Identity operators

Membership operators

## Arithmetic Operators in Python

Arithmetic operators are used for basic mathematical operations on numeric values or variables bearing numeric values.

Operator | Name | Example | Description | Code Example |
---|---|---|---|---|

+ | Addition | x + y | Adds x and y. | Output: 7 |

- | Subtraction | x - y | Subtracts y from x. | Output: 3 |

* | Multiplication | x * y | Multiply x and y. | Output: 10 |

** | Exponent | x ** y | Raises x to the power of y. | Output: 25 |

/ | Division | x / y | Divide x by y and returns the result. | Output: 2.5 |

% | Modulus | x % y | Divide x by y and returns the remainder. | Output: 1 |

// | Floor Division | x // y | Divides x by y and rounds the result down to the nearest whole number. If one of the operands (x or y) is negative, the result is rounded towards negative infinity. | Output:2 -3 |

## Bitwise Operators in Python

These operators are used on numbers for comparison.

During the comparison, they treat numbers as strings of binary digits (bits), written in twos-complement binary, rather than a single numeric value.

To solve the examples in the table below, let x = 7 (0000 0111 in binary form) and y = 2 (0000 0010 in binary form).

Operator | Name | Example | Description | Code Example |
---|---|---|---|---|

& | Bitwise AND | x & y | Performs a "bitwise and". Sets each bit to 1 if the bit of both `x` and `y` is 1, otherwise, it's 0. | Output: 2 |

| | Bitwise OR | x | y | Performs a "bitwise or". Sets each bit to 1 if either or both bit of `x` and `y` is 1, otherwise, it's 0. | Output: 7 |

^ | Bitwise XOR | x ^ y | Performs a "bitwise exclusive or". Sets each bit to 1 if `x` is 0 and `y` is 1, or `x` is 1 and `y` is 0, otherwise, if both are the same, it's 0. | Output: 5 |

~ | Bitwise NOT | ~x | Performs a "bitwise not". Inverts the bit of `x` by switching each 1 for a 0 and each 0 for a 1. | Output: -3 |

>> | Bitwise Right Shift | x >> y | Performs a "bitwise right shift". Shifts the bits of `x` to the right by the number of bits specified in `y` . | Output: 1 |

<< | Bitwise Left Shift | x << y | Performs a "bitwise left shift". Shifts the bits of `x` to the right by the number of bits specified in `y` , and new bits on the right-hand-side are zeros. | Output: 28 |

## Assignment Operators in Python

To assign values to variables in python programming, you make use of assignment operators.

Operator | Name | Example | Description | Code Example |
---|---|---|---|---|

= | Assign | x = 7 | Assigns the value, 7, to the variable `x` . | Output: 7 |

+= | Add and Assign | x += 2 | Same as x = x + 2. Adds 2 to the variable `x` and assigns it back to the variable `x` . | Output: 9 |

-= | Subtract and Assign | x -= 2 | Same as x = x - 2. Subtracts 2 from the variable `x` and assigns it back to the variable `x` . | Output: 5 |

*= | Multiply and Assign | x *= 2 | Same as x = x * 2. Multiplies the variable `x` by 2 and assigns it back to the variable `x` . | Output: 14 |

**= | Exponent and Assign | x **= 2 | Same as x = x ** 2. Raises the variable `x` to the power of 2 and assigns it back to the variable `x` . | Output: 49 |

/= | Divide and Assign | x /= 2 | Same as x = x / 2. Divides the variable `x` by 2 and assigns it back to the variable `x` . | Output: 3.5 |

%= | Modulus and Assign | x %= 2 | Same as x = x % 2. Divides the variable `x` by 2, returns the remainder and assigns it back to the variable `x` . | Output: 1 |

//= | Floor Division and Assign | x //= 2 | Same as x = x // 2. Performs floor division on the variable `x` and 2, and assigns it back to the variable `x` . | Output: 3 |

&= | Bitwise AND and Assign | x &= 2 | Same as x = x & 2. Performs Bitwise AND to the variable `x` and 2, and assigns it back to the variable `x` . | Output: 2 |

|= | Bitwise OR and Assign | x |= 2 | Same as x = x | 2. Performs Bitwise OR to the variable `x` and 2, and assigns it back to the variable `x` . | Output: 7 |

^= | Bitwise Exclusive OR and Assign | x ^= 2 | Same as x = x ^ 2. Performs Bitwise Exclusive OR to the variable `x` and 2, and assigns it back to the variable `x` . | Output: 5 |

<<= | Binary Left Shift and Assign | x <<= 2 | Same as x = x << 2. Performs Bitwise Binary Left Shift to the variable `x` and 2, and assigns it back to the variable `x` . | Output: 28 |

>>= | Binary Right Shift and Assign | x >>= 2 | Same as x = x >> 2. Performs Bitwise Binary Right Shift to the variable `x` and 2, and assigns it back to the variable `x` . | Output: 1 |

## Comparison Operators in Python

Comparison operators are used to compare two or more values, statements, variables etc.

Operator | Name | Example | Description | Code Example |
---|---|---|---|---|

== | Equal | x == y | Checks if value of x equals y. | Output: False |

!= | Not Equal | x != y | Checks if value of x is not equal to y. | Output: True |

> | Greater Than | x > y | Checks if value of x is greater than y. | Output: True |

< | Less Than | x < y | Checks if value of x is less than y. | Output: False |

>= | Greater Than | x >= y | Checks if value of x is greater than or equal to y. | Output: True |

<= | Less Than | x <= y | Checks if value of x is less than or equal to y. | Output: False |

## Logical Operators in Python

These operators are used to combine two or more statements. They can be used to compare statements to make a decision based on an outcome.

Logical operators are the `and`

, `or`

, and `not`

operators.

Operator | Name | Example | Description | Code Example |
---|---|---|---|---|

and | Logical AND | x == a and x == b | Returns `true` if `x` has same value as `a` and `b` . Returns `false` if `x` 's value differs from `a` , `b` , or both. | Output: False |

or | Logical OR | x == a or x == b | Returns `true` if `x` has same value as `a` or `b` or both. Returns `false` if `x` 's value differs from `a` and `b` . | Output: True |

not | Logical NOT | not(x == a) | Checks if `x` has same value as `a` , and returns the opposite of the result. That is, it returns `true` if `false` and returns `false` if `true` . | Output: True |

## Special Operators in Python

There some special operators used particularly in Python programming language that is worth mentioning.

These operators are the identity operator and the membership operator./p>

### Identity Operators in Python

Identity operators are used to compare two values or variables by checking if they are located on the same part of the memory.

Two variables or values that are similar does not mean that they are identical.

Unlike the comparison operators which check if two values are **similar** using their form, the identity operator checks if they are **identical** using their memory location.

Identity operators are the `is`

and `is not`

operators.

Operator | Name | Example | Description | Code Example |
---|---|---|---|---|

is | Is same object | x is y | Returns `true` if `x` has the same memory location as `y` . Otherwise, it returns false. | Output:False True |

is not | Is Not same object | x is not y | Returns `true` if `x` is not located in the same place in memory as `y` . Otherwise, it returns false. | Output:True False |

### Membership Operators in Python

Membership operators are used to check if a value or variable is found in a sequence such as `string`

, `list`

, `tuple`

, `set`

, and `dictionary`

.

In the case of a `dictionary`

, Python allows us to only test the key and not the value.

Identity operators are the `in`

and `not in`

operators.

Operator | Name | Example | Description | Code Example |
---|---|---|---|---|

in | In object | x in y | Returns `true` if `x` can be found in `y` . Otherwise, it returns false. | Output: True |

not in | Not In object | x not in y | Returns `true` if `x` can not be found in `y` . Otherwise, it returns false. | Output: False |

## What is the Precedence of Operators in Python?

Precedence of operators is the sequence in which operators will be processed and evaluated when used in a mathematical expression.

It is similar to the "BODMAS" and "PEMDAS" used in mathematics.

The operators are evaluated from the highest to the lowest. The operator(s) with the highest precedence evaluated first followed by those with the lowest.

For example, x = 2 + 3 * 2; here, `x`

is assigned 8, not 10 because the operator `*`

has higher precedence than `+`

, so it first multiplies 3 * 2 and then is added to 2.

The table below shows the precedence of operators from highest to lowest. In python programming, some operators are ranked the same.

Rank | Operator | Name |
---|---|---|

1 | ** | Exponent |

2 | ~ + - | Complement, unary plus and minus (method names for the last two are [email protected] and [email protected]) |

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 | is is not | Comparison operators |

12 | in not in | Membership operators |

13 | not or and | Logical operators |

## Wrap Off

Operators in programming are used to perform different operations on variables and values. These operations can be arithmetic, comparison, assignment, logical, etc.

In Python programming language, operators are grouped into different categories: Arithmetic operators, Bitwise operators, Assignment operators, Comparison operators, Logical operators, and Special operators like Identity operators and Membership operators.

If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.