- Home
- python Tutorial
- operators

# Operators in Python

Operators are used to make certain manipulations on operands

Let us look at the type of the operators in python

## Python Arithmetic Operators

These operators perform necessary arithmetic operations on the operands. These computations include all the operations that are defined in the table below.

Let x and y be two operands

Operator |
Operation | Expression |
Description |

+ |
Addition | x+y |
Returns sum of a & b |

- |
Subtraction | x-y |
Returns difference of a&b |

* |
Multiplication | x*y |
Returns product of a&b |

/ |
Division | x/y |
Returns quotient of a&b |

% |
Modulus | x%y |
Returns remainder of a&b |

\*\* |
Exponent | x\*\*y |
Returns x to the power y |

// |
Floor Division | x-- |
Returns the floored value of the quotient |

## Python Comparison Operators

Comparison operators unlike arithmetic operators only compare two values and return true or false as the result.

Operator |
Operation | Expression | x = y | x>y |
x<y |

== |
Equal to | x == y | True | False |
False |

!= |
Not Equal to | x != y | False | True |
True |

<= |
Less than equal to | x <= y | True | False |
True |

>= |
Greater than equal to | x >= y | True | True |
False |

< |
Less than | x < y | False | False |
True |

> |
Greater than | x > y | False | True |
False |

## Python Assignment Operators

Assignment operators assign to operands on the left side of the operator after the computation or comparison of the operands on the right hand side of the assignment operator.

Let us assume the values of three variables x=5, y=3, and z=4.

Operator | Description |
Resulting Value |
---|---|---|

= |
x=y+z |
x=7 |

+= |
x += y |
x=8 |

-= |
x -= y |
x=2 |

/= |
x /= y |
x=1.66 |

%= |
x%=y |
x=2 |

*= |
x*=y |
x=15 |

\*\*= |
x \*\*= y |
x= 125 |

//= |
x //= y |
x=1 |

## Python Bitwise Operators

Bitwise operators perform operations on a bit level. After converting the value into binary form, the bitwise operators perform operations on corresponding bits of the operands.

Operator |
Operation | Expression |
Description |

& |
Bitwise AND | a&b |
Returns 1 when both corresponding bits are 1 |

| |
Bitwise OR | a|b |
Returns 1 when either corresponding bits are 1 |

^ |
Bitwise XOR | a^b |
Returns 0 only when both bits are 1 |

~ |
Compliment | ~a |
Flips all the bits in the binary form of the operand |

<< |
Left Shift | a<<b |
shifts all the bits to the left by one position* |

>> |
Right Shift | a>>b |
shifts all the bits to the right by one position* |

## Python Logical Operators

Membership Operators

Membership operators in python test for membership in sequence like strings, lists or tuples.

Let x be be a variable and y be a tuple, list or a string

Operator | Expression |
Description |
---|---|---|

in |
x in y |
Returns 1 if the value of x is a part of y |

not in |
x not in y |
Returns 1 if the value of x is not a part of y |

## Python Identity Operators

Identity operators compare the memory locations of two variables

Operator | Expression | Description |
---|---|---|

is |
x is y |
Returns true if id(x) is equal to id(y) |

is not |
x is not y |
Returns true if id(x) is not equal to id(y) |

## Python Operators Precedence

The operator precedence in python set in the descending order

Operator

\*\*

~, +, -

*, /, %, //

+, -

>>, <<

&

^, |

<=, <, >, >=

== !=

=, %=, /=, //=, -=, +=, *=, \*\* =

is, is not

in, not in

not, or, and