Python has a variety of operators to perform various arithmetic operations like addition, subtraction, multiplication, division, modulus and exponentiation. Here we will discuss the multiplication operator in Python with some code snippets
The multiplication operator (*) in Python performs multiplication of 2 numbers. The numbers can be of the type integer, floating point or complex. Let’s see some examples and understand the multiplication operator.
Multiplication of 2 Integer numbers
In below code the multiplication operator is used to perform multiplication of 2 integers, the product too is a integer value.
a=10
b=20
c=a*b
print("The product is ",c) #200
print(type(c)) #int
Multiplication of 2 Floating Point numbers
In below code the multiplication operator is used to perform multiplication of 2 floating point numbers, the product too is a floating point value.
a=3.6
b=4.5
c=a*b
print("The product is ",c) #16.2
print(type(c)) #float
Multiplication of an integer number and a floating point number
In below code the multiplication operator is used to perform multiplication of an integer number and a floating point number, the product is a floating point value.
a=3
b=4.5
c=a*b
print("The product is ",c) #13.5
print(type(c)) #float
Multiplication of 2 complex numbers
In below code the multiplication operator is used to perform multiplication of 2 complex numbers, the product too is a complex number.
a=3+5j
b=4+6j
c=a*b
print("The product is ",c) #-18+38j
print(type(c)) #complex
Multiplication of an integer number and a complex number
In below code the multiplication operator is used to perform multiplication of an integer number and a complex number, the product is a complex number.
a=3
b=4+6j
c=a*b
print("The product is ",c) #12+18j
print(type(c)) #complex
Multiplication of a floating point number and a complex number
In below code the multiplication operator is used to perform multiplication of a floating point number and a complex number, the product is a complex number.
a=3.5
b=4+6j
c=a*b
print("The product is ",c) #14+21j
print(type(c)) #complex
Multiplication of a string and an integer
With string and an integer (n), the multiplication operator works as a repetition operator and produces a string result which contains the operand string repeated n times.
a=3
b="Python."
c=a*b
print("The product is ",c) #Python.Python.Python.
print(type(c)) #str
However, note that using multiplication operator between a float and a string will generate a TypeError. Similarly, using multiplication operator between two strings will also generate a TypeError.
Multiplication of 2 boolean values
With boolean variables, the multiplication operator works as in integer values, treating True as integer value 1 and False as integer value 0. The product is a int.
a=True
b=False
c=a*b
print("The product is ",c) #0
print(type(c)) #int