Operator Module in Python

Operator Module in Python

In this tutorial, we will learn about the operator module in Python and its various functions. We will use these functions of the operator module in a Python program to demonstrate their work.

Python Operator Module

As the name suggests, the operator module performs various operations and operates two input numbers in a Python program. The Python operator module is one of the inbuilt modules in Python, and it provides us with a lot of functions such as add(x, y), floordiv(x, y) etc., which we can use to perform various mathematical, relational, logical and bitwise operations on two input numbers. We will learn about some of these functions of the operator module in this section and will use each of them in a Python program to understand their functioning. We have categorised the operator functions into categories according to their functioning.

Mathematical Operation Functions:

Here, we will look at some of the major operator functions we can use to perform mathematical operations such as addition, subtraction, division etc., on two input given values. Let’s have a look at the following functions where we understand them in brief by using them in a program:

1. add(x, y): We can use add(x, y) function of the operator module to add two input given numbers, whereas x and y are two input values.

Operator module performs “x + y” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 234
Enter second integer number: 729
Addition of input numbers given by you is:  963

Explanation: After importing the operator module in the program, we added the two user input numbers by using the add() function and giving these input numbers as arguments. Then, we printed the addition of two numbers as a result of the program in the output.

2. sub(x, y): As the name suggests, the sub() function of operation is used to perform subtraction operation, and we can subtract two numbers by giving them as an argument inside the sub() function.

Operator module performs “x – y” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 727
Enter second integer number: 344
Subtraction of input numbers given by you is:  383

Explanation: We have subtracted the user input numbers by giving them as arguments inside the sub() function of the operation module and printed the subtraction result in the output.

3. mul(x, y): We can use the mul(x, y) function to get the multiplication of two input given numbers, whereas x and y are two input values.

Operator module performs “x * y” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 27
Enter second integer number: 23
Multiplication result of numbers given by you is:  621

Explanation: We have multiplied the user input numbers by giving them as arguments inside the mul() function of the operation module and printed the result.

4. truediv(x, y): The truediv() function of the operator module is used to get the exact division value or result of the two numbers, i.e., x and y, which is given as arguments in it.

Operator module performs “x / y” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 25
Enter second integer number: 6
True division result of numbers given by you is:  4.166666666666667

Explanation: We divided the user input numbers by giving them as arguments inside the truediv() function to get the exact true division result and printed it.

5. floordiv(x, y): As the function’s name suggests, the floordiv() function is also used to divide two numbers, but it performs floor division on them and returns the floored value, i.e., greatest small integer.

Operator module performs “x // y” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 25
Enter second integer number: 6
Floor division result of numbers given by you is:  4

Explanation: We have performed the floor division on the user input numbers by giving them as arguments inside the floordiv() function to get the floored value as the division result and printed it.

6. mod(x, y): The mod(x, y) is used to get the modulus of two numbers by giving them as arguments inside the function.

Operator module performs “x % y” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 17
Enter second integer number: 13
Result of modulus operation on numbers given by you is:  4

Explanation: We performed the modulus operation on the user input numbers by giving them as arguments inside the mod() function and printed the modulus result.

7. pow(x, y): To get the power value of a function, we use the pow(x, y) function where y is treated as the power of x.

Operator module performs “x ** y” operation in this function.

Syntax:

Example:

Output:

Enter an integer number: 5
Enter power value for the integer number you gave: 5
Exponentiation result of power value of number given by you is:  3125

Relational Operation Functions:

We will look at some more functions from the operator module, but these functions belong to the relational operation category. With these functions, we can establish a relationship between any two given input numbers such as smaller one, larger one, equal etc. Look at the following operator functions from relational category with the use of each of them in a Python program:

8. lt(x, y): We can use this function to compare if the first number (x) given in the argument is smaller than the second input number, i.e., y. lt(x, y) will return ‘true’ in the output if x is smaller than y; otherwise, it will return false as a result.

Operator module performs “a < b” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 24
Enter second integer number: 26
Is first number given by you is smaller than the second number:  True

Explanation: After taking both numbers as user input, we have compared them and checked if the first number given by the user is smaller than the second number or not. Then, we printed the comparison result as true or false in the output statement.

9. le(x, y): We can use the le(x, y) function of the operator module to establish a relationship between x & y and to check if x (first number) is smaller or equal to y (second number) or not.

Operator module performs “a <= b” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 23
Enter second integer number: 19
Is first number given by you is smaller or equal to the second number:  False

10. gt(x, y): The gt(x, y) is used to compare the two input numbers and check if the first number in the argument (x) is greater than the second number (y) or not, and it also results in the form of True or False only.

Operator module performs “a > b” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 38
Enter second integer number: 49
Is first number given by you is greater than the second number:  False

11. ge(x, y): We can use the ge(x, y) function of the operator module to establish a relationship between x & y and to check if x (first number) is greater or equal to y (second number) or not.

Operator module performs “a => b” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 23
Enter second integer number: 21
Is first number given by you is greater or equal to the second number:  True

12. eq(x, y): The eq(x, y) is used to establish a relationship between the two input numbers and check if the first number in the argument (x) is equal to the second number (y), and it also results in the form of True or False only.

Operator module performs “a = b” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 24
Enter second integer number: 26
Is both input numbers given by you are equal:  False

13. ne(x, y): The ne(x, y) function works exactly opposite to that of the eq(x, y) function, i.e., it checks if both the numbers are given in the argument are not equal, and then it yields result in the form of true and false.

Operator module performs “a != b” operation in this function.

Syntax:

Example:

Output:

Enter first integer number: 7
Enter second integer number: 9
Is both input numbers given by you are not equal:  True

Conclusion

In this tutorial, we have discussed the operator module in Python and its various function with examples.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263408.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论