Nimfa Placer
last year
Nimfacountry asked

The result of 2 * 5 - 10 / 5 is 8.0. How was this evaluated?

Kelish Rai
Expert
last year
Kelish Rai answered

If you're familiar with PEMDAS, the expression 2 * 5 - 10 / 5 follows the same order of operations.

Here’s a quick breakdown of PEMDAS:

  1. Parentheses first (none here).

  2. Exponents next (none here).

  3. Multiplication and Division, from left to right.

  4. Addition and Subtraction, from left to right.

Now, let’s see how Python evaluates the expression step by step:

  1. 2 * 5 = 10, so the expression becomes 10 - 10 / 5.

  2. 10 / 5 = 2.0, so now we have 10 - 2.0.

  3. 10 - 2.0 = 8.0.

Since division in Python always produces a float, the final result is 8.0 instead of an integer.

However, if you want the result without decimals, you can use // for integer division.

result = 2 * 5 - 10 // 5
print(result)  # Output: 8

In this case, the result would be an integer (8), not a float (8.0).

Python
This question was asked as part of the Getting started with Python course.