Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (86 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

Operators In Python – All You Need To Know

Last updated on Nov 27,2024 4.7K Views


Python language is one of the most popular programming languages. While learning python is seemingly easy, there are certain core concepts that must be mastered before moving on with various applications of python. Operators in python is one of the core fundamental concept in python. This blog will help you understand the different types of operators in python. Following are the topics covered in this blog:

What Is An Operator?

Operators in python are used for operations between two values or variables. The output varies according to the type of operator used in the operation. We can call operators as special symbols or constructs to manipulate the values of the operands. Suppose if you want to perform addition of two variables or values, you can use the addition operator for this operation. The values in the operands can be  variable or any data type that we have in python.

operators in python-edureka

Depending upon the type of operations there are 7 types of operators in python programming language.

Types of Operators

  1. Arithmetic operators
  2. Assignment operators 
  3. Comparison operators
  4. Logical operators
  5. Membership operators
  6. Identity operators
  7. Bitwise operators

Arithmetic operators

Arithmetic operators are used to perform arithmetic calculations in python. Below are the arithmetic operators with names and their symbols. These are the symbols that we use while doing an arithmetic operation in python.

x = 10
y = 15
#addition
x + y
#subtraction
x - y
#multiplication
x * y
#division
x / y
#floor division
x // y
#modulus
x % y
#exponentiation
x ** y

Assignment operators

Assignment operators are used to assign values to the variables or any other object in python. Following are the assignment operators that we have in python.

assignment operators-operators in python-edureka


x = 10
x += 5
#it is same as x = x + 5
x -= 5
x *= 5
x /= 5
#similarly we can write all assignment operators like this.

Comparison operators

Comparison operators are used to compare two values. Following are the comparison operators that we have in python.

comparison operators-operators in python-edureka


x = 5
y = 3

#equal
x == 5

#not equal
x != 5

#greater than
x > y

#less than
x < y 
#greater than or equal to 
x >= y

#less than or equal to
x <= y

Logical operators

Logical operators are used to compare two conditional statements. Following are the logical operators that we have in python.

logical operators-operators in python-edureka


#logical and
5 > 3 and 5 > 4
#it will return true, since both statements are true.
5 > 3 or 5 < 2
#it will return true, since one of the statements is true.
not(5 > 2 and 5 < 3)
#it will return true, even when logical and will return false.

Identity operators

Identity operators compare two objects. Following are the identity operators that we have in python.

identity operators-operators in python-edureka


a = [10,20,30]
b = [10,20,30]
x = b
z = a
# is operator
x is a
#this will return false
x is z
#this will return true.
a is b
#this will return false, even though both have the same items in the list.
a is not b
#this will return true, since both are not same objects.

Membership operators

Membership operators are used to check if a sequence is present in an object. Following are the membership operators that we have in python.

membership operators-operators in python-edureka


a = [10,20,30,'edureka']
#in operator
'edureka' in a
#this will return true, since the item is present in the object.
'python' in a
#this will return false, since it is not present in a.
10 not in a
#this will return false, because it is there.
50 not in a
#this will return true, since there is no 50 in a.

Bitwise operators

Bitwise operators compare the binary values. Following are the bitwise operators that we have in python.

bitwise -operators in python-edureka


#bitwise AND
10 & 12
#this will return 8
#bitwise OR
10 | 12
#this will return 14
#bitwise XOR
10 ^ 12
#this will return 6
#bitwise NOT
~ (10 & 12)
#this will return -9
#left shift
10 << 2 #this will return 40 
#right shift 
10 >> 2
#this will return 2

To understand how we got the result using the bitwise operators lets take a look at the binary equivalent of 10 and 12.

10 in binary is 1010 and 12 in binary is 1100. When doing an AND operation between 1010 and 1100, the bit will be 1 if both the bits are 1. Therefore, the resultant binary equivalent will be 1000 which is 8 when we convert it to decimal.

Bitwise OR operator will set each bit to 1 if one of the bits is 1, bitwise XOR will set each bit to 1 if only one of the bits is 1 and bitwise not will invert all bits.

When doing a left shift or a right shift, the bits will shift left 2 places in our example. Therefore 1010 will become 101000 which is 40. Similarly, when doing right shift 1010 will become 10, which is 2.

In this blog, we have discussed different types of operators in python. This topic is a fundamental concept for learning python programming language. It is a core python concept that is necessary while moving to various other domains in python. If you are looking for a structured learning approach towards python programming, you can enroll for Edureka’s Python Certification Program to kick-start your learning.

If you have any queries, mention them in the comments section. We will get back to you. 

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 28th December,2024

28th December

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 25th January,2025

25th January

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Operators In Python – All You Need To Know

edureka.co