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.
- A value all by itself is a simple expression. For example 17 is a value: >>> 17
- 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
- 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
- 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
- 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
- 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.
- Integers - the short name is int - int values are numbers such as -1, 0, 1, 2 and so on
- Strings are values that represent text such as “Hello World!”. Strings are always enclosed in quote marks.
- 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:
- 20 + 32 is 20 plus 32
- hour - 1 is Value assigned to hour minus 1
- When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed.
- hour * 60 is Value assigned to hour times 60. The asterisk * is the operator for multiplication.
- minute / 60 is Value assigned to minutes divided by 60
- 5 ** 2 is 5 to the power of 2. ** is the operator for exponentiation
- (5 + 9) * (15 - 7) The tokens +, -, and *, and the use of parentheses for grouping, mean in Python what they mean in mathematics.
The 5 arithmetic operators that you will see in the CS of GAT are:
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:
- x == y # Produce True if ... x is equal to y
- x != y # ... x is not equal to y
- x > y # ... x is greater than y
- x < y # ... x is less than y
- x >= y # ... x is greater than or equal to y
- 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
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.
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|
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|
The third logical operator, not only takes a single operand, so its truth table only has two rows: