89DEVs

Python: How to create a multiply function?

How to create a multiply function in Python?

To create a multiply function in Python, define a new function that returns the product of the multiplication. Or use the mul() function in the operator module.

define multiply function

To create a multiply function in Python, define the function as shown below. # define multiply function in python def multiply(a, b): return a * b # call the multiply function result = multiply(2, 2) # print result print(result) The multiply function is defined. Then the function is called, with the arguments 2 and 2. The result is assigned to the variable result. Finally, the result is printed. 4

import mul()

Instead of creating your own multiply function in Python, use the mul() function in the operator module. # import mul from operator module from operator import mul # call mul() function print(mul(2, 2)) In this example, mul is imported form the operator module. Then the mul() function is called, with the arguments 2 and 2. Finally, the result is printed. 4

                
        

Summary


Click to jump to section