Skip to content

Python Operators




Python – Operators


As we read earlier about Python Typecasting Now we will study about Operators in Python. Python Operators have a lot of space in Python, If we are creating a program related to mathematics, then it is a very big role of operators and Calculator is a very good example for operators. so lets start.




What is operator in Python ?

Operator are used to perform operations on variables and values is called as Operator. 

Consider the expression  3 + 4 = 7. here, 3 and 4 are called operands and + is called operator.




What are the types of operators in python ?

Python programming language divides the operators in the following groups :

Python Operators

  • Arithmetic Operators

  • Assignment Operators

  • Relational or Compare Operators

  • Logical Operators

  • Bitwise Operators

  • Membership Operators

  • Identity Operators


 

  • Arithmetic Operator

    Arithmetic Operator are used with numeric values to perform common mathematical operations like Addition, Subtraction, Multiplication, Division. It works only on “Values”.

     

OperatorDescriptionExample
Addition ( + )To adds two operands.x + y = 50
Subtraction (-)To subtracts two operands.x – y = -10
Multiplication ( * )To multiplies two operands.x * y = 100
Division ( / )To divides the first operand by the second operand.y / x = 2
Modulus ( % )To divides first operand by the second operand and returns remainder.y % x = 0
Exponent (**)To performs exponential ( to the power) calculation on operators.y ** x = 10 to the power of 20
Double division
or

Floor division ( // )
To divides the first operand by the second operand.5//2 and 5.0 // 2.0, -13 // 2 and -13.0 // 2.0





 

  • Assignment Operator

     

     

    Assignment Operators are the operators that is used to assign values to variable. It works only on “Reference”.

Operator DescriptionExample
= ( Equal )To assign values of right side of expression to left side operand.z = x + y assigns value of x + y into z
+= ( Add AND )It adds right side operand to the left side operand and then assign result to the left operand.z += x OR z = z + x
-= ( Subtract AND )It subtracts right side operand from the left side operand and then assign result to the left operand.z -= x OR z = z – x
*= ( Multiply AND )It multiplies right side operand with the left side operand and then assign result to the left operand.z *= x OR z = z * x
/= ( Divide AND )It divides left side operand with the right side operand and then assign result to the left operand.z /= x OR z = z / x
%= ( Modulus AND )It takes modulus using two operands and then assign result to the left operand.z %= x OR z = z % x
**= ( Exponent AND )It performs exponential ( power ) calculation on operator and assign value to the left operand.z **= x OR z = z ** x
//= ( Floor Division )It performs floor division on operators and assign value to the left hand.z //= x OR
z = z // x

This Operator is also known as Short Hand Operator.






 

  • Relational Operator 

     

    Relational or Compare Operator are used to compare two values. It works only on “Values”.

OperatorDescriptionExample
==If both the operands are equal, then the condition becomes true.( x == y )
!=If operands are not equal, then the condition becomes true.( x != y )
>If the left operand is greater than the right operand, then the condition becomes true.( x > y )
<If the left operand is less than the right operand, then the condition becomes true.( x < y )
>=If the left operand is greater than or equal to the right operand, then the condition becomes true.( x >= y )
<=If the left operand is less than or equal to the right operand, then the condition becomes true.( x <= y )

This Operator are always return’s Boolean values ( True or False ).

                                              0     →     False
                                              1     →     True 

Hence this operator are always used with conditional statement.






 

  • Logical Operator

     

    Logical operators are used to combine conditional statements is called as logical operator.

     

OperatorDescriptionExample
and
Logical AND
If both the operands are true then condition becomes true.( x and y ) is true
or
Logical OR
If any of the two operands are non-zero then condition becomes true.( x and y ) is true
not
Logical NOT
Used to reserve the logical state of its operand.Not ( x and y ) is false

  •  This operator works with Boolean operands only.
  • This operators are always return Boolean or Boolean like values.

ABA and BA or B
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
FALSETRUEFALSETRUE
FALSEFALSEFALSEFALSE





 

  • Bitwise Operator 

     

    Bitwise operator are used to compare ( binary ) number. It works only on “Values”.

OperatorDescriptionExample
&Binary
AND
( x & y ) ( means 0000 1100 )
|Binary
OR
( x | y ) = 61 ( means 0011 1101 )
^Binary
XOR
( x ^ y ) = 49 ( means 0011 0001 )
~Binary
Ones complement
( ~x ) = -61 ( means 1100 0011 in 2’s complement form due to a signed binary number)
<<Binary
Left Shift
x << 2 = 240 ( means 1111 0000 )
>>Binary
Right Shift
x >> 2 = 15 ( means 0000 1111 )





 

  • Membership Operator

     

    Membership operator test for membership in a sequence ; such as strings, lists or tuples. It works only on “Values”.

OperatorDescriptionExample
inIf the value is found in the sequence, then the conditions becomes True.x in y, if x is a member of sequence of y.
not inIf the value is not found in the sequence, then the conditions becomes True.x not in y, if x is not a member of sequence y.

This operator works on collection.






 

  • Identity Operator


    Compare the memory locations of two objects. There are two identity operators. It works only on “Reference”.

OperatorDescriptionExample
isIf the operands are identical, then the condition becomes True.x is y, if id (x) equals id (y)
is notIf the operands are not identical, then the condition becomes True.x is not y, if id (x) is not equal to id (y)


>