Operators in computer languages are used to perform various operations on variables and values. Python operators perform the same task in the python language. Below is a very simple program in python to understand the basic function of the operator.
print(10 + 10);
This statement in python is making use of the ‘+’ operator. This operator is helping the user directly perform the addition of two numbers.
Python Operators Types
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
- Python Modulo operator
Arithmetic operators are used with numeric values to perform common mathematical operations like addition, subtraction, multiplication, division, etc. Some of them are described below:
Assignment operators are used to assign values to variables. Some of them are described below:
Comparison operators in python are used to perform comparisons between two values. Some of them are described below:
Logical operators are used to combine conditional statements. Some of them are described below:
|and||Returns True if both statements are true||a < 5 AND b < 10|
|or||Returns False if both statements are false||a < 5 OR b < 10|
|not||Reverse the result, returns False if the result is true||NOT(a < 5 and b < 10)|
Identity operators are used for comparing the objects in python, not if they are equal, but if they are actually the same object, with the same memory location. Some of them are described below:
|is||Returns True if both variables are the same object||a is y|
|is not||Returns True if both variables are not the same object||a is not y|
Membership operators are used for testing if a sequence is presented in an object. Some of them are described below:
|in||Returns True if a sequence with the specified value is present in the object||a in y|
|not in||Returns True if a sequence with the specified value is not present in the object||a not in y|
Bitwise operators are used for comparing binary numbers in python. Some of them are described below:
|&||AND||Sets each bit to 1 if both bits are 1|
||||OR||Sets each bit to 1 if one of the 2 bits is 1|
|^||XOR||Sets each bit to 1 if only one of two bits is 1|
|~||NOT||Inverts all the bits|
Python Modulo Operator
We denote the Python Modulo operator as %. We can also call it a Modulus Operator. Python Modulo is a mathematical operation and we can use it for calculating remainder. Basically, this module is used to get the remainder of a division problem. This modulo operator is a type of arithmetic operator.
To read more about Modulo Operator
Operator Precedence and Associativity
The precedence and associativity of operators in python determine the priorities of the operator. Mainly, precedence is used in an expression with more than one operator with different precedence to determine which operation to perform first. However, if an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.
Operator Overloading in python is a term for giving extended meaning beyond their pre-defined meaning. For example operator, ‘+’ is used to perform addition between two integers as well as join two strings and merge two lists. It is achievable because the ‘+’ operator is overloaded by int class and str class in python. The learner might have noticed that the same built-in operator or function shows different behavior for objects of different classes, this is called Operator Overloading.
Below is a python code to understand operator overloading better:
print(10 + 10) print("Developer"+"Helps") print(10 * 10) print("Developer"*2)
20 DeveloperHelps 100 DeveloperDeveloper >