Practice With Arithmetic Operators
Practice these programming examples to internalize these concepts.
5. Multiplication and Division
Like addition and subtraction, multiplication and division will look very similar to how they do in mathematics. The sign we'll use in Python for multiplication is *
and the sign we'll use for division is /
.
Here's an example of doing multiplication in Python with two float values:
k = 100.1 l = 10.1 print(k * l)
Output:
1011.0099999999999
When you divide in Python 3, your quotient will always be returned as a float, even if you use two integers:
m = 80 n = 5 print(m / n)
Output:
16.0
This is one of the major changes between Python 2 and Python 3. Python 3's approach provides a fractional answer so that when you use /
to divide 11
by 2
the quotient of 5.5
will be returned. In Python 2 the quotient returned for the expression 11 / 2
is 5
.
Python 2's /
operator performs floor division, where for the quotient x
the number returned is the largest integer less than or equal to x
. If you run the above example of print(80 / 5)
with Python 2 instead of Python 3, you'll receive 16
as the output without the decimal place.
In Python 3, you can use //
to perform floor division. The expression 100 // 40
will return the value of 2
. Floor division is useful when you need a quotient to be in whole numbers.