Python Operators

Operator is a symbol that tells the compiler to perform specific mathematical or logical manipulation. Python provides a variety of operators, they are,

Python Operators
Python Operators

Operator is an symbol that tells the compiler to perform specific mathematical or logical manipulation. Python provides a various of operators, they are,

  • Arithmetic operators
  • Relational Operators
  • Assignment Operators
  • Logical operators
  • Identity operators
  • Bitwise Operators
  • Membership Operators

Arithmetic operators

Arithmetical operator is used to perform mathematical operations.

Operator Description
+ + operator is used to add two values.
– operator is used to subtract two values.
* * operator is used to multiply two values.
/ / operator returns quotient after dividing
1st operand by 2nd operand.
% % operator returns remainder after dividing
1st operand by 2nd operand.
** ** operator calculates the 1st operand power
by the second operand.
// // operator returns the quotient(answer or result of division)
in which the digits after the decimal point are removed.

Relational Operators

Relational operator is used to compare two values.

Operator Description
== If both the condition are true then the
condition will become true.
< This operator will check whether the left operand
is less than the right operand.
> This operator will check whether the right operand
is greater than the left operand.
<= This operand will check whether the left operand
is less than or equal to the right operand.
>= This operand will check whether the right operand
is greater than or equal to the left operand.
!= If both the operand are not equal then the
condition will become true.

Assignment Operators

Assignment operator is used to assign the values.

Operator Description
= This operator assign the value from the right to left.
+= This operand adds the right operand to the left and
assign the value to left operand.
-= This operand subtract the right operand to the left and
assign the value to left operand.
*= This operand multiply the right operand to the left and
assign the value to left operand.
/= This operand divide the right operand to the left and
assign the value to left operand.
%= This operand takes the modulus from both the operand
and assign values to left operand.

Logical operators

Logical operator is used to perform logical operations.

Operator Description
AND If both the expression are true then the
condition will become rue.
OR If any one of the expression is true then the
condition will become true.
NOT If the expression result is false this operand
will convert the result into true.

Identity operators

Identity operator is used to compare memory location of two objects.

Operator Description
is If both side of the variable point to the same
object then it will return true. Otherwise false.
is not If both side of the variable point to the same
object then it will return false. Otherwise True.

Membership Operators

Membership operator is used to check whether the membership value present in a list, tuple, or dictionary.

Operator Description
in If the specific value present in the object,
then it will return true. Otherwise false.
not in If the specific value is not present in the object,
then it will return true. Otherwise false.

Bitwise Operators

Bitwise operator is used to compare binary values.

Operator Description
$(AND) Bitwise AND operator returns 1 if both the
bits are 1, otherwise 0.
|(OR) Bitwise OR operator returns 1 if any of the bit is 1.
It both are true it will return 0.
^(XOR) Bitwise XOR operator returns 1 if one of the
bit is 0 and other bit is 1.
<<(Left Shift) The left operands value is moved left by the
given number of times in the right operand.
>>(Right Shift) The left operands value is moved right by the
given number of times in the right operand.