Which of the following operators is used for performing exponentiation A B * C D?

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. Python follows the same precedence rules for its mathematical operators that mathematics does.

  1. Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even though it doesn’t change the result.

  2. Exponentiation has the next highest precedence, so 2**1+1 is 3 and not 4, and 3*1**3 is 3 and not 27. Can you explain why?

  3. Multiplication and both division operators have the same precedence, which is higher than addition and subtraction, which also have the same precedence. So 2*3-1 yields 5 rather than 4, and 5-2*2 is 1, not 6.

  4. Operators with the same precedence (except for **) are evaluated from left-to-right. In algebra we say they are left-associative. So in the expression 6-3+2, the subtraction happens first, yielding 3. We then add 2 to get the result 5. If the operations had been evaluated from right to left, the result would have been 6-(3+2), which is 1.

Note

An exception to the left-to-right left-associative rule is the exponentiation operator **. A useful hint is to always use parentheses to force exactly the order you want when exponentiation is involved:

See Operator precedence table for all the operators introduced in this book. You will also see many upcoming non-mathematical Python operators.

Check your understanding

    What is the value of the following expression:

  • 14
  • Using parentheses, the expression is evaluated as (2*5) first, then (10 // 3), then (16-3), and then (13+1).
  • 24
  • Remember that * has precedence over -.
  • 3
  • Remember that // has precedence over -.
  • 13.667
  • Remember that // does integer division.

    What is the value of the following expression:

  • 768
  • Exponentiation has precedence over multiplication, but its precedence goes from right to left! So 2 ** 3 is 8, 2 ** 8 is 256 and 256 * 3 is 768.
  • 128
  • Exponentiation (**) is processed right to left, so take 2 ** 3 first.
  • 12
  • There are two exponentiations.
  • 256
  • Remember to multiply by 3.

Here are animations for the above expressions:

You have attempted of activities on this page

Click below to download a Maple V worksheet. You can look at the appended non-interactive HTML version of the worksheet to learn what the worksheet covers.
  • Maple V Release 4 version

  • Maple V Release 3 version

This woksheet is designed to accompany Chapter 3 of Introduction to Scientific Programming: Computational Problem Solving Using Maple and C by Joseph L. Zachary. In it, we will use Maple to explore the concept of operator precedence. (AW, Jan 97)

Multiplication/Division vs. Addition/Subtraction

Examine the Maple expression below. Try to predict what its value will be before you ask Maple to evaluate it. There seem to be two possibilities. If the addition is performed first, the result will be 4/2, which is 2. If the division is performed first the result will be 5/2. Which is it? Try it out and see.

> 1+3/2;

The second possibility was correct: the division was performed first. We say that division takes precedence over addition.

Now consider this expression. What are two reasonable possible values? Which do you think Maple will find?

> 2-3*4;

Here, the multiplication was performed prior to the subtraction. (Had the subtraction been performed first, the answer would have been -4.) We say that multiplication takes precedence over addition.

Now try the following experiments:

  • In the first expression above, replace the + operator with a - operator and reevaluate. Which operator takes precedence, subtraction or division?
  • In the second expression above, replace the - operator with a + operator and reevaluate. Which operator takes precedence, addition or multiplication?

Your experiments should reveal that in Maple (as in most other programming languages) multiplication and division take precedence over addition and subtraction. We say that multiplication and division are at a higher level of precedence than are addition and subtraction.

Multiplication vs. Division and Addition vs. Subtraction

What happens when an expression involves more than operator at the same level of precedence? (For example, more than one multiplication and division or more than one addition and subtraction.)

What would be two possible values for this expression?

> 2/3*4;

The division was performed first (to obtain 2/3), and the result was then multiplied by 4 (to obtain 8/3). Had the multiplication been performed before the division, the result would have been 1/6.

This does not mean that multiplication takes precedence over division, however. In the absence of parentheses, multiplication and division are performed left to right. We say that multiplication and division are left associative.

Try to predict the outcome of each of the following calculations before you try it. If your prediction turns out to be incorrect, be sure to figure out where the flaw in your reasoning occurred.

> 2*3/4;

> 2/3/4;

> 4/3*2/5;

> 6*3/4*5;

Addition and subtraction are also left associative. In the absence of parentheses, addition and multiplication is performed from left to right. In rational arithmetic (at least in the absence of overlow) this does not make any difference, but in floating-point arithmetic it can become important.

The following two expressions are mathematically identical; they differ only in the fact that their last two terms have been interchanged. Evaluate them.

> 1 - 1 + 1e-12;

> 1 + 1e-12 - 1;

The answers are not the same! Why?

In the first expression, the subtraction is performed first and the result is zero. When zero is added to the number 1e-12, we end up with the result 1e-12.

In the second expression, the addition is performed first. With only ten digits of mantissa, however, the number 1e-12 is the lost in the roundoff and the result is 1. The subtraction thus yields a result of zero. (This example shows that floating-point arithmetic need not satisfy the associative rule.)

Exponentiation

What about the precedence of the exponentiation operator? See if you can figure out the rule by evaluating the following expressions:

> 4*3^2;

> 1+3^2;

> 5/2^3;

> 3/10^3*7;

Notice that in each of these examples, the exponentiation was performed first. This is indeed the way that it works: exponentiation has higher precedence than multiplication and division, which in turn have higher precedence than addition and multiplication.

Now try this expression.

> 2^3^4;

The exponentiation operator is non-associative. If you wish to do more than one exponentiation in an expression, you must use parentheses to tell Maple whether you want

> (2^3)^4;

or

> 2^(3^4);

Parentheses

Speaking of parentheses, you can override Maple's precedence rules by using parentheses to group expressions in the order that you wish Maple to evaluate them. Here are some examples where we use parentheses to force a different evaluation order than Maple would otherwise use.

> 2*(3+4);

> 2^(3*4);

> 2/(3/3);

If you have any doubt, use parentheses to make usre Maple executes your expressions in the order in which you intend.

Summary

  • There are five arithmetic operators: ^, *, /, +, and -.
  • Exponentiation (^) is at the highest level of precedence, multiplication (*) and division (/) are at a lower level of precedence, and addition (+) and subtraction (-) are at an even lower level of precedence.
  • In the absence of parentheses, operators at a higher level of precedence are performed before operators at a lower level of precedence.
  • In the absence of parentheses, multiplication and division are performed from left to right, as are addition and subtraction.
  • Parentheses can be used to explicitly control the order of evaluation of an expression.

Exercises

1. Evaluate the following expression.

> 2 - 2^3*2;

Add one set of parentheses so that the expression evalutes to -12. Now move your parentheses so that the expression evaluates to -62. Move your parentheses one last time to make the expression evaluate to 0.

2. Evaluate the following expression.

> 2/3*4^2;

Add two sets of parentheses that leave the value of the expression unchanged. (Your parentheses may enclose neither the entire expression nor a single number.

3. Repeat exercise 2 for this expression.

> 1+2/3*4+5;


Which of the following operators is used for performing exponentiation?

Exponentiation uses the ^ Operator, as the following example demonstrates.

Which has more precedence * or?

Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Which automatic operator is used for exponentiation?

*) for multiplication, (./) for division, and (. ^) for exponentiation.

Which of the following has the highest precedence A addition B negation C exponentiation D division?

We say that multiplication and division are at a higher level of precedence than are addition and subtraction.