Multiply in Python

Multiply in Python

Python allows us to perform various mathematical operations using different operators and functions. Multiplication is one of the four basic arithmetic operations.

Multiply operator in Python

To perform multiplication in Python, we use the * operator.

For example,

Output:

20

The final output is an integer as well. Similarly, we can multiply two float values as well.

For example,

Output:

23.865

The final result here is a float value. We can control the final number of digits after the decimal place using the round() function or string formatting. If we multiply an integer and a float value, then also we get a float value in the result.

See the code below.

Output:

22.2

In this article, we will further discuss multiplication in Python.

Multiplying a string with an integer

If we multiply a string with an integer, it will duplicate that string by the number given.

See the code below.

Output:

aaaaa

However, if we have a string containing a number and wish to perform normal multiplication with it, we can typecast it to an integer using the int() function. After this, we can perform normal multiplication using the * operator.

For example,

Output:

50

Multiplying all the elements of a list

A list is used to store different elements under a common name. We can perform multiplication operations on a list also.

First, we will discuss different methods to multiply all the elements within a list.

Use the for loop to multiply all the elements of a list

We can iterate through the list using the for loop, multiply each element with one individually, and store the product in a separate variable.

For example,

Output:

70

In the above example, we initialized the variable c with the value 1. We multiplied this with every element of the list and displayed its final value.

Use the functools.reduce() function to multiply all the elements of a list

The functools.reduce() function is used to apply a given function to elements of the iterable. It takes a given number of elements at a time, stores their result, and applies the same function on this list for the remaining elements.

For example,

Output:

70

In the above example, we used the lambda keyword to create a single line function, which multiplies two elements from the list at a time.

In Python 3 and above, the reduce() function was shifted to the functools module. However, we do not need to import any module to use this function in Python 2. If we want to make something compatible with both the versions of Python, we can use the reduce() function from the six module, which is compatible with both Python 2 and Python 3.

Use the numpy.prod() function to multiply all the elements of a list

The numpy.prod() function returns the product of all the elements in an array over some specified axis. We can use this method for lists also.

For example,

Output:

70

Use the math.prod() function to multiply all the elements of a list

The math.prod() function works similar to the above method, but it works with all iterables. This is an addition to the recent versions of Python.

For example,

Output:

70

Multiply elements of a list with another number

We can also multiply all the elements of a list by a given number. We cannot directly use the number*list expression because it will not provide the required result and will duplicate the list the specified number of times, like a string.

Thus, we will discuss other methods on how to multiply all the elements of a list with another number.

Using the for loop to multiply the elements of a list with another number

We can iterate through the loop, multiply each element individually, and then store the result in a new list.

For example,

Output:

[35, 25, 10]

In the above example, we multiplied all the elements of the list by 5 and stored the new elements in a new list.

Using the list comprehension method to multiply the elements of a list with another number

We do the same thing in the previous example, but using list comprehension. This is a convenient and elegant way to create lists using a single line of code.

See the code below.

Output:

[35, 25, 10]

Using the map() function to multiply the elements of a list with another number

The map() function can apply a given function to all the elements of an iterable. It returns a map-type object in recent versions of Python.

To multiply the elements of a list with a number, we can use this method. We will provide the __mul__ function, which will multiply all the elements with the required number.

For example,

Output:

[35, 25, 10]

We use the list() function, in the end, to convert the map type object to a list.

Using the numpy.array() function to multiply the elements of a list to another number

If we multiply a number with an array, it will multiply all the elements of the array with it. The same does not happen with a list, as discussed earlier.

For example,

Output:

[35, 25, 10]

Let us discuss what happened in the above example.

  • We used the numpy.array() function to convert the list to an array.
  • We performed the necessary multiplication using the * operator.
  • Converted the result back to a list using the list() function.

Multiplying a list with another list

We can multiply every element of a list by the corresponding elements of another list. In our examples, we will assume both the list to be of the same length.

Using the for loop to multiply a list with another list

In this method, we will iterate through the length of the list, and multiply every element at the corresponding index. We will append the result to a new list.

For example,

Output:

[10, 3, 4]

We can use the list comprehension method to achieve the same in a single line of code.

See the code below.

Output:

[10, 3, 4]

Using the numpy module to multiply a list with another list

We can use two methods from the numpy module to achieve this. The first is by using the numpy.multiply() function. This function works with lists and arrays, and multiplies every element from one list with the corresponding element at the other list. The final result will be stored in an array.

For example,

Output:

[10 3 4]

We can also convert the lists to an array and multiply them. The numpy.array() function will convert them to an array, and then we can multiply them using the * operator.

For example,

Output:

[10 3 4]

We can convert the final type back to a list using the list() function in both of the above-mentioned methods.

Using the map() function to multiply a list with another list

As discussed earlier, the map() function can be used to apply a function to elements of an iterable. We can use it with two iterables and multiply their elements.

For example,

Output:

[10, 3, 4]

In the above example,

  • The operator.mul() function essentially multiplies two numbers.
  • This function is applied to the corresponding elements of both lists.
  • The list() function type-casts the final object to a list.

In this section, we discussed many methods on how to perform multiplication operations in a list. We can use most of these methods for other iterables. Some methods might be compatible with arrays, for others, we can convert them to a list using the tolist() function and use the above-mentioned methods.

That’s all about how to multiply in Python.


import_contacts

You may also like:

Related Posts

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • Count the number of characters in a String in Python
    13 January

    Count number of characters in a string in python

    Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • What is percent in python
    11 January

    What is % in Python?

    Table of ContentsWhat is % Operator in Python?The % Operator with strings in pythonThe % Operator as a placeholder for variables  in pythonThe % Operator as format specifiers in pythonConclusion In this article, we will cover what is % in Python and what are different ways to use % in Python. In python, there are […]

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusion Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to string in Python […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.