# Operators in python | What types of operators in python and how to use it | Python Operators

## Operators In Python

An operator is a symbol that operates on a value or a variable. For example + is an operator to perform addition. Python has a wide range of operators to perform variously. Python language is rich in built-in operators and provides the following types of operators.

1. Arithmetic Operators
2. Relational Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators

## Arithmetic Operators

An arithmetic operator performs mathematical operations such as addition, subtraction, and multiplication on numerical values Assume variable A holds 10 and variable B holds 5 then.

Operator Description Example
+ Adds two operands. A + B = 15
- Subtracts the second operand from the first. A − B = 5
* Multiplies both operands. A * B = 50
/ Modulus Operator and the remainder of after an integer division. A / B =2
% Modulus Operator and the remainder of after an integer division. A % B = 0
**(Exponent) It is an exponent operator represented as it calculates the first operand power to the second operand. A**B=10**5=100000
//(Floor division) It gives the floor value of the quotient produced by dividing the two operands. A//B=10//5=2

#### Example:

a=10
b=5
print("Substraction=",(a-b))
print("Multiplication=",(a*b))
print("Division=",(a/b))
print("Exponent=",(a**b))
print("Floor division=",(a//b))

Substraction5
Multiplication50
Division2.0
Exponent100000
Floor division2

## Relational Operators

A relational operator checks the relationship between two operands. If the relation is true, it returns 1 or true, if the relation is false, it returns value 0 or false Assume variable A holds 10 and variable B holds 5 then.

OperatorDescriptionExample
==Checks if the values of two operands are equal or not. If yes, then the condition becomes true.(A == B) is not true.
!=Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.(A != B) is true
>Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true. (A > B) is true.
<Checks if the value of the left operand is less than the value of right operand. If yes, then the condition becomes true.(A < B) is not true.
>=Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.(A >= B) is true.
<=Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.(A <= B) is not true.

a=10
b=5
print(a==b)
print(a!=b)
print(a>b)
print(a<b)
print(a>=b)
print(a<=b)

False
True
True
False
True
False

## Assignment Operators

An assignment operator is used for assigning a value to a variable. The most common assignment operator is =.

#### Example:

a = 10
b = 5
c = 0
c = a + b
print ("c=a+b="c)
c += a
print ("c=c+a="c)
c *= a
print ("c=c*a="c)
c /= a
print ("c=c/a="c)
c %= a
print ("c=c%a="c)
c=2
c **= a
print ("c=c**a="c)
c //= a
print ("c=c//a="c)

c=a+b= 15
c=c+a= 25
c=c*a= 250
c=c/a= 25.0
c=c%a= 5.0
c=c**a= 1024
c=c//a= 102

## Logical Operators

The logical operators are used primarily in the expression evaluation to make a decision. Python supports the following logical operators.

OperatorDescriptionExample
and Logical ANDIf both the operands are true then the condition becomes true. (a and b) is true.
or Logical ORIf any of the two operands are non-zero then condition becomes true.(a or b) is true.
not Logical NOTUsed to reverse the logical state of its operand.Not(a and b) is false.

## Bitwise Operators

The bitwise operators perform bit by bit operation on the values of the two operands.

OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands. (A & B)
Binary OR Operator copies a bit if it exists in either operand.(A | B)
^Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B)
~Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.(~A )
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2

## Membership Operators

Python membership operators are used to check the membership of value inside a data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false.

OperatorDescription
in The result of this operation becomes True if it finds a value in a specified sequence & False otherwise.
not inresult of this operation becomes True if it doesn't find a value in a specified sequence & False otherwise.

#### Example:

a = 10
b = 5
l = [12345 ];
if ( a in l ):
print ("true")
else:
print ("false")

if ( a not in l ):
print ("true")
else:
print ("false")

if ( b in l ):
print ("true")
else:
print ("false")

false
true
true

## Identity Operators

Identity operators compare the memory locations of two objects.

OperatorDescription
isIt is evaluated to be true if the reference present at both sides point to the same object.
is notIt is evaluated to be true if the reference present at both side do not point to the same object.

#### Example:

a = 10
b = 5
if ( a is b ):
print ("true")
else:
print ("false")
if ( a is not b ):
print ("true")
else:
print ("false")

false
true
No Comment