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.
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 |
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 |
An assignment statement is used to assign a value to a variable while a program is running.
Syntax:Example:
age = 5
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.
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 (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.
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 1Expression | 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 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 (||).
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.
Truth Table for And (&&) Operator | ||
---|---|---|
Subcondition1 | Subcondition2 | Subcondition1 && subcondition2 |
True | True | True |
True | False | False |
False | True | False | False | False | False |
Truth Table for Or (||) Operator | ||
---|---|---|
Subcondition1 | Subcondition2 | Subcondition1 && subcondition2 |
True | True | True |
True | False | True |
False | True | True | False | False | False |
Truth Table for Or (||) Operator | ||
---|---|---|
Subcondition | !Subcondition2 | |
True | False | |
False | True |
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 |
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 |