Expressions

Expression example:

i = i + j * k

Terminology, in the above expression:

  • i, j, and k are operands
  • +, *, and = are operators.

There are a number of issues to examine in order to understand how expressions are evaluated:

  • Operator precedence and associativity
  • What happens when operands have different types
  • Result and side effect (discussed with functions)

Expression - Definition

Expression - An expression performs computation in a program. A simple expression applies an operator to one or more operands. Each expression yields a result. Expressions can be used as operands, so we can write compound expressions requiring the evaluation of multiple operators.

The carrying out of the operations is called expression evaluation.

In the evaluation of an expression:

  • One operation is carried out at a time
  • A set of rules specifies how operands are grouped with operators. This gives orderings to the expression evaluation.

Operator precedence and associativity specify how operators are grouped with operands.

Parentheses may be used by the programmer to change the grouping according to precedence.

Operator Precedence

Operator precedence - Operator precedence defines the order in which different operators in a compound expression are grouped. Operators with higher precedence are grouped more tightly than operators with lower precedence. [Lippman]

Operator Precedence Example

# Set up some variables
j = 8
k = 4
m = 1

# What happens first ...?
i = j + k * m

We can insert the parentheses that the operator precedence implies.

* has the highest precedence, it gets its operands first, ....

i = (j + (k * m))
 3    2    1

Associativity

Associativity - Associativity determines how operators of the same precedence are grouped.

Two types of associativity:

  1. left associative - operators are grouped left to right
  2. right associative - operators are grouped right to left

Associativity Example

# Set up some variables
j = 8
k = 4
m = 1

# What happens first ...?
i = j - k - m

We can insert the parentheses that the associativity implies.

Subtraction, like most binary operators is left associative.

i = ((j - k) - m)
 3    1    2

Fundamental Precedences

Operators in the same group have the same precedence.

The groups below are given in the order of their precedence, higher precedence first.

In evaluating an expression all operators in a higher group are handled before operators in a lower group.

Operator Precedence Example

j = 3, k = 4, m = 5, n = 6;
i = j * k + m / n;

Grouping according to precedence:

i = ((j * k) + (m / n));
 4    1     3    2
 4    2     3    1

There is no difference between the possible orderings.

Type and Operations

We have mentioned three important types that are heavily used:

  1. Integers
  2. Real numbers, called floats in Python
  3. Strings

There are many other types a variable may have, for example:

  • list - a Container
  • dictionary - a Container
  • File - used for input and output
  • ...

Certain operations don't make sense for some types.

For example it doesn't make sense to multiply a File times a dictionary.

Type is important, a variable's type determines what operations can be performed with/on it.

Variable Type Operation Examples

Consider the following. Does each make sense? What would the result be, if any?

5 - 9

2.8 * 4.5

2 + 3.1

8 + 'hello'

'hi' + 'bilbo'

10 * 'yo'

Variable Type Operation Examples - 2

What is the result of each operation

1234 + 5555

'1234' + '5555'

Now we see, there is a difference between 347 and '347'

A Variable Type Surprise

Most programming languages will behave as the following demonstrates.

What is output by the following code?

a = 9 / 10
print a # Output
total = 199
count = 100
average = total / count
print(average)

Important Concepts List

  • In the evaluation of an expression one operation is carried out at a time.
  • Operator precedence and associativity determine the grouping of operators and their operands and thus the order of the expression evaluation.
  • Type is important, a variable's type determines what operations can be performed with/on it.