ch04 3
ch04 3
Computer Programming 1
Program: Intermediate Diploma in Programming and Computer Science
Amira Alotaibi
Operators are symbols or keywords that allow Python programs to perform computations,
comparisons, and decision-making.
Operator Examples:
Unary Operator & Binary Operator
• The +, - and * operators are straightforward, but note that the + and - operators can be both unary
and binary
• A unary operator has only one operand a binary operator has two
• For example, the operator in +5 is a unary operator to negate the number 5 whereas the - operator
in 4 - 5 is a binary operator for subtracting 5 from 4
Float and Integer Division ( / & // ) Operator
The (/ )operator performs a float division that results in a floating number . For example:
The //operator performs an integer division; the result is an integer, and any fractional part is
truncated. For example:
Exponentiation ( ** ) Operator
To compute 𝒂𝒃 (a with an exponent of b) for any numbers a and b, you can write a** b in Python. For
example:
Remainder (%) Operator
The operator (%), known as remainder or modulo operator , yields the remainder after
division.
The left side operand is the dividend and the right side operand is the divisor .
• Python expressions are written the same way as normal arithmetic expressions
• Example
(3 + 4 * x) / 5 – 10 * (y - 5) * (a + b + c) / x + 9 * (4 / x + (9 + x) / y)
How to Evaluate an Expression
You can safely apply the arithmetic rule for evaluating a Python expression
1.Operators inside parenthesis are evaluated first
• Parenthesis can be nested
• Expression in inner parenthesis is evaluated first
2.Use operator precedence rule
• Exponentiation (**) is applied first
• Multiplication(*) float division(/) integer division(//) and remainder operators(%) are applied
next
▪ If an expression contains several multiplication, division, and remainder operators they
are applied from left to right
• Addition (+) and subtraction(-) operators are applied last
▪ If an expression contains several addition and subtraction operators they are applied
from left to right
Example
• Very often the current value of a variable is used, modified, and then reassigned back to
the same variable
For example, the following statement increases the variable count by 1:
count = count + 1
• Python allows you to combine assignment and addition operators using an augmented (or
compound) assignment operator
• For example:
count += 1
Augmented Assignment Operators
Caution
Caution
The equal to comparison operator is two equal signs (==), not a single equal sign (=).
The latter symbol is for assignment.
Boolean variable
Output
▪ bool function
You can also use the bool function to convert a numeric value to a Boolean value. For example:
Output
Logical Operators
Logical operators, also known as Boolean operators, operate on Boolean values to create a new
Boolean value.
Truth Table for Operator (not)
Truth Table for Operator (and)
Truth Table for Operator (or)
Operator Precedence and Associativity
▪ Order in which Python evaluates operators:
1- The expression in the parentheses is evaluated first
2- The operators are applied according to the precedence rule and the associativity
rule.
▪ The precedence rule