In this article, we will see how to write a code in python to get the multiplication of numbers or elements of lists given as input.

So, there are different ways to perform multiplication in python. The most simple one is using asterisk operator (*****). i.e., you pass two numbers and just printing num1 * num2 will give you the desired output.

**M**ultiplication in python with two numbers entered by user

### 1. Without using a function

Let’s write a quick script to print the product of two numbers without using a function. We’ll simply print the results.

```
n1 = int(input("Enter a number:")
n2 = int(input("Enter another number:")
res = n1 * n2
print("The product is ", res)
```

The output will be:

```
Enter a number: 3
Enter another number: 5
The product is 15
```

### 2. With a function

If you have to use multiplication in your program several times, then you must create a function that will return the product of the numbers passed to it while calling. This will reduce the complexity of your program and introduce reusability, i.e., you can call the same function again and again with a different set of arguments.

One such example is written below:

```
def mult(a , b):
return a * b
n1 = int(input("Enter a number :"))
n2 = int(input("Enter another number :"))
multiplication1 = mult(n1 , n2)
num1 = 6.0
num2 = 5.0
multiplication2 = mult(num1 , num2)
print("The product of {} and {} is {}".format(n1 , n2 , multiplication1))
print("The product of {} and {} is {}".format(num1 , num2, multiplication2)
```

Output for the above code:

```
Enter a number : 4
Enter another number : 7
The product of 4 and 7 is 28
The product of 6.0 and 5.0 is 30.0
```

Here , We have defined a function named **mult** which returns the product. We are calling this function twice in our code. First, using user entered integers values. Second , using the float values. Hence, proving reusability.

## Perform multiplication operations on elements of a list

We can also print the product of all the elements of a given list using different ways :

### 1. By traversing through a list

In this method , we will use a for loop to traverse through the list and a variable ‘**res**‘ that is initialized to **1 **( not 0 because we need the product and 0 * anything = 0). The value of ‘**res**‘ is being updated with each iteration.

```
list1 = [3,4,5]
res = 1
for val in list1 :
res = res * val
print("The product of elements of the given list is ", res)
```

The output :

```
The product of elements of the given list is 60
```

### 2. Using numpy.prod()

In the NumPy, we have **prod()** which takes a list as an argument and returns the product of all the elements in the list. This function is of great use and saves a lot of code. You just need to import **NumPy** to use numpy.prod(). An example is given below:

Code:

```
import numpy
list1 = [2,3,4,5]
list2 = [10,10,10]
ans1 = numpy.prod(list1)
ans2 = numpy.prod(list2)
print("Multiplication of elements of list1 is ",ans1)
print("Multiplication of elements of list2 is ",ans2)
```

Output:

```
Multiplication of elements of list1 is 120
Multiplication of elements of list2 is 1000
```

## Conclusion

That’s how you multiple numbers in Python! I hope this tutorial was useful to you.