# Multiply in Python with Examples

In this Python tutorial, we will discuss how to multiply in python. Also, we will discuss:

• How to multiply numbers in Python
• How to multiply float numbers in Python
• How to multiply complex numbers in Python
• How to multiply string with an integer in python
• How to multiply numbers in a list Python
• How to multiply negative numbers in Python

In Python, the ( * ) operator is used to perform multiplication.

## How to multiply numbers in Python

In Python, use the asterisk “*” operator to multiply numbers.

``````# Assign the value 3 to the variable x
x = 3
# Assign the value 4 to the variable y
y = 4
# Multiply x and y and store the result in the variable 'result'
result = x * y
# Print the value stored in 'result'
print(result)
``````

The above code outputs the value 12.

## How to multiply float numbers in Python

In Python, use the asterisk “*” operator to multiply float numbers just like you would with integers.

``````# Assign the value 3.14 to the variable x
x = 3.14
# Assign the value 2.71 to the variable y
y = 2.71
# Multiply x and y and store the result in the variable 'result'
result = x * y
# Print the value stored in 'result'
print(result)
``````

The above code outputs 8.4894, which is the result of multiplying 3.14 and 2.71.

## How to multiply complex numbers in Python

Let us check all the methods to multiply complex numbers in Python.

### Method-1: Using the asterisk * operator

In Python, use the same asterisk “*” operator to multiply complex numbers.

• It’s important to note that the “*” operator uses the standard mathematical rules for multiplying complex numbers, so the order of the operands does not matter.
``````# Assign the value 3+4j to the variable x
x = 3 + 4j
# Assign the value 2+3j to the variable y
y = 2 + 3j
# Multiply x and y and store the result in the variable 'result'
result = x * y
# Print the value stored in 'result'
print(result)``````

The above code outputs (-6 + 17j), which is the result of multiplying (3 + 4j) and (2 + 3j).

### Method-2: Using the complex() function

Use the complex() method to convert a given string to a complex number and multiply two numbers, the complex number contains real and imaginary parts.

``````# Assign the string "3+4j" to the variable c
c = "3+4j"
# convert the string to a complex number and multiply it by 2
d = complex(c) * 2
# Print the value stored in d
print(d)
``````

The above code outputs (6+8j), which is the result of multiplying (3+4j) by 2.

## How to multiply string with an integer in python

In Python, use the asterisk “*” operator to multiply a string with an integer. It will repeat the string the number of times specified by the integer.

• It’s important to note that the string should be multiplied with an integer, if you try to multiply with any other data type it will raise a TypeError.
``````# Assign the string "Hello" to the variable x
x = "USA"
# Assign the value 3 to the variable y
y = 3
# Multiply x and y and store the result in the variable 'result'
result = x * y
# Print the value stored in 'result'
print(result)
``````

## How to multiply numbers in a list Python

There are multiple ways to multiply numbers in a list in Python.

### Method-1: Using the for loop

This method uses a for loop to iterate through the list of numbers, updating a result variable with the product of each number and the previous result.

``````# Using a for loop:
numbers = [1, 2, 3, 4, 5] # Initialize a list of numbers
result = 1 # Initialize a variable to store the result

# Iterate over the list of numbers
for number in numbers:
result *= number # Multiply the current number with the previous result

print(result) # Print the final result
``````

The above code creates a list of numbers and initializes a variable result to 1.

• Then, it uses a for loop to iterate over the list of numbers. Inside the loop, the result variable is multiplied by the current number in the list, so that on each iteration the product of the current number and all previous numbers are stored in the result.
• The final result will be the product of all the numbers in the list which is 120 in this case.

### Method-2: Using the reduce() function

The reduce() function is a powerful tool for reducing a list of elements to a single value. In this method, we use the reduce() function in combination with a lambda function to multiply the numbers in the list.

``````# Using the `reduce()` function from the `functools` module:
from functools import reduce # Import the reduce function
numbers = [1, 2, 3, 4, 5] # Initialize a list of numbers

# Use the reduce function to apply the lambda function to all elements in the list
result = reduce(lambda x, y: x * y, numbers)

print(result) # Print the final result``````

The above code creates a list of numbers and then uses the reduce() function from the functools module to apply a lambda function to all the elements in the list.

• The lambda function takes two arguments, x and y, and returns the result by multiplying them. The final result will be the product of all the numbers in the list which is 120 in this case.
• This method uses the reduce() the function which is a powerful tool for reducing a list of elements to a single value and is useful for functional programming and large lists.

### Method-3: Using List comprehension

List comprehension is a concise and readable way to create a new list from an existing one. In this method, we use list comprehension to create a new list with the product of each number and the initial result.

``````# Using List comprehension :
numbers = [1, 2, 3, 4, 5] # Initialize a list of numbers
result = 1 # Initialize a variable to store the result

# Use list comprehension to create a new list with the product of each number and the initial result
result = [result*i for i in numbers]

print(result[-1]) # Print the final result by accessing the last element of the final list``````

The above code creates a list of numbers and initializes a variable result to 1.

• Then, it uses list comprehension to iterate over the list of numbers and create a new list with the product of each number and the initial result.
• The final result is accessed by indexing the final list using [-1], which returns the last element of the list which is 120 in this case.

## How to multiply negative numbers in Python

One of the most basic ways to multiply negative numbers is to use the multiplication operator (*).

``````x = -2 # Assign the value -2 to variable x
y = -3 # Assign the value -3 to variable y
result = x * y # Multiply the values of x and y and store the result in variable result
print(result)``````

The above code assigns the value -2 to the variable x and -3 to the variable y.

• Then it multiplies the values of x and y using the multiplication operator (*) and assigns the result to the variable result.
• Finally, it prints the value of the result which is 6.

You may also like to read the following Python tutorials.

In this tutorial, we have covered how to multiply numbers in Python using the following topics.

• How to multiply numbers in Python
• How to multiply float numbers in Python
• How to multiply complex numbers in Python
• How to multiply string with an integer in python
• How to multiply numbers in a list Python
• How to multiply negative numbers in Python