# What are the different Operators available in python?

First of all let’s try to understand, What is meant by Operators? So The Operators in python are the reserved symbols that represents an action. Each operator symbol in python has its own predefined action or operation to perform on the input entities. These input entities are called as operands.

Let’s see one expression “3 + 5”. In this expression number 3 and 5 are the operands and symbol “+” is an operator. In python “+” symbol is supposed to perform “Addition” operation on the operands. Therefore after execution of this expression it gives us the addition of operands (Result: 8) as a result.

Likewise all other computer programming languages, In python as well Operator symbols are adapted from mathematics and logic. In python, There are many types of operators available for different actions. For the learning purpose we have classified these operators on the basis of operations they perform on operands. Now let’s see each type of operators one by one.

## 1. Arithmetic operators in python:

Arithmetic operators in python performs mathematical operations on operands. Basic arithmetic operations are Addition, Subtraction, multiplication etc.

Here table given below explains about how we can perform basic arithmetic operations in python.

SymbolSyntaxOperator NameDescription
+x + yAdditionAdds two operands. e.g (3 + 2 = 5)
-x - ySubtractionSubtracts the right hand operator from Left hand operator. e.g (3 - 2 = 1)
*x * yMultiplicationMultiply the two operands. e.g (3 * 2 = 6)
/x / yFloat DivisionDivides the left operand by right. Result is always a float number. e.g (3 / 2 = 1.5)
//x // yFloor DivisionDivides the left operand by right. Result is always a Integer number. e.g (3 / 2 = 1)
**x ** yExponentLeft operator is raised to power of right. (3 ** 2 = 9)
%x % yModulusResult is remainder of division operation. e.g (3 % 2 = 1 Remainder of 3 / 2)

## 2. Comparison operators in python:

As the name suggests, Comparison operator performs comparison operation on two operands. It compares the operand on left side with the operand on right. If the compared condition is met then it returns the result “True” otherwise it returns “False”. Comparison operator always return the result in Boolean format. i.e True or False.

See the table below for different comparison operators in python.

SymbolSyntaxOperator NameDescription
.==x == yEqual ToCheck If "x is equal to y" e.g (if 3 == 2 result: False)
!=x != yNot Equal ToCheck If "x is NOT equal to y" e.g (if 3 != 2 result: True)
<x < yLess ThanCheck If "x is less than y" e.g (if 3 < 2 result: False)
>x > yGreater ThanCheck If "x is greater than y" e.g (if 3 > 2 result: True)
<=x <= yLess than or Equal ToCheck If "x is Less than or equal to y" e.g (if 3 <= 2 result: False)
>=x >= yGreater than or Equal ToCheck If "x is Greater than or equal to y" e.g (if 3 >= 2 result: True)

## 3. Logical operators in python:

Logical operators tests multiple conditions. Each operand is considered as a condition which results in Boolean format i.e True or False. Final result is decided depending upon result of each operand condition. E.g There is a logical conditions like if value of both the operands is True then return the result “False”.

See the table below to understand different Logical operators in python.

• x = True
• y= False
SymbolSyntax Description
ANDx and yResult is True only if the value of both the operand is True. e.g x and y is False.
ORx or yResult is True if any one of the operand value is True. e.g x or y is True.
NOTnot yIt compliments the operand i.e if operand value is True then it will result False. e.g not y is True.

## 4. Assignment and Compound operators in python:

Assignment operators in python are used to assign a value to the variable. Value present on the left side of operator is assigned to the variable on right side of operator.

Let’s see one expression “x = 8”. In this expression ‘Equal to’ symbol is an assignment operator and ‘8’ is the value. Therefore at the time we execute this expression, Value ‘8’ will be assigned to the variable ‘x’.

If you use any other operator in conjunction with assignment operator (Equal To sign), It changes the value that is going to get assigned to the variable. These type of operators are known as compound operators in python.

In expression “x += 8 ” we have used compound operator “+=”. This expression is equivalent to “x = x + 8”. Compound operators in python are very helpful and it is broadly used by the developers. See the table below for different compound operators in python and their equivalent statements.

Compound OperatorSyntaxEquivatent to
=x = 8x = 8
+=x += 8x = x + 8
-=x -= 8x = x - 8
*=x *= 8x = x * 8
/=x /= 8x = x / 8
%=x %= 8x = x % 8
//=x //= 8x = x // 8
**=x **= 8x = x ** 8
&=x &= 8x = x & 8
|=x |= 8x = x | 8
^=x ^= 8x = x ^ 8
>>=x >>= 8x = x >> 8
<<=x <<= 8x = x << 8

## 5. Membership operators in python:

Membership operators are used to check whether the input value is present in the sequence or not. These operators can only be used with sequence data type. Sequence data type includes “String, List, Tuple, Set”. They can be used with “Dictionary” data type as well, But you can only check the keys and not the values.

Suppose we are having one string “My name is John”. Then using membership operator we can check if word “John” is present in this string or not.

Let’s see an example, We are having a variable “x” of list data type.

•  x = [2, 4, 6, 8]
OperatorExampleResult
in'2' in xTRUE
Not in'Hello' not in xTRUE

In above example both the statements i.e “2 in x” and “Hello not in x” are correct hence result is “True”.

## Precedence of Operators in Python:

Until now we have learned about different operators in python. Let’s move on further and try to understand, What is precedence of operators in python?

When multiple operators and operands are present in one expression. There is a specific sequence to evaluate an expression. It guides the interpreter for evaluating the expression. This sequence of operation is known as “Precedence of Operator”.

Suppose we are having an expression 8 – 3 * 2. Since this expression has two operators, Result of this expression certainly depends upon the order in which we evaluate it. Like if we perform subtraction operation first and then multiplication (8 – 3) * 2 the the result will be 10. Whereas if we change the order and evaluate expression like 8 – (3 * 2) the result will be 2. To avoid these problems we have order of sequence which tells us the order in which the expressions should be evaluated.

Table here explains the precedence of operators in python. Operations will be evaluated in descending orders.

OperatorsMeaning
()Parentheses
**Exponent
+x, -x, ~xUnary plus, Unary minus, Bitwise NOT
*, /, //, %Multiplication, Division, Floor division, Modulus
+, -Addition, Subtraction
!=, ==, >, >=, <, <=, is, is not, in, not inComparisions, Identity, Membership operators
notLogical NOT
andLogical AND
orLogical OR

## Continue reading..

Also you can get more help on python forum.  Click Here