Home » Python Tutorial » Python Operators

Python Operators


Since we have covered all the built-in data types of python, in this tutorial we can look into the operators we can perform on the variables using the python operators.

In Python, the operators are divided into the following operators

Types of Operators in Python

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

Arithmetic Operators in Python

Arithmetic operators are used for performing arithmetic operations on variables like addition, subtraction, multiplication, division, etc.

Operator Name Example
+ Addition x+y
Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus(Returns the remainder of the division operation) x%y
** Exponential Operator x**y
// Floor division operator(Result of the division operation without decimal points) x//y

Examples:

x=10
y=5
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
print(x//y)

Assignment Operators in Python

Assignment operators are used for assigning values to variables.

Operator Example
= x=5
+= x+=5 same as x=x+5
-= x-=5 same as x=x-5
*= x*=5 same as x=x*5
/= x/=5 same as x=x/5
//= x//=5 same as x=x//5
%= x%=5 same as x=x%5
&= x&=5 same as x=x&5
|= x|=5 same as x=x|5
^= x^=5 same as x=x^5
>>= x>>=5 same as x=x>>5
<<= x<<=5 same as x=x<<5

In the above tables, these ‘&’,’|’,’^’,’>>’,'<<‘ are bitwise operators.

x=10
print(x)
x+=5
print(x)
x-=5
print(x)
x*=5
print(x)
x/=5
print(x)
x//=5
print(x)
x%=5
print(x)
x=10
x&=5
print(x)
x!=5
print(x)
x^=5
print(x)
x<<=5
print(x)
x>>=5
print(x)

Comparison Operators in Python

As the name implies, comparison operators are used for comparing two values.

Operator Name Example
== Equal to x==5
!= Not equal to x!=5
> Greater than x>5
< Lesser than x<5
>= Greater than or equal to x>=5
<= Lesser than or equal to x<=5

As we have covered Arithmetic, Assignment, and comparison operators above, now we can cover the remaining operators below.

x=10
print(x==5)
print(x!=5)
print(x>5)
print(x<5)
print(x>=5)
print(x<=5)

Logical Operators in Python

Logical operators are used for combining two conditional statements.

Operator Description Example
and Returns True only if all the conditions are True if x>=5 and x<=10
or Returns True if one of the two conditions is True if x<5 or x<10
not Reverses the result i.e Returns True if the result of the conditions is False and vice versa if not (if x>=5 and x<=10)

Examples of logical operators are covered in the “If Else” tutorial.

x=10
print(x>=5 and x<=10)
print(x<5 or x<10)
print(not(x>=5 and x<=10))

Identity Operators in Python

Identity operators are used for comparing objects, not if they are equal. It checks for, whether the objects are the same and shares the same memory location.

Operator Description Example
is Return True if both are variables of the same object x is y
is not Returns True if both are not variables of the same object x is not y

An example is the same as the one we have seen for deep and shallow copy in the “Lists” tutorial.

l=[1,2,3]
l1=l.copy()
l2=l
print(l1 is not l)
print(l2 is l)

Membership Operators in Python

We have already covered this in the “Lists” tutorial. It checks whether the object is present in a sequence.

Operator Description Example
in Return True if the object is present in the sequence 1 in [1,2,3]
not in Returns True if the object is not present in the sequence 4 not in [1,2,3]
l=[1,2,3]
print(1 in l)
print(1 not in 1)

Bitwise Operators in Python

Bitwise operators are used for comparing binary numbers.

Operator Name Example
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of the two bits is 1
^ XOR Sets each bit to 1 if only one of the two bits is 1
~ NOT Inverts all the bits
>> SIGNED RIGHT SHIFT Shifts right by pushing copies of the leftmost bit from the left, and let the rightmost bits fall off
<< ZERO LEFT SHIFT Shifts left by pushing zeros in from the right and let the leftmost bits fall off
x=5
print(x&5)
print(x|5)
print(x^5)
print(~x)
print(x>>5)
print(x<<5)

Reference

Reference1

Python Basic Syntax