Python Certification Training for Data Scienc ...
- 56k Enrolled Learners
- Live Class
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:
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.
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 are used to assign values to the variables or any other object in python. Following are the assignment operators that we have in python.
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 are used to compare two values. Following are the comparison operators that we have in python.
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 are used to compare two conditional statements. Following are the logical operators that we have in python.
#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 compare two objects. Following are the identity operators that we have in python.
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 are used to check if a sequence is present in an object. Following are the membership operators that we have in python.
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 compare the binary values. Following are the bitwise operators that we have in python.
#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.