Python Operators: Types of Operators in Python

Sakshi Dhameja  22 min read
27 Sep 2023
Beginner
1.32K Views

Python Operators: An Overview

Are you constantly wondering what to do with programming including Python programming in particular? Have you been trying to understand concepts related to operators within the Python training program language, but found yourself confused by all of its idiosyncrasies? Don’t worry, we’ve got your back. In this blog post, we will provide Python Operators, types of operators in Python, Operators in Python, Precedence of Operators in Python & comprehensive overview of some of the most common and powerful Python operators so that you get a crystal clear idea as soon as possible!

What are Operators in Python?

Python operators are not different than any other language. They are operators specific to the Python language and allow a programmer to perform operations on operators, variables, objects, and more. In Python, operators are used in expressions, where values such as numbers, strings, or others are manipulated and combined with operators to produce a result. With Python operators, users can do a variety of tasks such as creating complex mathematical equations that involve exponents and roots or adding strings together for concatenation. 

Types of Python Operators

Python language supports various types of operators, which are:
  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Python Arithmetic Operators

Python Arithmetic Operators provide a simple and intuitive way to do math in Python. By leveraging the Python language's powerful syntax and abstractions, Python Arithmetic Operators allow for quick setup and use of mathematical operations. There are some arithmetic operators those are Addition, Subtraction, Multiplication, Division, Modulus, Exponents, and Floor Division.
OperatorNameExample
+Addition10 + 20 = 30
-Subtraction20 – 10 = 10
*Multiplication10 * 20 = 200
/Division20 / 10 = 2
%Modulus22 % 10 = 2
**Exponent4**2 = 16
//Floor Division9//2 = 4

Example

