Concepts / Operators and Expressions

Operators and Expressions

An operator indicates the operation being used, while operands are the values that the operator acts on.

  • Programming

Reading a Line of Code

Most statements, or logical lines, that you write will contain expressions. An expression is a part of a statement that can be understood by breaking it into operators and operands. The simple expression 2 + 3 gives us a clear starting point: the numbers are the operands, and the plus sign is the operator.

Key point

An operator indicates the operation being used, while operands are the values that the operator acts on.

Expression Parts

2operand+operator3operandused byuses
Which part of 2 + 3 is the operator, and which parts are the operands?

In 2 + 3, the plus sign is the operator. The values 2 and 3 are the operands. Together, the operator and its operands form the expression 2 + 3. This way of reading an expression helps you separate the values involved from the operation connecting them.

Worked example

Decomposing a Simple Expression

Identify the operator and operands in 2 + 3.

  1. 1
    Find the operator

    The plus sign indicates the operation in the expression.

  2. 2
    Find the operands

    The values on either side of the plus sign, 2 and 3, are the operands.

  3. 3
    Reassemble the expression

    The operator and operands together make the complete expression 2 + 3.

Result

Operator: +. Operands: 2 and 3. Complete expression: 2 + 3.

Choosing the First Operation

An expression can contain more than one operator. For example, 2 + 3 * 4 contains both addition and multiplication. The operations are not treated as if their symbols appeared in an arbitrary order. Multiplication has higher precedence than addition, so Python evaluates the multiplication before the addition.

2 + 3 * 4addition and multiplication3 * 4higher precedence2 + resultaddition aftermultiplicationevaluate firstthen evaluate
Which operation is considered first when addition and multiplication appear in the same expression?

What do you think happens?

In 2 + 3 * 4, which operator has higher precedence?

  • Addition

  • Multiplication

  • They have the same precedence

Reveal answer
Answer:

Multiplication

Multiplication has higher precedence than addition, so Python evaluates the multiplication before the addition.

Precedence describes how strongly an operator binds in an expression. The source describes Python's precedence table as running from the lowest precedence, which is the least binding, to the highest precedence, which is the most binding. In a given expression, Python evaluates operators and expressions according to their precedence. For 2 + 3 * 4, multiplication is higher in precedence than addition, so multiplication comes first.

Mistakes with Expression Order

  • Common mistake

    Treating every operator as if it had the same precedence.

    What it looks like

    Reading 2 + 3 * 4 as though addition and multiplication must be handled by the same rule.

    Why it fails

    The source states that multiplication has higher precedence than addition.

    Do this instead

    Identify the operators and compare their precedence before deciding which operation is evaluated first.

  • Common mistake

    Assuming the first operator written must be evaluated first.

    What it looks like

    Choosing the plus sign before considering the multiplication sign in 2 + 3 * 4.

    Why it fails

    The position of an operator in the written expression does not override precedence.

    Do this instead

    Evaluate the multiplication operator first because it has higher precedence than addition.

  • Common mistake

    Calling the entire expression an operator.

    What it looks like

    Describing 2 + 3 as the operator instead of separating its parts.

    Why it fails

    An expression can be broken down into operators and operands; the plus sign is the operator, while 2 and 3 are operands.

    Do this instead

    Name the complete expression separately from its operator and operands.

Practice Check

EASY

For the expression 2 + 3 * 4, identify both operators. Then state which operator Python evaluates first and explain why.

Hints
  • Separate the expression into its symbols and values.
  • Compare the precedence of addition and multiplication.
  • The source example gives the precedence relationship you need.
EASY

For the expression 2 + 3, label each part as an operator, an operand, or the complete expression.

Hints
  • There is one operator in this expression.
  • The two numbers are the operands.
  • The full sequence is the expression.

Key Takeaways

  1. 1
    Most statements, or logical lines, that you write will contain expressions.
  2. 2
    An expression can be broken down into operators and operands.
  3. 3
    In 2 + 3, the plus sign is the operator and 2 and 3 are the operands.
  4. 4
    When operators have different precedence levels, the higher-precedence operator is evaluated first.
  5. 5
    In 2 + 3 * 4, multiplication has higher precedence than addition.

At a glance

  • Expressions are parts of statements that can be analyzed as operators and operands.
  • In 2 + 3, 2 and 3 are operands, and + is the operator.
  • Operator precedence determines which operation Python evaluates first.
  • Multiplication has higher precedence than addition, so it is evaluated first in 2 + 3 * 4.

Related concepts

More concepts starting with O