Multiplying and Dividing Numbers in Python

Have you ever multiplied or divided numbers in other coding languages? If yes, then you’ll discover the process for Multiplying and Dividing Numbers in Python is very similar, if not pretty much exactly the same. Knowing how to multiply and divide numbers using Python is necessary not just to know the answer to something like 10 times 5, but because you can use certain operations in any of your complex code to manage other functionalities.

In this Multiplying Dividing Numbers Python Tutorial, you’ll learn the following stuff: 

How to do Multiplying Numbers using Python?

To multiply two numbers by each other, you’ll use the “*” operator, like this:

2*4
5*10
3*7

That’s just about all it takes to perform some multiplication. Don’t forget that if you want to print the results of the numbers you’re multiplying, you’ll have to use the print command, like this:

print(2*4)
print(5*10)
print(3*7)

The output of the code above would be:

8
50
21

Do Check: Using Exponents Python

Python program to multiply floating-point number

#Python program to multiply two numbers using function
def mul_Num(a,b):#function for multiplication
    multiply=a*b;
    return multiply; #return value
num1=2.456 #variable declaration
num2=55.54
print("The product is",mul_Num(num1,num2))#call the function

Output:

The product is 136.40624

How to perform Dividing numbers in Python?

For dividing two numbers, the syntax is similar to the one you’d use to multiply numbers, but instead of using the * operator, you use the “/” operator. Just like when dividing numbers using any other programming language (or even using a calculator, for that matter), put the number you want to be divided in front of the slash, and the number you want to divide it by after the slash.

Here’s how your division should look:

8/2
16/8
20/4

And if you want it printed, do the same as we did to the multiplication equations above:

print(8/2)
print(16/8)
print(20/4)

The output for the code above should be this:

4
2
5

Presently you have understood the basics of multiplication and division in Python, you can apply the concept in your more complicated functions, use it in your loops, and see just how much you can do with a simple operator like * or /. If you want to learn how to use another operator to calculate the remainder of when one number is divided by another, check out our tutorial on how to use the modulo operator.

Program to perform Multiplication and Division on two numbers in Python

# Python program to perform Multiplication and Division of two numbers

num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))

print("Enter which operation would you like to perform?")
ch = input("Enter any of these char for specific operation +,-,*,/: ")

result = 0
if ch == '*':
    result = num1 * num2
elif ch == '/':
    result = num1 / num2
else:
    print("Input character is not recognized!")

print(num1, ch , num2, ":", result)

Output: 

Enter the first number: 12
Enter the second number: 4
Enter the operator you want to perform
Enter any of these operator for operation +, -, *, / *
12 * 4 : 48
12 / 4 : 3.0

Python program for multiplication and division of complex number

Multiplication Example Code: 

# Python program to demonstrate
# multiplication of complex numbers
  
  
def mulComplex( z1, z2):
    return z1*z2
   
      
# driver code
z1 = complex(2, 3)
z2 = complex(4, 5)
  
  
print("Multiplication is :", mulComplex(z1,z2))

Output: 

Multiplication is : (-7+22j)

Division Example Code: 

# Python program to demonstrate
# division of complex numbers
  
  
def divComplex( z1, z2):
    return z1 / z2
  
# driver code
  
z1 = complex(2, 3)
z2 = complex(4, 5)
  
print( "Division is :", (divComplex(z1, z2))

Output:

Division is : (0.5609756097560976+0.0487804878048781j)

Leave a Reply

Your email address will not be published.