To multiply two numbers in Python, you can use the multiplication operator *. You can multiply integers, floats, and decimal variables.

a = 1
b = 2

c = a * b

print(c)

#Output:
2

One of the most fundamental operations in programming is performing different calculations and math.

You can easily multiply two numbers in Python with the * multiplication operator.

You can multiply integers, floats, and decimal variables in Python with *.

The rest of this article will show you examples of how you can use * to multiply two numbers in Python.

How to Multiply Two Integers in Python

The most basic use of * is if you have two integers and you want to multiply them together.

Below shows you a simple example of using / to multiply two integers in Python.

a = 1
b = 2

c = a * b

print(c)

#Output:
3

How to Multiply Two Floats in Python

Another type of number that you can use in Python is a floating point number. Floats allow you to create numbers which have a decimal portion.

Below shows you a simple example of using * to multiply two floats in Python.

a = 1.0
b = 2.0

c = a * b

print(c)

#Output:
2.0

How to Multiply a Float and an Integer in Python

You can multiply a float and an integer together with *. The result will be a float.

Below shows you how to multiply a float and an integer together in Python.

a = 1
b = 2.0

c = a * b

print(c)

#Output:
2.0

How to Multiply Two Decimals in Python

Another type of number that you can use in Python is a decimal. The decimal module provides support for fast correctly rounded decimal floating point arithmetic.

Below shows you a simple example of using * to multiply two decimals in Python.

from decimal import *

a = Decimal('1.01')
b = Decimal('2.02')

c = a * b

print(c)

#Output:
2.0402

Hopefully this article has been useful for you to learn how to multiply two numbers in Python.

Categorized in:

Python,

Last Update: March 11, 2024