# 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.

## 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:

Output:

a + b = 16
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:

Output:

a > b = True
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:

Output:

Result after a += b = 10
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:

Output:

a and b is False
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:

Output:

a & b = 1
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:

Output:

5 in a True
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:

Output:

True
False
Here since a1 and b1 are integers, they are identical, so a1 is b1 returns true but with the list, it is not the case as the list is stored in separate memory space.

## 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.

SHARE Python Operators

#### You may also like... 