
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Operator Functions in Python
In Python there are some additional standard library methods for mathematical operations, like arithmetic, logical, relational, bitwise etc. operations. These methods can be found under the operator module.
To use it at first we need to import it the operator standard library module.
import operator
In this section we will see some operator functions for bitwise operations and container operations.
Arithmetic Operations
At first we will see the arithmetic operating functions. These are like below.
Sr.No | Functions & Description |
---|---|
1 |
add(x,y) The add() method is used to add two numbers x and y. It performs simple addition. It is similar to the x + y operation. |
2 |
sub(x,y) The sub() method is used to subtract y from x. It is similar to the x - y operation. |
3 |
mul(x,y) The mul() method is used to multiply two numbers x and y. It is similar to the x * y operation. |
4 |
truediv(x,y) The truediv() method is used to find the result after dividing x by y. This method may return fractional values as result. It is similar to the x / y operation. |
5 |
floordiv(x,y) The floordiv() method is used to find the quotient of x/y. It is similar to the x // y operation. |
6 |
mod(x,y) The mod() method is used to get the remainder of x/y. It is similar to the x % y operation. |
7 |
pow(x,y) The pow() method is used to find the x ^ y. It is similar to the x ** y operation. |
Example code
#Arithmetic Operators import operator print('Add: ' + str(operator.add(56, 45))) print('Subtract: ' + str(operator.sub(56, 45))) print('Multiplication: ' + str(operator.mul(56, 45))) print('True division: ' + str(operator.truediv(56, 45))) # same as a / b print('Floor division: ' + str(operator.floordiv(56, 45))) #same as a // b print('Mod: ' + str(operator.mod(56, 45))) #same as a % b print('pow: ' + str(operator.pow(5, 3)))
Output
Add: 101 Subtract: 11 Multiplication: 2520 True division: 1.2444444444444445 Floor division: 1 Mod: 11 pow: 125
Relational Operations
The operator module also contains the relational operators like <, <=, >, >=, ==, !=.
The operator functions are like below −
Sr.No | Functions & Description |
---|---|
1 |
lt(x,y) The lt() method is used to check whether the number x is less than y or not. It is like x < y operation. |
2 |
le(x,y) The le() method is used to check whether the number x is less than or equal to y or not. It is like x <= y operation. |
3 |
eq(x,y) The eq() method is used to check whether the number x and y are equal or not. It is like x == y operation. |
4 |
gt(x,y) The gt() method is used to check whether the number x is greater than y or not. It is like x > y operation. |
5 |
ge(x,y) The ge() method is used to check whether the number x is greater than or equal to y or not. It is like x >= y operation. |
6 |
ne(x,y) The ne() method is used to check whether the number x and y are not equal. It is like x != y operation. |
Example code
#Relational Operators import operator print('Less Than: ' + str(operator.lt(5, 10))) print('Less Than Equal: ' + str(operator.le(10, 10))) print('Greater Than: ' + str(operator.gt(5, 5))) print('Greater Than Equal: ' + str(operator.ge(5, 5))) print('Equal to: ' + str(operator.eq(12, 12))) print('Not Equal to: ' + str(operator.ne(15, 12)))
Output
Less Than: True Less Than Equal: True Greater Than: False Greater Than Equal: True Equal to: True Not Equal to: True