In this python tutorial, you will learn how to Multiply two floating point numbers using the arithmetic operator along with other functions like the input function, format function, float function and print function of the python programming language.
How to Multiply Two Floating Point Numbers in Python?
Let’s take a look at the first source code , here the values are assigned in the code and the multiplication operator carries out the function.
RUN CODE SNIPPET#Program to multiply two float numbers num1 = 8.2 num2 = 2.4 #Multiplying two float numbers product = float(num1)*float(num2) #Displaying the output value print("The product of {0} and {1} is {2}".format(num1, num2, product))
OUTPUT:
The product of 8.2 and 2.4 is 19.679999999999996
- At the start, we are declaring two variables
num1
andnum2
and assigning the values as8.2
and2.4
respectively. - We multiply two numbers
num1
andnum2
using the arithmetic operation*
, we declare the values with thefloat
function which converts a number stored in a string or integer into a floating point number, that is a number with a decimal point. We store the value in the variableproduct
. - Now, we display the output value using the
print
and display the statement/string"The product of {0} and {1} is {2}". format(num1, num2, product)
. - In the above statement/string, the variables
{0}
,{1}
and{2}
will hold the valuesnum1
,num2
andproduct
where theformat
function helps in variable substitution and data formatting.
Let’s take a look at the second source code , here the values are given as input by the user in the code and the multiplication operator carries out the function.
RUN CODE SNIPPET#Program to multiply two float numbers num1 = input('\n Enter the first number: ') num2 = input('\n Enter the second number: ') #Multiplying two float numbers product = float(num1)*float(num2) #Displaying the output value print("\n The product of {0} and {1} is {2}".format(num1, num2, product))
INPUT:
8.2 2.4
OUTPUT:
Enter the first number: Enter the second number: The product of 8.2 and 2.4 is 19.679999999999996
- Here we give the user the option to enter the values and the input values are scanned using the
input
function and are stored in two variables namelynum1
andnum2
with the statements/stringsEnter First number:
andEnter Second number:
with\n
respectively. - In the STDIN section of the code editor the input values are entered.
- When multiplying the two numbers
num1
andnum2
using the arithmetic operation*
, we declare the input values with thefloat
function and store the value in the variableproduct
. - As given in the first program, the variables
{0}
,{1}
and{2}
will hold the valuesnum1
,num2
andproduct
where theformat
function helps in variable substitution and data formatting.
NOTE:
- The Multiplication operator (*) is used to multiply integers in a program and the final value is stored in a variable named as product.
- The variables used by the format function for substitution is enclosed in curly braces.
- The print statement is followed by a period, to initiate the format function.
- The print statement/string to be displayed in enclosed in double quotes.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.