Topic 4: Operators

Arithmetic Operators

Arithmetic expressions in Python can be evaluated using arithmetic operators such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

Each operator has precedence which determines in which order operators in an expression are evaluated. Operators with lower-precedence numbers has higher precedence and are evaluated before higher precedence numbers. Parentheses have the highest-precedence, so they can be used to override precedence order. Operators with the same precedence number are evaluated from left to right in the order they appear.

...
Table 4.1
Example 1
Table 4.2
Expression Explanation
13 + 8 / 4 – 2 * 6 Initial Expression
13 + 2 – 2 * 6 Division is performed first
13 + 2 – 12 Multiplication is performed next
15 – 12 Addition is performed next
3 Subtraction is performed last

Example 2
Table 4.3
Expression Explanation
13 + 8 / (4 – 2) * 6 Initial Expression
13 + 8 / 2 * 6 subtraction is performed first
13 + 4 * 6 Division is performed next
13 + 24 Addition is performed last
37 Subtraction is performed last

Assignment Statements

An assignment statement is used to assign a value to a variable while a program is running.

Syntax:
  1. variableName = expression;
  • The equal (=) symbol is the assignment operator which tells the computer to evaluate the expression on the right side of the assignment operator and store the result in the variable on the left side of the operator.
  • expression can include one or more literal constants, named constants, variables, or arithmetic operators.
  • The data type of expression in an assignment statement must match the data type of the variable. If they don’t match, the compiler will use implicit type casting to get them to match. It’s better to explicitly cast to correct data type yourself.

Example:
age = 5

Abbreviating Assignment Operators

A assignment operator can be abbreviated as well.

Statement must be of the form variableName = variableName arithmeticOperator value, which is abbreviated as variableName arithmeticOperator= value .

Table 4.4 shows the expression and abbreviations of it.

Table 4.4
Expression Abbreviated Expression
a = a + b a += b
a = a - b a -= b
a = a * b a *= b
a = a / b a /= b
a = a % b a %= b
a = a // 3 a //= 3
a = a ** 3 a **= 3

Relational Operators

Relational (also called Comparison operators) are used to compare two values that have the same data type. Table 4.7 shows the relational operators used and its meaning.

Table 4.5
Operator Name Example Precedence No.
< Less than height < 180 1
<= Less than or equal to age <= 18 1
> Greater than mark > 50 1
>= Greater than or equal to temp >= 90 1
== Equal to grade == 90 2
!= Not equal to response != 2 2

Relational expressions are evaluated to a numerical value of 1 or 0 only. If the value is 1, the expression is true. If the value is 0, the expression is false.

Example 1
Table 4.6
Expression Explanation
7 – 3 + 8 < 9 + 5 Initial Expression
4 + 8 < 9 + 5 Subtraction is performed first
12 < 9 + 5 First addition is performed next
12 < 14 Second addition is perfomed next
true Less then operator is performed last

It is adviseable that not to use equality (==) or inequality operator (!=) to compare real numbers. Real numbers cannot be stored exactly, therefore, test that absolute value of their difference is within some small threshold. char values are automatically coerced to int values for comparison purposes. Strings are compared on a character by character basis. The string with the first lower character is considered smaller

Logical Operators

Logical operators allow the combining of two or more conditions (sub-conditions) into one compound condition. Also called Boolean operators and always evaluate to true or false.

The two most common are And (&&) and Or (||).

  • All sub-conditions must be true for a compound condition using And to be true.
  • Only one of the sub-conditions must be true for a compound condition using Or to be true.
  • And is evaluated before Or in an expression.

Logical operators are evaluated after arithmetic and comparison operators. Parentheses can override precedence ordering.

Truth tables summarize how computer evaluates logical operators. Only necessary sub-conditions are evaluated; called short-circuit evaluation . For example: if the first sub-condition in an And clause is false, the second sub-condition need not be evaluated.

Table 4.7
Truth Table for And (&&) Operator
Subcondition1 Subcondition2 Subcondition1 && subcondition2
True True True
True False False
False True False
False False False

Table 4.8
Truth Table for Or (||) Operator
Subcondition1 Subcondition2 Subcondition1 && subcondition2
True True True
True False True
False True True
False False False

Table 4.9
Truth Table for Or (||) Operator
Subcondition !Subcondition2
True False
False True

Example 1: sales > 3500 && sales < 6000
The compound condition is evaluated to be true when the number stored in the sales variable is greater than 3500 and at the same time, less than 6000; otherwise it evaluates to false.


Example 2: measurement == 20 || measurement > 65
The compound condition evaluates to true when the number stored in the measurement variable is either equal to 20 or greater than 65; otherwise, evaluates to false.


Table 4.10
Operator Operation Precedence No.
( ) Parenthesis overrides normal precedence rules 1
! Negation (reverses the sign of a number) 2
*, /, % Multiplication, division, and modulus 3
+, - Addition and subtraction 4
<, <=,>, >= Less than, less than or equal to, greater than, greater than or equal to 5
==, != Equal to, not equal to 6
And (&&) All sub-conditions must be true for the compound condition to evaluate to true 7
Or (||) Only one sub-condition needs to be true for the compound condition to evaluate to true 8

Example
Table 4.11
Expression Explanation
20 < 80 / 2 + 3 && 25> 10 * 2 Initial Expression
20 < 40 + 3 && 25> 10 * 2 80 / 2 is performed first
20 < 40 + 3 && 25> 20 10 * 2 is performed next
20 < 43 && 25> 20 40 + 3 is perfomed next
True && 25 > 20 20 < 43 is perfomed next
True && True 25 > 20 is perfomed next
True True && true evaluates to true