- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

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.

At first we will see the arithmetic operating functions. These are like below.

Sr.No | Functions & Description |
---|---|

1 |
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 |
The sub() method is used to subtract y from x. It is similar to the x - y operation. |

3 |
The mul() method is used to multiply two numbers x and y. It is similar to the x * y operation. |

4 |
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 |
The floordiv() method is used to find the quotient of x/y. It is similar to the x // y operation. |

6 |
The mod() method is used to get the remainder of x/y. It is similar to the x % y operation. |

7 |
The pow() method is used to find the x ^ y. It is similar to the x ** y operation. |

#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)))

Add: 101 Subtract: 11 Multiplication: 2520 True division: 1.2444444444444445 Floor division: 1 Mod: 11 pow: 125

The operator module also contains the relational operators like <, <=, >, >=, ==, !=.

The operator functions are like below −

Sr.No | Functions & Description |
---|---|

1 |
The lt() method is used to check whether the number x is less than y or not. It is like x < y operation. |

2 |
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 |
The eq() method is used to check whether the number x and y are equal or not. It is like x == y operation. |

4 |
The gt() method is used to check whether the number x is greater than y or not. It is like x > y operation. |

5 |
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 |
The ne() method is used to check whether the number x and y are not equal. It is like x != y operation. |

#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)))

Less Than: True Less Than Equal: True Greater Than: False Greater Than Equal: True Equal to: True Not Equal to: True

- Related Questions & Answers
- Operator Functions in C#
- Mathematical Functions in Python - Special Functions and Constants
- Statistical Functions in Python
- Decimal Functions in Python
- Partial Functions in Python?
- Time Functions in Python?
- Mathematical Functions in Python?
- Iterator Functions in Python
- Log functions in Python
- Trigonometric Functions in Python
- Python Mathematical Functions
- Inplace operator in Python
- Ternary Operator in Python?
- Terminal Control Functions in Python
- First Class functions in Python

Advertisements