Unit 1: Computational Expressions

Evaluating Python Expressions

Python expressions are composed of symbols, values, variables, operators and function calls. Evaluating these expressions is one of the core skills of a programmer. The evaluation of an expression produces a value.

  1. A value all by itself is a simple expression. For example 17 is a value: >>> 17
  2. A value may be assigned to a variable. For example, y is a variable and 3.14 is the value assigned to the variable y: >>> y = 3.14
  3. An arithmetic expression evaluates to a value. For example 1 and 2 are values, + is the operator and >>> 1 + 2 evaluates to the value 3
  4. An arithmetic expression with variables evaluates to a value. For example, x and y are variables with values 1 and 2 respectively. >>> x + y evaluates to the value 3
  5. An arithmetic expression can be assigned to a variable. For example, x and y are variables with values 1 and 2 respectively. r is a variable assigned with the arithmetic expression x + y. First, the expression x + y evaluates to 3 and then 3 is assigned to variable r. >>> r = x + y
  6. A function call evaluates to a value. For example, abs is a python builtin function to calculate the absolute value. -1 is passed to the function as input and the expression evaluates to the value of 1. 1 the absolute value of -1. >>> abs(-1)

Let’s take a deep dive into python expressions.

Types of Values in Python

Python values can be of the following types.

  1. Integers - the short name is int - int values are numbers such as -1, 0, 1, 2 and so on
  2. Strings are values that represent text such as “Hello World!”. Strings are always enclosed in quote marks.
  3. Boolean values are True and False. There are only two boolean values.

Variables and Assignment Operator Python variables are symbols with a name such as x. Variables can be assigned values. An assignment is done using = operator. Let’s review the following sequence of variables and value assignments. Notice how the values of the variable change.

  • x = 5 Value is assigned to the variable x
  • x = 6 The previous value of x i.e., 5 is replaced with 6
  • y = x A new variable y is assigned the value of x. Both x and y evaluate to the value 6.
  • y = 2 y is assigned the value 2, so x is 6 and y is changed to 2.

Operators and Operands

Operators are built-in symbols that represent computations like addition, multiplication, and division. The values the operator uses are called operands.

The following are all legal Python expressions whose meaning is more or less clear:

  1. 20 + 32 is 20 plus 32
  2. hour - 1 is Value assigned to hour minus 1
    1. When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed.
  3. hour * 60 is Value assigned to hour times 60. The asterisk * is the operator for multiplication.
  4. minute / 60 is Value assigned to minutes divided by 60
  5. 5 ** 2 is 5 to the power of 2. ** is the operator for exponentiation
  6. (5 + 9) * (15 - 7) The tokens +, -, and *, and the use of parentheses for grouping, mean in Python what they mean in mathematics.

Arithmetic Operators

The 5 arithmetic operators that you will see in the CS of GAT are:

  1. +
  2. -
  3. *
  4. /
  5. %

Boolean Values and Expressions

A Boolean value is either true or false. This is the basis of all modern computer logic.

In Python, the two Boolean values are True and False.

A Boolean expression is an expression that evaluates to produce a result which is a Boolean value. For example, the operator == tests if two values are equal. It produces a Boolean value:

>>> 5 == (3 + 2)   # Is five equal 5 to the result of 3 + 2?
True
>>> 5 == 6
False

In the first statement, the two operands evaluate to equal values, so the expression evaluates to True; in the second statement, 5 is not equal to 6, so we get False.

The == operator is one of six common comparison operators which all produce a bool result; here are all six:

  1. x == y # Produce True if ... x is equal to y
  2. x != y # ... x is not equal to y
  3. x > y # ... x is greater than y
  4. x < y # ... x is less than y
  5. x >= y # ... x is greater than or equal to y
  6. x <= y # ... x is less than or equal to y

Like any other types we’ve seen so far, Boolean values can be assigned to variables, printed, etc.

>>> age = 18
>>> old_enough_to_get_driving_licence = age >= 17
>>> print(old_enough_to_get_driving_licence)
True

Logical operators

There are three logical operators, and, or, and not, that allow us to build more complex Boolean expressions from simpler Boolean expressions. The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 produces True only if x is greater than 0 and at the same time, x is less than 10.

n % 2 == 0 or n % 3 == 0 is True if either of the conditions is True, that is, if the number n is divisible by 2 or it is divisible by 3. (What do you think happens if n is divisible by both 2 and 3 at the same time? Will the expression yield True or False?)

Finally, the not operator negates a Boolean value, so not (x > y) is True if (x > y) is False, that is, if x is less than or equal to y.

Truth Tables

A truth table is a small table that allows us to list all the possible inputs, and to give the results for the logical operators. Because the and and or operators each have two operands, there are only four rows in a truth table that describes the semantics of and.

a b a and b
False False False
False True False
True False False
True True True

In a Truth Table, we sometimes use T and F as shorthand for the two Boolean values: here is the truth table describing or:

a b a or b
False False False
False True True
True False True
True True True

The third logical operator, not only takes a single operand, so its truth table only has two rows:

a not a
False True
True False