How to Multiply Integers with Floating Point Numbers in Python

To multiply integers with floating point numbers in Python, we use the multiplication operator (*). If the multiplication operator (*) appears between a number of type int and of type float or vice versa, then the numbers will multiply each other. The product of an int and a float is a float.

If you want to learn more about how to multiply float by int in Python, then stick around because this post is going to exhaust everything you need to know about the subject. Let’s get started.

Table of Contents

Multiply int by float Python

In Python, integers and floating point numbers, although they are both numbers, are of two separate types. Passing each of these in the type() function will yield a different type:

>>> type(5)
<class 'int'>
>>> type(7.8)
<class 'float'>
>>> type(5.0)
<class 'float'>

What’s interesting from the scenario above is that 5 is an integer while 5.0 is a float. Mathematically, we know that 5 and 5.0 are equivalent, yet Python will often handle them differently due to the fact that they are different types of numbers.

Since they are of different types, one might ask: can you then multiply a float by an int in Python? The answer is YES, you can multiply an int by a float in Python or vice versa.

Below we will look at different case scenarios where the multiplication of integers and floats can be done.

>>> 5 * 5.0
25.0
>>> 4.3 * 5
21.5

Just like with the multiplication of floats and integers, any other multiplication of float to another number type will produce a product of float type.

int_num = input('Enter a number: ')
float_num = 4.3

product = int(int_num) * float_num
print(f'The product of {int_num} and {float_num} is {product}')
> Enter a number: 5
The product of 5 and 4.3 is 21.5

We get the string ‘5’ from the input and we change it into an integer using the int() function. Then we multiply the integer by a float 4.3. The product is 21.5.

Multiply int by float Python: Vocabulary

Below are basic terms we have to understand in order get to multiply int by float in Python. If you already know about them, you can skip to the part where we multiply int by float using Python.

What Is An Integer in Python

To put it simply, integers are a number type that only includes whole values and no decimal places. If you pass integers through the type() function, they should return a <class 'int'> meaning that they are of type int which is short for integer. Integers, in Python, are the most commonly used number type. If integers multiply each other, the product is always going to be an integer. But if Integers multiply floats, the product is always a float.

What Is A Floating Point Number in Python

A float, or a floating point number, is a number type with decimals. If you pass floating point numbers through the type() function, they should return a <class 'float'>. The number 2.0 is treated in Python as a float just because it has a decimal, although mathematically we know that it evaluates to the integer 2.

Multiplication operator in Python

The multiplication operator in Python is an asterisk (*). It works between numbers of any type. To learn more about the operator, check out CodingGear’s post on How to multiply in Python.

Questions On How to Multiply int by float Python

Why Is the product of an int and a float always a float?

We always get a float when we multiply an int and a float in Python to increase the accuracy of our calculated result. Changing float into int trims off all the decimals, which are also part of the number, so if we do that, the product will be a shortened version of the answer we should be getting.

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 105