OPERATORS IN PYTHON

Share via:

OPERATORS IN PYTHON

Operator: It is defined as a symbol which is responsible for a particular operation between two operands.
Python supports different types of operators:
1. Arithmetic Operators
2. Assignment Operators
3. Logical Operators
4. Comparison Operators
5. Bitwise Operators
6. Identity Operators
7. Membership Operators

 

Arithmetic Operators: They are used to perform Mathematical operations between two operands, Addition, Subtraction, Multiplication, Division etc.
Use Case: Used to perform basic arithmetic operators.

 

    Operator
      Description
   Syntax
   Example(x=5,y=2)
 +  (Addition)
Add two Operands
    ( x+y )
5+2=10
–  (Subtraction)
Subtract two Operands
    ( x-y )
5-2=3
*  (Multiplication)
multiply two Operands
    ( x*y )
5*2=10
/  (Division)
Divide first operand by second
    ( x/y )
5/2=2.5
**  (Power)
First Operand raised to power of second
    (x**y )
5**2=25
%  (Modulus)
Remainder when first Operand is Divide by second
    ( x%y )
5%2=1
// (floor Division)
Divide first operand by second
    (  X//y )
5//2=2
Example:

 

ASSIGNMENT OPERATORS 
Assignment operators: They are used to assign values of right expression to the left operand.
Use case: Used to manipulate variables, most commonly used in loops.

 

Operator
Description
Syntax (take s and t)
(=)
Assign value of right side of expression to left side operand.
s=t
(+=)
Add right operand from left operand and then assign to left operand.
s+=t (or)  s=s+t
(-=)
Subtract right operand from left operand and then assign to left operand.
s-=t  (or)  s=s-t
(*=)
Multiply right operand from left operand and then assign to left operand.
s*=t  (or)  s=s*t
(/=)
Divide left operand with right operand and then assign to left operand.
s/=t  (or)   s=s/t
(%=)
Takes modulus using left and right operands and assign the result to left operand.
s%=t  (or)  s=s%t
(**=)
Calculate exponent value and assign value to left operand.
s**=t  (or)  s=s**t
(//=)
Divide left operand with right operand and then assign the value(floor) to left operand.
s//=t  (or)   s=s//t
Example:

 

Comparison operators
Comparison operators compare the values of the two operands and return a True or False i.e. Boolean Value
Use case: To evaluate conditions and make logical decisions.

 

Operator
Description
Syntax
(==) Equal to
True if both operands are equal
S==T
(!=) Not Equal to
True if operands are not equal
S!=T
( >) Greater than
True if the left operand is greater than the right
S>T
( <) Less than
True if the left operand is Less than the right
S<T
( >=) Greater than or equal to
True if the left operand is greater than or equal to the right
S>=T
( <=) Less than or equal to
True if the left operand is less than or equal to the right
S<=T
Example:                                                                                                              

 

BITWISE OPERATORS
Bitwise Operators: performs bit by bit operations on two operands.
Use case: Mostly used in low level programming like hardware manipulation and optimizing the algorithms.

 

Operator
Description
Syntax(X, Y)
 &  (BITWISE AND)
Resulting bit will be 1 only if both the bits are 1 else 0
X&Y
|  (BITWISE OR)
Resulting bit will be 1 if either of the bits is 1
X|Y
~  (BITWISE NOT)
Resulting will be compliment of the given operand
~X
^  (BITWISE XOR)
Resulting will be 1 when both the bits are different otherwise 0
X^Y
>>  (RIGHT SHIFT)
Shift the left operand bits towards the right for right operand number of times
X>>Y
<<  (LEFT SHIFT)
Shift the left operand bits towards the left for the right operand number of times
X<<Y
Example:

 

LOGICAL OPERATORS
Logical Operators: Used in the expression evaluation to make a decision.
They are three logical operators Logical AND, Logical OR & Logical NOT
Use case: Used to combine multiple conditions in the decision-making process and filtering the data.

 

Operator
Description
Syntax (S,T are operands)
AND
True if both the operands are False
   S and T
OR
True when either of the operands is True
   S or T
NOT
True if the operand is False
  not S
Example:

 

IDENTITY OPERATORS
They are used to check if two values are located in the same part of memory, or both belong to the same class or not.
There are two identical operators:
▪️ is
▪️ is not
Use case: Used to compare memory locations of the object.

 

Operator
Description of operator
is
True if the two operands share same memory location.
Is not
True if the two operands share different memory location.
We can verify it using the in-built function of python called id ().
Example:

 

MEMBERSHIP OPERATORS
Membership operators: It is used to check whether a value is present in the sequence or not.
They are two membership Operators:
▪️ in
▪️ not in
Example:

 

 

Author    : Venkat Vinod Kumar Siram 
LinkedIn : https://www.linkedin.com/in/vinodsiram/
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates.
KTExperts is always active on social media platforms.
Facebook  : https://www.facebook.com/ktexperts
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts
Instagram  : https://www.instagram.com/knowledgesharingplatform
Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment