Python Operators

Python Operators are symbols to perform a specific mathematical, relational or logical operation and produce the final result.

Python Operators helps us to perform a lot of operations on Python Variables. These are required for arithmetic operations, to match different variables, assigning value to other variables, and many other operations.


Type of Python variables

Python Operators are categorised into the following groups:

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

In this Python tutorial, we are going to learn all these Python Operators with examples step by step.


Arithmetic operators

The Python Arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication, etc.

OperatorDescriptionExample
+Addition of two operands.x + y
Subtracts second operand from the first.x − y
*Multiplies both operands.x * y
/Divides numerator by denominator and gives us the Quotient.y / x
%Modulus Operator: calculate the remainder of after an integer division.y % x
**Exponentiation y to x i.e. xyx**y
//Floor Division of x//y rounds the result down the division to the nearest whole numberx//y
x = 5
y = 3
print(x+y)     # will give 8
print(x-y)     # will give 2
print(x*y)     # will give 15
print(x/y)     # will give 1.6666666666666667
print(x % y)   # will give 2
print(x**y)    # will give 125
print(x//y)    # will give 1

Assignment operators

The Python assignment operators are used with numeric values to assign a value to a variable. One of the common assignment operators in Python is “=”. It means that the left operand gets set to the value of the assignment expression on the right. Mostly when we declare a variable, we also assign a value to it.

Here is the table which shows all the assignment OPerators supported in Python Programming.

OperatorDescriptionExample
=assign it’s value to the otherx = y
+=Assign the value after additionx+=y same as x=x+y
-=Assign the value after subtractionx-=y same as x=x-y
*=Assign the value after multiplicationx*=y same as x=x*y
/=Assign the value after calculating the quotientx/=y same as x=x/y
%=Assign the value after calculating the remainderx%=y same as x=x%y
//Floor Division of x//y rounds the result down the division to the nearest whole numberx//y

Comparison Operators

The Python Comparison Operators are used to compare two values. The values can be of any type such as numbers, strings, complex numbers, etc. Comparison operators are also known as Relational Operators. Here is the table which shows all Comparison supported in Python programming:

OperatorDescriptionExample
==If the values of both operands are equal, then the condition becomes true, and return True otherwise return false.A == B.
!=If values are not equal, then the condition becomes true and return true otherwise return false.A != B.
>If the value of the left operand is greater than the value of right operand, then the condition becomes true.A > B
<If the value of the left operand is less than the value of right operand, then the condition becomes true and return 1 otherwise return 0(false).A < B
>=Either the value of the left operand is greater than or equal to the value of right operand, then the condition becomes true and returns 1 otherwise return 0(false).A>=B
<=Either the value of the left operand is less than or equal to the value of right operand, then the condition becomes true and returns 1 otherwise return 0(false).A<=B

Example of Comparison Operator

We have discussed the different Comparison operators. Now let’s see a simple example of all Comparison Operators.

x = 10
y = 20
print(x == y)   # return False
print(x != y)   # return True
print(x < y)    # return True
print(x > y)    # return False
print(x <= y)   # return True
print(x >= y)   # return False

Logical Operators

The Python logical operators are used to combine conditional statements. Here is the table showing all logical operators supported by Python:

OperatorDescriptionExample
andLogical AND operator. if both conditions are true then it returns true otherwise returns false.xy
orLogical OR Operator. If any of condition is true, then it will return true otherwise returns false.xy
notLogical NOT Operator. it reverses the result such as if the condition is true then it will return false and vice-versa.not(x

Example of Logical Operator

Let’s see a simple example using logical operators.

x = 1
y = 2
result1 = (x < y) and (x > y)
result2 = (x > y) and (x < y)
result3 = (x > y) and (x == y)
result4 = (x < y) and (x <= y)
result5 = (x < y) or (x > y)
result6 = (x > y) or (x < y)
result7 = (x > y) or (x == y)
result8 = (x < y) or (x <= y)
result9 = not((x < y) and (x > y))
result10 = not((x < y) or (x > y))
print(result1)                  # return False
print(result2)                  # return False
print(result3)                  # return False
print(result4)                  # return True
print(result5)                  # return True
print(result6)                  # return True
print(result7)                  # return False
print(result8)                  # return True
print(result9)                  # return True
print(result10)                 # return False
False
False
False
True
True
True
False
True
True
False

Identity Operator

An identity operator in python is used to check whether the identity of a variable or object is the same as other variables or objects. Variables can be identical in different ways such as value, datatype, etc.

For example, if a variable has integer type value, and we want to check if the other variable is the same as the first variable. Then we use the identity operator.

OperatorDescriptionExample
isIf the identity of a variable(datatype) is the same then it will return true, otherwise returns false.x is y
is notIf the identity of a variable(datatype) is the same then it will return true, otherwise returns false.x is not y

Example of Identitical Operators

Let’s see a simple example of all the identical operators in Python programming.

x = 2
y = 3
result1 = x is y
result2 = x is not y
result3 = type(x) is type(y)
result4 = type(x) is not type(y)
print(result1, result2, result3, result4)
False True True False

Membership Operator

Membership Operator in python is used to check whether a sequence is a member of other variables or lists. In other words, these operators check if a variable is present in the other variable or not.
There is two inbuilt Membership operator in Python Programming:

OperatorDescriptionExample
inIf the value of a sequence is found in the other sequence then it will return true, otherwise returns false.x in y
not inIf the value of a sequence is found in the other sequence then it will return false, otherwise returns true.x is not y

Example of Membership Operators

Let’s see a simple example of all the membership operators in Python programming.

print("Welcome" in "Welcome to Tutorials Class")
print("welcome" in "Welcome to Tutorials Class")
print("Welcome" not in "Welcome to Tutorials Class")
print("welcome" not in "Welcome to Tutorials Class")
True
False
False
True

Bitwise Operators

With the help of Python Bitwise Operators, we can easily perform bitwise operations such as converting bit value from 0 to 1, shifting left to right, etc. Here is the table of all the bitwise operators supported in Python programming:

OperatorDescriptionExample
& Binary ANDOperator copies a bit to the result if it exists in both operands(a & b) (means 0000 1100)
| Binary ORIt copies a bit if it exists in either operand.(a | b) = 61 (means 0011 1101)
^ Binary XORIt copies the bit if it is set in one operand but not both.(a ^ b) = 49 (means 0011 0001)
~ Binary One’s ComplementIt is unary and has the effect of ‘flipping’ bits.(~a ) = -61 (means 1100 0011 in 2’s complement form due to a signed binary number.
<< Binary Left ShiftThe left operands value is moved left by the number of bits specified by the right operand.a << 2 = 240 (means 1111 0000)
>> Binary Right ShiftThe left operands value is moved right by the number of bits specified by the right operand.a >> 2 = 15 (means 0000 1111)

Example of Bitwise Operator in Python Programming

num1 = 12            
num2 = 2            
result = num1 & num2        
print ("Result  is ", result)

result = num1 | num2        
print ("Result is ", result)

result = num1 ^ num2        
print ("Result is ", result)

result = ~num1           
print ("Result is ", result)

result = num1 << 2       
print ("Result is ", result)

result = num1 >> 2       
print ("Result is ", result)
Result  is  0
Result is  14
Result is  14
Result is  -13
Result is  48
Result is  3

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.