Python Operators are used to performing operations on values and variables. Operator is nothing but used with Variables to get some satisfying output. Using operators you can perform the operation on variables like 1 + 2 = 3 where +, = are operators.

Learn Python Programming from Basic to Advanced Concept with Examples. Python Operators, Assignment,Comparison,Logical,Identity,Membership,Bitwise operators
Image by Gerd Altmann from Pixabay

Types of Operators

Python divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators
Python Operators Video Tutorial

Let us have a look at all the operators one by one:

Python Arithmetic Operators

Arithmetic Operators are used to perform arithmetic operation.

OperatorExampleSame As
+Additionx + y
Subtractionx – y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
 **Exponentiationx ** y
//Floor divisionx // y
x = 5
y = 3

print("Additon:",x + y)
print("Substraction:",x - y)
print("Multiplication:",x * y)
print("Division:",x / y)
print("Modulus:",x % y)
print("Exponentiation:",x ** y)
print("Floor Division:",x // y)

Python Assignment Operators

Assignment operators are used to assign values to variables:

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x – 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
=x **= 3x = x ** 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3
x = 5

print("Equal Sign:",x )

x = 5
x += 3
print("Addition Assign:",x)

x = 5
x -= 3
print("Substraction Assign:",x)

x = 5
x *= 3
print("Multiplication Assign:",x)

x = 5
x /= 3
print("Division Assign:",x)

x = 5
x %= 3
print("Modulus Assign:",x)

x = 5
x **= 3
print("Exponentiation Assign:",x)

x = 5
x //= 3
print("Floor Division Assign:",x)

Python Comparison Operators

Comparison operators are used to compare two values:

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y
x = 5
y = 3

print("Equal:",x == y)
print("Not Equal:",x != y)
print("Greater Than:",x > y)
print("Less Than:",x < y)
print("Greater than or equal to:",x >= y)
print("Less than or equal to:",x <= y)

Python Logical Operators

Logical operators are used to combine conditional statements:

OperatorDescriptionExample
and Returns True if both statements are truex < 5 and x < 10
orReturns True if one of the statements is truex < 5 or x < 4
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)
x = 5

print(x > 3 and x < 10)
# returns True because 5 is greater than 3 AND 5 is less than 10

print(x > 3 or x < 10)
# returns True because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)

print(not(x > 3 and x < 10))
# returns False because not is used to reverse the result

Python Identity Operators

Identity operators are used to comparing the objects, not if they are equal, but if they are actually the same object, with the same memory location:

OperatorDescriptionExample
is Returns true if both variables are the same objectx is y
is notReturns true if both variables are not the same objectx is not y
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print(x is z)
# returns True because z is the same object as x

print(x is not z)
# returns False because z is the same object as x

Python Membership Operators

Membership operators are used to testing if a sequence is presented in an object:

OperatorDescriptionExample
in Returns True if a sequence with the specified value is present in the objectx in y
not inReturns True if a sequence with the specified value is not present in the objectx not in y
x = ["apple", "banana"]

print("banana" in x)
# returns True because a sequence with the value "banana" is in the list

print("pineapple" not in x)
# returns True because a sequence with the value "pineapple" is not in the list

Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

OperatorNameDescription
ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
 ^XORSets each bit to 1 if only one of two bits is 1
NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Whats Next – Python – If…Else Condition