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. i.e. Addition, Subtraction, Multiplication, Division etc.

Operator Description Syntax Example(x=5,y=2)
+  (Addition) Add two Operands (x+y ) (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:

Output

Assignment operators: They are used to assign values of right expression to the left operand .

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:

Output:

Comparison operators : Comparison operators compare the values of the two operands and return a True or False i.e Boolean Value

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:                   

Output:     

Bitwise Operators: Performs bit by bit operations on two operands.

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:

Output:

Logical Operators: Used  in the expression evaluation to make a decision

They are three logical operators

  • Logical AND
  • Logical OR 
  • Logical NOT
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:

Output

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

Output

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:

Ouput

Author    : Teja

LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201

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 (No Ratings Yet)
Loading...

Add Comment