Skip to content

Python Operators

Python Operators

Python operator is used to perform different operations between different variables and values. In the operation a+b, a and b is operand and + is operator .

Example 1:

number1=5
number2=6
#add operation between two values
print(1+2)
#in the operation 1+2 , 1 & 2 is operand and + is operator .
#+ operator adds the two operands.
#add operation between a variable and a value
print(number1+2)
#add operation between two variables
print(number1+number2)
3
7
11

In the above code we have created two variables named number1 and number2 and performed different operations –

  • Through the print(1 + 2) statement, Python first added 1 and 2 and print 3 to the output.
  • Through the print(number1 + 2) statement, Python first added number1 and 2 .As the value of number1 is 5, addition of number1 and 2 is 7 and Python print 7 to the output .
  • Through the print(number1 + number2) statement Python first added number1 and number2 . Since the values of number1 and number2 are 5 and 6, respectively, Python has printed 11 in output by adding number1 and number2.

Output :

Python offers a total 7 types of operators, theose are following :

  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Membership operators
  • Identity operators

We will now discuss about these operators in detail –

Arithmetic operators

Arithmetic operators are used to perform various mathematical processes (such as addition, division, multiplication, subtraction, etc.). All the arithmetic operators and their application are shown below in the form of a table taking value of x and y as 9 and 2

Operator(name)ExampleDescriptionResult
+(Addition)x + yAdd two operands11
-(Subtraction)x – ySubtract right operand from left operand7
*(Multiplication)x * yMultiply two operands18
/(Division)x / yDivide left operand by right operand (returns a float)4.5
//(Floor division)x // yDivide left operand by right operand (returns a whole number less than the float number return from division)4
%(Modulus)x % yRemainder of the division of left operand by the right operand1
**(Exponential)x ** yLeft operand raised to the power of right operand81

To learn more about arithmetic operator go to – Arithmetic Operators

Comparison operators

The comparison operator compares any two variables or values. All the comparison operators and their application are shown below in the form of a table taking value of x and y as 9 and 2

Operator(name)ExampleDescriptionResult
==(Equal to)x == yReturns True if both operands are equal else FalseFalse
!=(Not equal to)x != yReturns True if both operands are not equal else FalseTrue
>(Greater than)x > yReturns True if left operand is greater than right operand else FalseTrue
>=(Greater than or equal to)x >= yReturns True if left operand is greater than or equal to right operand else FalseTrue
<(Less than)x < yReturns True if left operand is less than right operand else FalseFalse
<=(Less than or equal to)x <= yReturns True if left operand is less than or equal to right operand else FalseFalse

To learn more about comparison operator go to – Comparison Operators

Logical operators

Logical operators are used to check various conditions. All the logical operators and their application are shown below in the form of a table taking value of x and y as True and False

OperatorExampleDescriptionResult
andx and yReturns True if both operands are True else FalseFalse
orx or yReturns True if any of the operands is True else FalseTrue
notx not yReturns True if the operand is False else FalseFalse

To learn more about logical operator go to – Logical Operators

Bitwise operators

Bitwise operator works bit by bit. The bitwise operator is used to perform various operation on binary numbers. All the bitwise operators and their application are shown below in the form of a table.

The values of variables x and y used in the table are 6 and 10 respectively whose binary number is 0110 and 1010 respectively.

Operator(name)ExampleDescriptionResult
&(Bitwise AND)x & ySet each bit to 1 if both bits are 1 else 06 & 10 = 0110 & 1010 = 0010 = 2
|(Bitwise OR)x | ySet each bit to 1 if any of the bits is 1 else 06 | 10 = 0110 | 1010 = 1110 = 14
~(Bitwise NOT or one’s complement)~ xReturns -(x +1)~ 6 = ~ 0110 = -(0110+1) = -0111 = -7 , ~10 = ~1010 = -(1010+1) = -1011 = -11
^(Bitwise XOR)x ^ ySet each bit to 1 if only one of two bits is 1 else 06 ^ 10 = 0110 ^ 1010 = 1100 = 12
<<(Bitwise Left Shift)x << 2Shift left by pushing zeros at the right side6 << 2 = 0110 << 2 = 011000 = 24 ,10 << 2 = 1010 <<2 = 101000 = 40
>>(Bitwise Right Shift)x >> 2Shift right by removing bits from right side6 >> 2 = 0110 >> 2 = 01 = 1 ,10 >> 2 = 1010 >> 2 = 10 = 2

To learn more about bitwise operator go to – Bitwise Operators

Assignment operators

Assignment operator is used to assign a value to a variable. All the assignment operators and their application are shown below in the form of a table. Every time we will take the value of x as 10 .

OperatorExampleDescriptionResult
=(Assign)x = 2x = 22
+=(Add and Assign)x += 2x = x+212
-=(Subtract and Assign)x -= 2x = x-28
*=(Multiply and Assign)x *= 2x = x*220
/=(Divide and Assign)x /= 2x = x/25.0
//=(Divide (floor) and Assign)x //= 2x = x//25
%=(Modulus and Assign)x %= 2x = x%20
**=(Exponent and Assign)x **= 2x = x**2100
&=(Bitwise AND and Assign)x &= 2x = x&22
|=(Bitwise OR and Assign)x |= 2x = x|210
^=(Bitwise XOR and Assign)x ^= 2x = x^28
>>=(Bitwise Right Shift and Assign)x >>= 2x = x>>22
<<=(Bitwise Left Shift and Assign)x <<= 2x = x<<240

To learn more about assignment operator go to – Assignment Operators

Membership operators

Membership operator is used to check whether a value or variable is in a sequence (eg – String, List, Tuple, Dictionary, Set) or not. All the membership operators and their application are shown below in the form of a table taking x and y as 1 and [1,2,3,4]. In the table, left operand x is the value or variable and right operand y is the sequence.

OperatorExampleDescriptionResult
inx in yReturns True if left operand is in the right operand else FalseTrue
not inx not in yReturns True if left operand is not in the right operand else FalseFalse

To learn more about membership operator go to – Membership Operators

Identity operators

Identity operator compares two objects. Identity operator never checks the value of two objects but checks whether the two objects are the same (same memory address) or not. All the identity operators and their application are shown below in the form of a table taking x and y as 2 and 3.

OperatorExampleDescriptionResult
isx is yReturns True if x and y are same object else FalseFalse
is notx is not yReturns True if x and y are different object else FalseTrue

To learn more about identity operator go to – Identity Operators

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment 

Leave a Reply