2. • Operators
• Arithmetic operators
• Including mod and integer division
• Expression
• Priorities of operators
• Bindings of operators
• Variables
• Rules for naming
• Different types
• How to use them
• Printing output to the screen
• Getting input from the user
• Assignment operators
• Comparison operators
• Boolean operators
Objectives
3. Python Basic Operators
An operator is a symbol of the programming language, which
is able to operate on the values.
Operators are the constructs which can manipulate and
evaluate our data.
Consider the expression:
num = 4 + 5
operator
4. Types of Operators in Python
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Membership Operators
Bitwise Operators
Identity Operators
focus of today’s lecture
5. Arguments
when both arguments are integers, the result is an
integer, too;
when at least one argument is a float, the result is a
float, too.
6. Operators – Addition & Subtraction
the + (plus) sign is the operator which is able to add two
numbers, giving the result of the addition.
“Lowest” priority in the order of operations
Function as they normally do
Examples:
1. cash = cash - bills
2. (5 + 7) / 2
3. ( ((2 + 4) * 5) / (9 - 6) )
4. -4 + 4 = 0
5. -4. + 8 = 4.0
7. The subtraction operator, unary and
binary operators
The subtraction operator is obviously the - (minus) sign, although you
should note that this operator also has another meaning ‒ it can change
the sign of a number.
This is a great opportunity to present a very important distinction
between unary and binary operators.
In subtracting applications, the minus operator expects two arguments:
the left (a minuend in arithmetical terms) and right (a subtrahend).
For this reason, the subtraction operator is considered to be one of the
binary operators, just like the addition, multiplication and division
operators.
-4 – 4= - 8
4. – 8 = - 4.0
-1.1= - 1.1
8. Operators – Multiplication & Division
An * (asterisk) sign is a multiplication operator.
A // (slash) sign is a division operator.
The value in front of the slash is a dividend, the value behind the slash,
a divisor.
The result produced by the division operator is always a float, regardless
of whether or not the result seems to be a float at first glance: 1/2, or if it looks
like a pure integer: 2/1.
Higher priority in the order of operations
than addition and subtraction
Function as they normally do
Examples:
1. tax = subtotal * 0.06
2. area = PI * (radius * radius)
3. totalDays = hours / 24
9. Operators – Integer Division
Reminder: integers (or ints) are whole numbers
What do you think integer division is?
A // (double slash) sign is an integer division operator. It differs from the
standard / operator in two details:
•its result lacks the fractional part ‒ it's absent (for integers), or is always equal to zero (for
floats); this means that the results are always rounded; rounding always goes to the
lesser integer.
•it conforms to the integer vs. float rule.
•integer by integer division gives an integer result. All other cases
produce floats.
Remember division in grade school?
Integer division is
Division done without decimals
And the remainder is discarded
Integer division can also be called floor division
10. How not to divide
As you probably know, division by zero doesn't work.
Do not try to:
•perform a division by zero;
•perform an integer division by zero;
•find a remainder of a division by zero
12. Operators – Mod
Also called “Remainder” or “modulo” or “modulus”
Its graphical representation in Python is the % (percent) sign, which may look a bit
confusing.
Example: 17 % 5 = 2
What do you think mod does?
The result of the operator is a remainder left after the
integer division.
Remember division in grade school?
Modulo gives you the remainder
The “opposite” of integer division
14. Modulo Answers
Result of a modulo operation will always be:
Positive
No less than 0
No more than the divisor minus 1
Examples:
1. 8 % 3 =
2. 21 % 3 =
3. 13 % 3 =
2
0
1 no less than zero
no more than
the divisor minus
1
15. Operators – Exponentiation
“Exponentiation” is just another word for raising one
number to the power of another
Examples:
1. binary8 = 2 ** 8
2. squareArea = length ** 2
3. cubeVolume = length ** 3
4. squareRoot = num ** 0.5
16. Exponentiation
We've surrounded the double asterisks with spaces in our
examples. It's not compulsory, but it improves
the readability of the code.
2 ** 3 = 8
2 ** 3. = 8.0
2. ** 3 = 8.0
2. ** 3. = 8.0
18. Division: Floats and Integers
Floats (decimals) and integers (whole numbers) behave in
two different ways in Python
And in many other programming languages
Biggest difference is how their division works
Python 3 automatically performs decimal division
For both integers and floats
Have to explicitly call integer division
20. Rounding Errors
Sometimes we need to approximate the representation
of numbers
0.66666666666666666666666667…
3.14159265358979323846264338328…
We know that this can lead to incorrect
answers when doing calculations later
Something similar happens when numbers
are stored in a computer’s memory
21. Float Arithmetic Examples
What do the following expressions evaluate to?
1. 8 / 3
2. 5 / 7
3. 1.99 + 0.12
4. 0.99 + 0.12
5. 1.13 * 1.19
= 2.6666666666666667
= 0.7142857142857143
= 2.11
= 1.1099999999999999
= 1.3446999999999998
Because computers store
numbers differently, they
sometimes run into different sets
of rounding errors
What’s
going on
here???
22. Handling Floating Point Errors
How to fix floating point errors?
You can’t!
¯_(ツ)_/¯
They’re present in every single programming language
that uses the float data type
Just be aware that the problem exists
Don’t rely on having exact numerical representations
when using floats in Python
23. Expression
Data and operators when connected together
form expressions. The simplest expression is a literal
itself.
24. Operators and their priorities
We've treated each operator as if it had no connection with
the others. Obviously, such an ideal and simple situation is a
rarity in real programming.
you will very often find more than one operator in one
expression, and then things are no longer so simple.
The order of their appearance is not accidental.
The phenomenon that causes some operators to act before
others is known as the hierarchy of priorities.
Python precisely defines the priorities of all operators, and
assumes that operators of a higher priority perform their
operations before the operators of a lower priority.
26. Order of Operations (Arithmetic)
Expressions are evaluated in what direction?
What can change this ordering?
Parentheses!
from left to right
Operator(s) Priority
highest
lowest
**
* / // %
+ -
27. Operators and parentheses
you're always allowed to use parentheses, which can
change the natural order of a calculation.
In accordance with the arithmetic
rules, subexpressions in parentheses are always
calculated first.
You can use as many parentheses as you need, and
they're often used to improve the readability of an
expression, even if they don't change the order of the
operations.
An example of an expression with multiple parentheses
is here:
10.0
print((5 * ((25 % 13) + 100) / (2 * 13)) // 2)
28. Operators and their bindings
The binding of the operator determines the order of
computations performed by some operators with equal
priority, put side by side in one expression.
Most of Python's operators have left-sided binding,
which means that the calculation of the expression is
conducted from left to right.
(9 % 6 % 2) =1
There are two possible ways of evaluating this
expression:
• from left to right: first 9 % 6 gives 3, and then 3 %
2 gives 1;
• from right to left: first 6 % 2 gives 0, and then 9 %
0 causes a fatal error.
29. Operators and their bindings
But there's one interesting exception.
Use this snippet of code:
print(2 ** 2 ** 3) -> 256
The two possible results are:
•2 ** 2 → 4; 4 ** 3 → 64
•2 ** 3 → 8; 2 ** 8 → 256
•The result clearly shows that the exponentiation operator uses right-
sided binding.
print(-3 ** 2) -> -9
print(-2 ** 3) -> -8
print(-(3 ** 2)) -> 9
Notas do Editor
Instructions
To create em dash above headlineSame size and weight as the headline and set using a soft return.PC: Em dash (—): Alt+Ctrl+ - (minus)Mac: Em dash (—): Shift+Alt/Option+hyphen