How to Multiply in Python

Reading Time: 3 Minutes
Publication date:

Arithmetic Operations

Python supports all standard arithmetic operations:

Addition

To add two numbers:

print(6 + 2)  # Output: 8

Subtraction

To subtract one number from another:

print(6 - 2)  # Output: 4

Multiplication

To multiply two numbers:

print(6 * 2)  # Output: 12

Division

To divide one number by another:

print(6 / 2)  # Output: 3.0

Floor Division

To get the integer division of two numbers:

print(7 / 2)  # Output: 3.5
print(7 // 2)  # Output: 3

This operation gives the integer result of the division, discarding the fractional part.

Exponentiation

To raise a number to the power of another:

print(6 ** 2)  # Output: 36 (6 raised to the power of 2)

Modulus

To find the remainder of the division:

print(7 % 2)  # Output: 1 (remainder of 7 divided by 2)

In this case, the nearest number to 7 that is divisible by 2 is 6. Thus, the remainder is 7 – 6 = 1.

When multiple arithmetic operations are used sequentially, they are executed according to their precedence. Higher precedence operations are executed first. The table below shows the order of precedence from lowest to highest:

Operation Precedence

OperationDirection
**Right to Left
/ // %Left to Right
+ –Left to Right

Consider the following expression:

number = 3 + 4 * 5 ** 2 + 7
print(number)  # Output: 110

Here, the exponentiation (5 ** 2) is executed first, followed by multiplication (25 * 4), then addition (3 + 100), and finally, another addition (103 + 7).

To override the order of operations, you can use parentheses:

number = (3 + 4) * (5 ** 2 + 7)
print(number)  # Output: 224

It’s important to note that arithmetic operations can involve both integers and floating-point numbers. If an operation involves both an integer (int) and a floating-point (float) number, the integer is converted to a float.

Arithmetic Operations with Assignment

Several special operations allow you to assign the result of an operation to the first operand:

  • += Assign the sum
  • -= Assign the difference
  • *= Assign the product
  • /= Assign the quotient
  • //= Assign the integer quotient
  • **= Assign the exponentiation result
  • %= Assign the remainder

Examples:

number = 10
number += 5
print(number)  # Output: 15

number -= 3
print(number)  # Output: 12

number *= 4
print(number)  # Output: 48

Rounding and the round() Function

When working with floating-point numbers, keep in mind that the results of operations may not be perfectly accurate. For example:

first_number = 2.0001
second_number = 5
third_number = first_number / second_number
print(third_number)  # Output: 0.40002000000000004

Here, we might expect 0.40002, but we get a slightly different result due to floating-point precision issues.

Or another example:

print(2.0001 + 0.1)  # Output: 2.1001000000000003

In the cases above, you can use the round() function to round the result:

first_number = 2.0001
second_number = 0.1
third_number = first_number + second_number
print(round(third_number))  # Output: 2

The round() function takes the number to be rounded. If only the number is given, it is rounded to the nearest integer.

The round() function can also take a second argument, specifying the number of decimal places to include in the result:

first_number = 2.0001
second_number = 0.1
third_number = first_number + second_number
print(round(third_number, 4))  # Output: 2.1001

Here, the value of third_number is rounded to 4 decimal places.

If only one value is provided to the function – just the number to be rounded, it rounds to the nearest integer:

Examples of Rounding

# rounding to a whole number
print(round(2.49))  # Output: 2
print(round(2.51))  # Output: 3

However, if the fraction to be rounded is equidistant between two integers, the rounding goes to the nearest even number:

print(round(2.5))  # Output: 2 (nearest even)
print(round(3.5))  # Output: 4 (nearest even)

Rounding is performed to the nearest power of 10 minus the fraction to be rounded:

# rounding to two decimal places
print(round(2.554, 2))      # Output: 2.55
print(round(2.5551, 2))     # Output: 2.56
print(round(2.554999, 2))   # Output: 2.55
print(round(2.499, 2))      # Output: 2.5

However, it should be noted that the round() function is not a perfect tool. For example, when rounding to integers, the rule applied is that if the fraction to be rounded is equidistant from two values, the rounding goes to the nearest even value. In Python, due to the fact that the fractional part cannot be accurately represented as a floating-point number, some expected results may not be achieved. For example:

# round to two decimal places
print(round(2.545, 2))   # Output: 2.54
print(round(2.555, 2))   # Output: 2.56 (round to even)
print(round(2.565, 2))   # Output: 2.56
print(round(2.575, 2))   # Output: 2.58

print(round(2.655, 2))   # Output: 2.65 (not rounding to even)
print(round(2.665, 2))   # Output: 2.67
print(round(2.675, 2))   # Output: 2.67

5 / 5. 1

Share:

Leave a comment