# Python Operators

Operators in Python are the symbols that perform specific function between operands. An operator is one of the most important features in any programming language as they allow an easy way of performing a basic operation like addition, subtraction, and many other operations. In Python there are seven different operators, let us explore them in this tutorial.

Contents

- 1 What are the Arithmetic Operators in Python?
- 2 What are the Comparison Operators in Python?
- 3 What are the Assignment Operators in Python?
- 4 What are the Logical Operators in Python?
- 5 What are the Bitwise Operators in Python?
- 6 What are the Membership Operators in Python?
- 7 What are the Identity Operators in Python?
- 8 What is the Precedence of Operators in Python?
- 9 Associativity of Python Operators

## What are the Arithmetic Operators in Python?

Arithmetic operators perform mathematical operations like addition, subtraction, multiplication, division, exponent.

Operator | Description |

+ | ( Addition ) Performs addition between two operands. |

– | (Subtraction) Performs subtraction between two operands. |

* | (Multiplication) Performs multiplication between two operands. |

/ | (Division) Performs division between two operands. |

% | (Modulus) The remainder of the division of left operand by the right |

** | (Exponent) Left operand raised to the power or right |

// | (Floor Division) division result where digits after the decimal point are removed. |

Example:

1 2 3 4 5 6 7 8 9 | a = 10 b = 6 print("a + b =", a + b) print("a - b =", a - b) print("a * b =", a * b) print("a / b =", a / b) print("a % b =", a % b) print("a ** b =", a ** b) print("a // b =", a // b) |

Output:

a – b = 4

a * b = 60

a / b = 1.6666666666666667

a % b = 4

a ** b = 1000000

a // b = 1

## What are the Comparison Operators in Python?

Comparison operator performs a comparison between operands and returns either **True** or **False**.

Operator | Description |

> | ( Greater Than ) Returns true if the left operand is greater than the right. |

< | ( Less Than ) Returns true if the left operand is less than the right. |

== | (Equal to) Return true if both operands are equal. |

!= | (Not Equal to) Returns true if the left and right operands are not equal. |

>= | (Greater than or equal to) Returns true if the left operand is greater than or equal to right. |

<= | (Less than or equal to) Returns true if the left operand is less than or equal to right. |

Example:

1 2 3 4 5 6 7 8 | a = 10 b = 6 print("a > b =", a > b) print("a < b =", a < b) print("a == b =", a == b) print("a != b =", a != b) print("a >= b =", a >= b) print("a <= b =", a <= b) |

Output:

a < b = False

a == b = False

a != b = True

a >= b = True

a <= b = False

## What are the Assignment Operators in Python?

Assignment operator assigns values to the variables in Python.

Operator | Description |

= | Assigns the value of the right expression to the right |

+= | Adds the value of the left operand by value of right operand and assigns back to left operand |

-= | Subtracts the left operand with the value of right operand and assigns back to left operand |

*= | Multiplies the value of the left operand by value of right operand and assigns back to left operand |

/= | Divides the value of the left operand by value of right operand and assigns back to left operand |

%= | Assigns the remainder of the division operation of the left operand with right operand assigns back to left operand |

//= | Assigns the floor division operation of the left operand with right operand assigns back to left operand |

**= | Assigns the exponent operation of the left operand with right operand assigns back to left operand |

Example:

1 2 3 4 5 6 7 | a = 10 b = 6 a += b a -= b print("Result after a += b =", a) print("Result after a -= b =", a) |

Output:

Result after a -= b = 10

## What are the Logical Operators in Python?

The use of a logical operator in Python is to make a decision. They are used with control statements.

Operator | Description |

and | Returns True if both the operands are True |

or | Returns True if any of the operands is True |

not | Returns True if the operand is false |

Example:

1 2 3 4 5 | a = True b = False print('a and b is', a and b) print('a or b is', a or b) print('not a is', not a) |

Output:

a or b is True

not a is False

## What are the Bitwise Operators in Python?

Bitwise operators perform bit by bit operation on the values assigned to two operands.

Operator | Description |

& | ( Binary AND ) Copies 1 to the same place if two operands are 1 and 0 if any one is 0. |

| | ( Binary OR ) Copies 1 to the same place if either operand is 1 and 0 if any both are 0. |

^ | (Binary XOR) The resulting bit will be 1 if both the bits are different otherwise the resulting bit will be 0. |

~ | (Negation) Results in the negation of each bit |

<< | (Left Shift) The left operand value is moved left by the number of bits present in the right operand. |

>> | (Right Shift) The left operand is moved right by the number of bits present in the right operand. |

Example:

1 2 3 4 5 6 7 8 | a = 9 # 1001 b = 5 # 0101 # output 1 i.e. binary 0001 print('a & b = ', a & b) # output 13 i.e. binary 1011 print('a | b = ', a | b) |

Output:

a | b = 13

## What are the Membership Operators in Python?

There are two membership operators in Python viz. in and not in, which check the presence of the given variable/value in the sequence ( string, list, tuple, set or dictionary ).

Operator | Description |

in | Returns True if given value or variable is found in the specified sequence |

not in | Returns True if given value or variable is not found in the specified sequence |

Example:

1 2 3 4 | a = [1, 3, 5, 7] # a is a list of numbers print("5 in a", 5 in a) print("4 in a", 4 in a) print("10 in a", 10 not in a) |

Output:

4 in a False

10 in a True

## What are the Identity Operators in Python?

There are two identity operators in Python viz. is and is not, that checks if the two values/variables share the same memory location.

Operator | Description |

is | Return True if the reference present at both sides point to the same object. |

is not | Return True if the reference present at both sides do not point to the same object. |

Example:

1 2 3 4 5 6 | a1 = 5 b1 = 5 a2 = [1, 2, 3] b2 = [1, 2, 3] print(a1 is b1) print(a2 is b2) |

Output:

False

## What is the Precedence of Operators in Python?

The order in which the operator is evaluated first plays important role in any programming language. Python has the following precedence of operators:-

Operator | Description |

() | Parenthesis has the highest precedence |

** | The exponent operator is given priority over all the others used in the expression. |

+x, -x, ~x | Unary plus, Unary minus, Bitwise NOT |

* / % // | The multiplication, divide, modules, reminder, and floor division. |

+ – | Binary plus and minus |

>> << | Left shift and right shift |

& | Binary and. |

^ | | Binary xor and or |

<= < > >= | Comparison operators (less than, less than equal to, greater then, greater then equal to). |

<> == != | Equality operators. |

= %= /= //= -= += *= **= | Assignment operators |

is / is not | Identity operators |

in / not in | Membership operators |

not or and | Logical operators |

## Associativity of Python Operators

When two operators have the same precedence, the associativity helps to determine which is the order of operations. Almost all the operators have left-to-right associativity in Python. Exponent operator ** has right-to-left associativity in Python. Assignment operators and comparison operators do not have associativity in Python.

## Recent Comments