- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

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

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

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

- Related Articles
- Operator Functions in C#
- Ternary Operator in Python?
- Inplace operator in Python
- Mathematical Functions in Python - Special Functions and Constants
- Decimal Functions in Python
- Time Functions in Python?
- Mathematical Functions in Python?
- Partial Functions in Python?
- Log functions in Python
- Iterator Functions in Python
- Trigonometric Functions in Python
- Statistical Functions in Python
- What is @ operator in Python?
- Types of Operator in Python
- String Formatting Operator in Python