Multiply in Python with Examples

By: On:

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
  • Multiply two numbers using the function in python
  • Multiply two lists python

Multiply in Python

Now, we will discuss how to multiply in Python. We will see how to multiply float numbers, multiply complex numbers, multiply string with an integer and Multiply two numbers using the function in python.


How to multiply numbers in Python

In python, to multiply number, we will use the asterisk character ” * ” to multiply number.

Example:

number = 20 * 3
print('The product is: ',number)

After writing the above code (how to multiply numbers in Python), Ones you will print “ number ” then the output will appear as a “ The product is: 60 ”. Here, the asterisk character is used to multiply the number.

You can refer to the below screenshot to multiply numbers in python.


How to multiply numbers in Python
How to multiply numbers in Python

This is how we can multiply numbers in python.

How to multiply float numbers in Python

In python, we can also multiply one or both numbers using asterisk character ” * “ when it is of float type, then the product is float number.

Example:

number = 2.0 * 3.0
print('The product is: ',number)

After writing the above code (how to multiply float numbers in Python), Ones you will print “ number ” then the output will appear as a “ The product is: 6.0 ”. Here, the asterisk character is used to multiply the float number.

You can refer to the below screenshot to multiply float numbers in python.

How to multiply float numbers in Python
How to multiply float numbers in Python

This is how we can multiply float numbers in python.

How to multiply complex numbers in Python

In python, to multiply complex numbers, we use complex() method to multiply two numbers and the complex number contains real and imaginary parts. Here, we multiply each term with the first number by each in the second.

Example:

num1 = complex(2, 3)
num2 = complex(4, 6)
product = num1 * num2
print('The product of complex number is: ', product)

After writing the above code (how to multiply complex numbers in Python), Ones you will print “ product ” then the output will appear as a “ The product of complex number is: (-10+24j) ”. Here, the complex() is used to multiply the complex number.

You can refer to the below screenshot to multiply complex numbers in python.

How to multiply complex numbers in Python
How to multiply complex numbers in Python

This is how we can multiply complex numbers in python

How to multiply string with an integer in python

In python, to multiply string with an integer in Python, we use a def function with parameters and it will duplicate the string n times.

Example:

def row(s, n):
return s * n
print(row('Hello all   ', 5))

After writing the above code (how to multiply string with an integer in python), Ones you will print then the output will appear as a “ Hello all Hello all Hello all Hello all Hello all ”. Here, n is 5, and s is “ Hello all “ and it will return duplicate string 5 times.

You can refer to the below screenshot to multiply string with an integer in python.

How to multiply string with an integer in python
How to multiply string with an integer in python

This is how we can multiply string with an integer in python.

Multiply two numbers using the function in python

In python, to multiply two numbers by using a function called def, it can take two parameters and the return will give the value of the two numbers.

Example:

def multiply(x,y):
return x*y;
num1=15
num2=5
print("The product is: ",multiply(num1,num2))

After writing the above code (multiply two numbers using the function in python), Ones you will print then the output will appear as a “ The product is: 75 ”. Here, we define the function for multiplication, and then it will return the value.

You can refer to the below screenshot to multiply two numbers using the function in python

Multiply two numbers using the function in python
Multiply two numbers using the function in python

This is how we can multiply two numbers using the function in python.

Multiply two lists python

In python, to multiply two equal length lists we will use zip() to get the list and it will multiply together and then it will be appended to a new list.

Example:

my_list1 = [5, 2, 3]
my_list2 = [1, 5, 4]
multiply = []
for number1, number2 in zip(my_list1, my_list2):
multiply.append(number1 * number2)
print(multiply)

After writing the above code (multiply two lists in python), Ones you will print “multiply” then the output will appear as a “ 5 10 12 ”. Here, we multiply each element from one list by the element in the other list.

You can refer to the below screenshot to multiply two list in python

Multiply two lists python
Multiply two lists python

This is how we can multiply two lists in python.


You may like following Python tutorials:

In this tutorial, we learned how to multiply in Python.

  • 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
  • Multiply two numbers using the function in python
  • Multiply two lists python

Leave a Comment