Multiplication of Two Numbers in Python

(Last Updated On: 26/09/2023)

Welcome to our tutorial on multiplication of two numbers in Python! Python is one of the popular, high-level programming languages that is widely used for a variety of applications, including data analysis, machine learning, and web development.

Multiplication is one of the basic mathematical functions that we learn in our school and use in our day-to-day life. In this tutorial, we will be using the numbers 15 and 9 as examples to demonstrate how to perform the multiplication of two numbers in Python.

Python Program to Multiply Two Numbers

The most straightforward way of multiplication of two numbers in Python is to use the ‘*‘ operator. For example, to multiply 15 by 9, we can write the following code:

# Multiplication of Two Numbers in Python By Taking User Inputs

# We are taking a number from user as input
# Entered value will be converted to int from string
num1 = int(input("Enter the First Number: "))
num2 = int(input("Enter the Second Number: "))

output = num1 * num2

# Display the Multiplication
print("Multiplication of Two Numbers using integer",output)

Output:

This will output the result of 135.

Enter the First Number: 15
Enter the Second Number: 9
Multiplication of Two Numbers using integer 135

It’s also possible to perform the multiplication of two numbers with variables, like x and y, instead of num1 and num2 to get the same output. This will also output the result of 135.

When working with the multiplication of two numbers in Python, it’s important to keep in mind the types of the numbers involved. If one of the numbers is a float and the other is an integer, the result will be a float. If both numbers are integers, the result will also be an integer.

In conclusion, the multiplication of two numbers in Python is a simple and straightforward operation that can be performed using the ‘*’ operator. With the example of 15 and 9, we saw how easy it is to perform multiplication of two numbers in Python, and also the importance of keeping in mind the types of the numbers involved. By understanding this basic concept, you’ll be well on your way to using Python for more advanced mathematical operations and other applications.

Python Program to Multiply Two Numbers Using Function

We earlier learned to multiply two numbers in Python. Now we will learn the code for multiplication of two numbers using a function.

# Multiplication of Two Numbers in Python Using Function

 # defined a function multiplication to multiply two numbers
def multiplication(x, y):
	z = x * y
	return z

# we are taking a number from user as input
# entered value will be converted to int from string
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

result = multiplication(a, b)
print("Multiplication of two numbers:", result)

Let’s take a closer look at each line of code and understand how it works.

The first line defines a function named multiplication that takes two arguments, x and y. The purpose of this function is for multiplication of two numbers using the function and return the result.

The second line of the function calculates the result of the multiplication by multiplying x and y and storing the result in a variable named z. The third line returns the value of z.

Next, we take two numbers as input from the user. The input function returns the user’s input as a string, so we use the int function to convert it to an integer.

Finally, we call the multiplication function and pass a and b as arguments. The function returns the result of the multiplication, which is stored in the result variable.

We then use the print function to display the result of the multiplication. The output will look like this:

Output:

Enter first number: 12 Enter second number: 8 Multiplication of two numbers using a function: 96

As you can see, the function works as expected and returns the correct result of the multiplication.

Enter first number: 12
Enter second number: 8
Multiplication of two numbers: 96

This example demonstrates how to multiply two numbers in Python using a function. By creating a function, we can reuse the code whenever we need to perform the multiplication of two numbers in our program. This makes our code more reusable, maintainable, and easier to read.

In conclusion, using functions is an important concept in Python programming, and it makes your code easier to manage and maintain. Whether you’re a beginner or an experienced programmer, understanding how to multiply two numbers in Python using a function is a valuable skill. Try running this code yourself, and see how it works!

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author

Leave a Reply