Python Methods and Functions

## Arithmetic Operations

## Sample Code

## Output

## Relational Operations

## Sample Code

## Output

Python has several additional standard library methods for mathematical operations such as arithmetic, logical, relational, bitwise, and so on. Operations. These methods can be found in the operator module.

To use it, we first need to import it into the operator's standard library module.

import operator

In this section we will see some operator functions for bitwise and container operations.

~~ ~~

First, we will see arithmetic control functions. It's like below.

- add () is used to add two numbers x and y. It does simple addition. This is similar to the x + y operation.
- sub () is used to subtract y from x. This is similar to the operation x &y.
- mul () is used to multiply two numbers x and y. This is similar to the x * y operation.
- truediv () is used to find the result after dividing x by y. This method can return fractional values as a result. This is similar to the x / y operation.
- floordiv () is used to find the x / y ratio. This is similar to the x // y operation.
- mod () is used to get the remainder of x / y. This is similar to the x% y operation.
- pow () is used to find x ^ y. This 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 relational operators such as & lt ;, & lt; =, & gt;, & gt; =, == ,! =.

Operator functions as below &

- lt () is used to check if x is less than y or not. It's like the operation x & lt; y.
- le () is used to check if a number x is less than or equal to y or not. It's like the operation x & lt; = y.
- eq () is used to check if x and y are equal or not. It's like the operation x == y.
- gt () is used to check if x is greater than y or not. It's like the operation x & gt; y.
- ge () is used to check if x is greater than or equal to y or not. It's like the operation x & gt; = y.
- ne () is used to check if x and y are not equal. It's like operation x! = Y.

# 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: Tr ue Greater Than: False Greater Than Equal: True Equal to: True Not Equal to: True

R for Everyone: Advanced Analytics and Graphics. ...

28/08/2021

ig Data applications are growing very rapidly around the globe. This new approach to decision making takes into account data gathered from multiple sources. Here my goal is to show how these diverse s...

10/07/2020

Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems PDF, 2nd Edition. This book assumes you know next to nothing about m...

22/08/2021

This book is an indispensable guide focuses on Machine Learning and R Programming, in an instructive and conversational tone which helps them who want to make their career in Big Data Analytics/ Data ...

10/07/2020

X
# Submit new EBook