Python Operators Overview with Examples

Python Operators are used to performing operations on variables and values. The operators use numbers, strings other data types to get the action and application make math calculation or logic

In this tutorial, you will learn about different types of Python Operators, their syntax expression, and how to use them with examples.

Python Operators Overview with Examples

Python language supports the following types of operators.

  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Let’s see the one by one Python Operators :

Python Arithmetic Operators

OperatorName
+Addition
Subtraction
*Multiplication
/Division
%Modulus
**Exponentiation
//Floor division

You can perform math operation with Arithmetic’s operators, here are examples of Arithmetic’s operators :

a = 10
b = 4
print("Addition : ", 10 + 4)
print("Subtraction : ", 10 - 4)
print("Multiplication : ", 10 * 4)
print("Division : ", 10 / 4)
print("Modulus : ", 10 % 4)
print("Exponentiation : ", 10 ** 4)
print("Floor division : ", 10 // 4)

Output : Addition : 14
Subtraction : 6
Multiplication : 40
Division : 2.5
Modulus : 2
Exponentiation : 10000
Floor division : 2

Python Assignment Operators

The assignment operators in Python are used to store data into a variable.

OperatorName & DescriptionExample
=Assigns values from right side operands to left side operandb = a assigns value of an into b
+=Add AND – It adds right operand to the left operand and assigns the result to left operandc += a is equivalent to c = c + a
-=Subtract AND – It subtracts right operand from the left operand and assign the result to left operandc -= a is equivalent to c = c – a
*=Multiply AND – It multiplies right operand with the left operand and assign the result to left operandc *= a is equivalent to c = c * a
/=Divide AND – It divides left operand with the right operand and assign the result to left operandc /= a is equivalent to c = c / ac /= a is equivalent to c = c / a
%=Modulus AND – It takes modulus using two operands and assign the result to left operandc %= a is equivalent to c = c % a
**=Exponent AND – Performs exponential (power) calculation on operators and assign a value to the left operandc **= a is equivalent to c = c ** a
//=Floor Division – It performs floor division on operators and assign value to the left operandc //= a is equivalent to c = c // a
a = 5

b = a
print("Assigns - value of b : ", b)

b += a
print("Add AND - value of b : ", b)

b -= a
print("Subtract AND - value of b : ", b)

b *= a
print("Multiply AND - value of b : ", b)

b /= a
print("Divide AND - value of b : ", b)

b %= a
print("Module AND - value of b : ", b)

b **= a
print("Exponent AND - value of b : ", b)

b //= a
print("Floor Division AND - value of b : ", b)

Output : Assigns – value of b : 5
Add AND – value of b : 10
Subtract AND – value of b : 5
Multiply AND – value of b : 25
Divide AND – value of b : 5.0
Module AND – value of b : 0.0
Exponent AND – value of b : 0.0
Floor Division AND – value of b : 0.0

Python Comparison Operators

OperatorNameExample
==Equal(a == b) is not true.
!=Not equal(a != b) is true.
>Greater than(a > b) is not true.
<Less than(a < b) is true.
>=Greater than or equal to(a >= b) is not true.
<=Less than or equal to(a <= b) is true.

Let’s see the examples

a = 10
b = 4
print("Equal : ", 10 == 4)
print("Not equal : ", 10 != 4)
print("Greater than : ", 10 > 4)
print("Less than : ", 10 < 4)
print("Greater than or equal to : ", 10 >= 4)
print("Less than or equal to: ", 10 <= 4)

Output : Equal : False
Not equal : True
Greater than : True
Less than : False
Greater than or equal to : True
Less than or equal to: False

Python Logical Operators

OperatorDescriptionExample
andReturns True if both statements are truea < 5 and  b < 10
orReturns True if one of the statements is truea < 5 or b < 4
notReverse the result, returns False if the result is truenot(a < 5 and b < 10)
a = 10
b = 4
print("and : ", a < 5 and b < 10)
print("or : ", a < 5 or b < 10)
print("not : ", not(a < 5 and b < 10))

Output : and : False
or : True
not : True

Python Bitwise Operators

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

Python Membership Operators

Python membership operators test(search or find) for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as below :

OperatorDescriptionExample
inReturns True if a sequence with the specified value is present in the objectan in listOne
not inReturns True if a sequence with the specified value is not present in the objecta not in listOne

Returns True if a sequence with the value is in the list else False

colorList = ["red", "yellow", "black"]

print("red" in colorList)
print("orange" in colorList)

print("red" not in colorList)

Output : True
False
False

Python Identity Operators

OperatorDescriptionExample
isReturns true if both variables are the same objecta is b
is notReturns true if both variables are not the same objecta is not b

Example

a = 10
b = a
print("is : ", a is b)
print("is not : ", a is not b)

Output: is : True
is not : False

Do comment if you have any doubts and suggestions on this tutorial.

Note : All Python Operators examples is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Examples of Operators are in Python 3, so it may change its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.