a = 21
b = 10
# Addition
print ("a + b : ", a + b)
# Subtraction
print ("a - b : ", a - b)
# Multiplication
print ("a * b : ", a * b)
# Division
print ("a / b : ", a / b)
# Modulus
print ("a % b : ", a % b)
# Exponent
print ("a ** b : ", a ** b)
# Floor Division
print ("a // b : ", a // b)

The two variables "a" and "b" are defined in this code, which then applies a number of arithmetic operations to them (including addition, subtraction, multiplication, division, modulus, exponentiation, and floor division) and outputs the results.

Output

a + b : 31
a - b : 11
a * b : 210
a / b : 2.1
a % b : 1
a ** b : 16679880978201
a // b : 2

Python Comparison Operators

Python comparison operators are a handy tool for developers who need to compare values and evaluate conditions within their python code. These operators allow you to determine the relative size of two or more items, or if they are equal. There are some comparison operators, those are equal, not equal, greater than, less than, greater than, or equal to, and less than or equal to.
OperatorNameExample
==Equal4 == 5 is not true.
!=Not Equal4 != 5 is true.
>Greater Than4 > 5 is not true
<Less Than4 < 5 is true
>=Greater than or Equal to4 >= 5 is not true.
<= Less than or Equal to 4 <= 5 is true.

Example

a = 4
b = 5
# Equal
print ("a == b : ", a == b)
# Not Equal
print ("a != b : ", a != b)
# Greater Than
print ("a > b : ", a > b)
# Less Than
print ("a < b : ", a < b)
# Greater Than or Equal to
print ("a >= b : ", a >= b)
# Less Than or Equal to
print ("a <= b : ", a <= b)

This code compares the values of variables 'a' and 'b' and prints if they are equal, not equal, greater than, less than, more than or equal to, and less than or equal to each other.

Output

a == b : False
a != b : True
a > b : False
a < b : True
a >= b : False
a <= b : True

Python Assignment Operators

Assignment Operator in Python is a key element in Python programming. The assignment operator in Python allows the programmer to create, manipulate, and assign values to any Python variable in an efficient manner. Python Assignment Operators can be used in a variety of ways: they can perform basic math operations; they can assign multiple objects with one statement; they can assign unchanged input variables to different output locations; and many more ways.

Operator Name Example
= Assignment Operator a = 10
+= Addition Assignment a += 5 (Same as a = a + 5)
-= Subtraction Assignment a -= 5 (Same as a = a - 5)
*=Multiplication Assignmenta *= 5 (Same as a = a * 5)
/=Division Assignmenta /= 5 (Same as a = a / 5)
%=Remainder Assignmenta %= 5 (Same as a = a % 5)
**=Exponent Assignmenta **= 2 (Same as a = a ** 2)
//=Floor Division Assignmenta //= 3 (Same as a = a // 3)

Example

# Assignment Operator
a = 10
# Addition Assignment
a += 5
print ("a += 5 : ", a)
# Subtraction Assignment
a -= 5
print ("a -= 5 : ", a)
# Multiplication Assignment
a *= 5
print ("a *= 5 : ", a)
# Division Assignment
a /= 5
print ("a /= 5 : ",a)
# Remainder Assignment
a %= 3
print ("a %= 3 : ", a)
# Exponent Assignment
a **= 2
print ("a **= 2 : ", a)
# Floor Division Assignment
a //= 3
print ("a //= 3 : ", a)

The Python assignment operators are shown in this code. It begins with the value of 'a' equal to 10, and then goes through the steps of addition, subtraction, multiplication, division, remainder, exponentiation, and floor division, updating 'a' as necessary and outputting the results.

Output

a += 5 : 105
a -= 5 : 100
a *= 5 : 500
a /= 5 : 100.0
a %= 3 : 1.0
a **= 2 : 1.0
a //= 3 : 0.0

Python Bitwise Operators

Python Bitwise Operators are an essential part of the Python programming language. They allow a Python programmer to perform operations on variables that act like binary numbers, allowing them to manipulate and compare data. Python Bitwise Operators can help simplify programs and in some cases may even speed up code execution time, making it an invaluable tool for Python programmers.
Operator Name Example
&Binary ANDSets each bit to 1 if both bits are 1
| Binary OR Sets each bit to 1 if one of the two bits is 1
^ Binary XOR Sets each bit to 1 if only one of two bits is 1
~Binary Ones ComplementInverts all the bits
~Binary Ones ComplementInverts all the bits
<<Binary Left ShiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Binary Right ShiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Example

a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
# Binary AND
c = a & b # 12 = 0000 1100
print ("a & b : ", c)
# Binary OR
c = a | b # 61 = 0011 1101
print ("a | b : ", c)
# Binary XOR
c = a ^ b # 49 = 0011 0001
print ("a ^ b : ", c)
# Binary Ones Complement
c = ~a; # -61 = 1100 0011
print ("~a : ", c)
# Binary Left Shift
c = a << 2; # 240 = 1111 0000
print ("a << 2 : ", c)
# Binary Right Shift
c = a >> 2; # 15 = 0000 1111
print ("a >> 2 : ", c)

The binary representations of the numbers 'a' and 'b' are subjected to bitwise operations in this code. It displays the results of binary AND, OR, XOR, Ones Complement, Left Shift, and Right Shift operations.

Output

a & b : 12
a | b : 61
a ^ b : 49
~a : -61
a >> 2 : 240
a >> 2 : 15

Python Logical Operators

Python has a great range of logical operators available to help get the most precise results when coding. These Python logical operators allow you to make evaluations based on many factors. They incorporate the use of different comparison symbols and values that return a Boolean value, which is either true or false. Python logical operators are an efficient tool in helping you write code as you can combine multiple expressions together in one statement.
Operator Description Example
and Logical AND If both of the operands are true then the condition becomes true. (a and b) is true.
or Logical OR If any of the two operands is non-zero then the condition becomes true. (a or b) is true.
not Logical NOT Used to reverse the logical state of its operand Not(a and b) is false.

Example

a = 20
b = 20
c = -10
if a > 0 and b > 0:
 print("The numbers are greater than 0")
if a > 0 and b > 0 and c > 0:
 print("The numbers are greater than 0")
else:
 print("Atleast one number is not greater than 0")

The code prints "The numbers are greater than 0" if variables "a" and "b" are greater than 0 in the test. Then it checks to see if all three variables ('a,' 'b,' and 'c') are greater than 0, but since 'c' is not, it outputs "At least one number is not greater than 0."

Output

The numbers are greater than 0
Atleast one number is not greater than 0

Python Membership Operators

Membership operators in Python are the means of specifying membership in Python. This can be used to see if a particular value exists within a sequence such as sets, lists, or tuples. Membership operators in Python will also provide the index numbers and can serve as a Python shortcut for sorting through data quickly.
Operator Description Example
in Evaluates to true if it finds a variable in the specified sequence and false otherwise. x in y, here in results in a 1 if x is a member of sequence y.
not in Evaluates to true if it does not find a variable in the specified sequence and false otherwise. x not in y, here not in results in a 1 if x is not a member of sequence y.

Example

a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
   print "Line 1 - a is available in the given list"
else:
   print "Line 1 - a is not available in the given list"
if ( b not in list ):
   print "Line 2 - b is not available in the given list"
else:
   print "Line 2 - b is available in the given list"
a = 2
if ( a in list ):
   print "Line 3 - a is available in the given list"
else:
   print "Line 3 - a is not available in the given list"

The 'in' and 'not in' operators are used in this code to show how to verify whether the values 'a' and 'b' are present in the list. When 'a' is set to 2, it checks once more to make sure that 'a' is present in the list and outputs if each value is present or not.

Output

Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list

Python Identity Operators

Python Identity Operators are a great way to check if two variables match exactly, down to their memory address. The identity operator in Python compares the memory locations of two Python objects and returns a boolean value indicating whether they are the same object or not. This makes Python identity operators perfect for users looking for exact matches between variables in Python code.
Operator Description Example
is Evaluates to true if the variables on either side of the operator point to the same object and false otherwise x is y, here are results in 1 if id(x) equals id(y)
is not Evaluates to false if the variables on either side of the operator point to the same object and true otherwise x is not y, there are no results in 1 if id(x) is not equal to id(y).

Example

a = 20
b = 20
if ( a is b ):
   print "Line 1 - a and b have same identity"
else:
   print "Line 1 - a and b do not have same identity"
if ( id(a) == id(b) ):
   print "Line 2 - a and b have same identity"
else:
   print "Line 2 - a and b do not have same identity"
b = 30
if ( a is b ):
   print "Line 3 - a and b have same identity"
else:
   print "Line 3 - a and b do not have same identity"
if ( a is not b ):
   print "Line 4 - a and b do not have same identity"
else:
   print "Line 4 - a and b have same identity"

The 'is' and 'is not' operators are used in this code to show how to verify the identity (memory address) of the variables 'a' and 'b'. It indicates whether or not 'a' and 'b' share the same identity, and it updates 'b' to 30 to show that they no longer share the same identity under the third and fourth requirements.

Output

Line 1 - a and b have same identity
Line 2 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity

Python Operators Precedence

Python Operators Precedence is can be explained by this given table,

Sr.No. Operator Description
1.** Exponentiation (raise to the power)
2.~ + - Complement, unary plus and minus (method names for the last two are +@ and -@)
3.* / % //Multiply, divide, modulo, and floor division
4.+ -Addition and subtraction
5.>> <<Right and left bitwise shift
6.&Bitwise 'AND'
7.^ |Bitwise exclusive `OR' and regular `OR'
8.<= < > >=Comparison operators
9.<> == !=Equality operators
10.= %= /= //= -= += *= **=Assignment operators
11.is is notIdentity operators
12in not inMembership operators
13.not or andLogical operators

FAQs

1. What is an operator in Python?

A symbol or special character that performs actions on one or more operands is referred to as an operator in Python.

2. What are the operators used in the Python list?

With Python lists, the operators + (concatenation), * (repetition), [] (indexing), and [:] (slicing) are often used.

3. What are the 7 operators in Python?

In Python, there are seven fundamental operators: + (addition), - (subtraction), * (multiplication), / (division), % (modulo), // (floor division), and ** (exponentiation).

Summary
In conclusion, we have learned about the various types of operators available in Python. We also learned the definition of the operator including its usage with the help of examples, Python Operators, types of operators in Python, Operators in Python, and Precedence of Operators in Python. I hope this article was helpful to you in understanding the concept better. If you have any doubts or queries feel free to ask. Additionally, if you're looking to validate your understanding of Python operators and enhance your programming skills, consider pursuing a Python Certification. Thanks for reading!

Share
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like Java, Python, C, C++ etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like JavaScript and Cloud.

Accept cookies & close this