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

Operators In Python

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("Addition=",(a+b))
print("Substraction=",(a-b))
print("Multiplication=",(a*b))
print("Division=",(a/b))
print("Exponent=",(a**b))
print("Floor division=",(a//b))

Output:

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

Example:

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

Output:

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

OperatorDescriptionExample
= Simple assignment operator. Assigns values from right side operands to left side operandC = A + B will assign the value of A + B to C
+=Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A
*=Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. C *= A is equivalent to C = C * A
/=Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.C %= A is equivalent to C = C % A
**= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operandc **= a is equivalent to c = c ** a
//= Floor Division It performs floor division on operators and assign value to the left operandc //= a is equivalent to c = c // a

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)

Output:

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")

Output:

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")

Output:

false
true
Next Post Previous Post
No Comment
Add Comment
comment url