Error
Error
1
Topics
1) Arithmetic Operations
2) Floor Division vs True Division
3) Modulo Operator
4) Operator Precedence
5) String Concatenation
6) Augmented Assignment
2
Arithmetic Operations
3
Mixing Types
Any expression that two floats produce a float.
x = 17.0 – 10.0
print(x) # 7.0
x = 17.0 – 10
print(x) # 7.0
y = 17 – 10.0
print(y) # 7.0
4
True Division vs Floor Division
The operator / is true division and the operator // returns floor division(round
down after true divide). True divide / always gives the answer as a float.
print(23 // 7) # 3
print(3 // 9) # 0
print(-4 // 3) # -2
print(6 / 5) # 1.2
print(6 / 3) # 2.0 NOT 2!
5
Remainder with %
The % operator computes the remainder after floor division.
• 14 % 4 is 2
• 218 % 5 is 3
3 43
4 ) 14 5 ) 218
12 20
2 18
15
3
Applications of % operator:
• Obtain last digit of a number: 230857 % 10 is 7
• Obtain last 4 digits: 658236489 % 10000 is 6489
• See whether a number is odd: 7 % 2 is 1, 42 % 2 is 0
6
Modulo Operator
The operator % returns the remainder after floor division.
print(18 % 5) # 3
print(2 % 9) # 2, if first number is smaller, it's the answer
print(125 % 10) # 5
print(0 % 10) # 0
print(10 % 0) # ZeroDivisionError
7
Why floor and modulo division are useful
Floor division allows us to extract the integer part of the division while the modulo
operator extracts the remainder part of the division. Consider the question:
8
Why the modulo operator is useful
If today is a Tuesday, which day is 43 days from today?
Answer: 43 divided by 7 is 6 with a remainder of 1. Thus, it will be Wednesday.
print(43 % 7) # 1
Output 1: Output 2:
Please enter an integer value: 4 Please enter an integer value: 13
4 is even: True 13 is even: False
9
Expressions
Find the exact change for 137 cents using quarters, dimes, nickels and cents. Use the least
number of coins.
Note: Modulo 10 (% 10) extracts the last digit. Floor division (// 10) discards the
last digit. Later in another lecture, we will see how to generalize this to
any number of digits.
11
Extracting Digits
Alternatively:
number = 352
ones = number % 10
tens = (number // 10) % 10
hundreds = number // 100
print("ones:", ones, "tens", tens, "hundreds:", hundreds)
Output:
12
Exponentiation and Negation
x = 2 ** 3
print(x) # 8
Negation is a unary operator. It applies to only one operand. Other operations such
as +, -, *, /, //, % are binary operators, they apply to two operands.
x = -5
y = --5
print(x) # -5
print(y) # 5
13
Operator Precedence
Precedence Operator Operation
highest ** exponentiation
- negation
Operators on the same row are applied left to right. Exponentiation, however, is
applied right to left. Expressions in parenthesis are evaluated first(PEMDAS).
14
Operator Precedence
x = -2 ** 4
print(x) # -16
y = 7 – 4 * 5 % (1 + 2)
print(y) # 5
7 - 4 * 5 % (1 + 2)
7 – 4 * 5 % 3
7 – 20 % 3
7 – 2
5
15
Augmented Assignment
An augmented assignment combines an assignment statement with an
operator to make the statement more concise.
Shorthand Equivalent version
variable += value variable = variable + value
variable -= value variable = variable - value
variable *= value variable = variable * value
variable /= value variable = variable / value
variable %= value variable = variable % value
x = 4
x += 1 # equivalent to x = x + 1
print(x) # 5
16
Augmented Assignment
x = 3
x *= 2 + 5
print(x) # 21
number = 5
number *= number
print(number) # 25
17
String Concatenation
Two strings can be combined, or concatenated, using the + operator:
string1 = "abra"
string2 = "cadabra"
magic_string = string1 + string2
first = "Michael"
last = "Smith"
full_name = first + " " + last
Concatenating a string and a number raises a TypeError. Must first cast the
number into a string using str().
apples = "I have " + 3 + "apples" # error!
apples = "I have " + str(3) + "apples" # correct!
18
Errors
Beginning programmers make mistakes writing programs because of
inexperience in programming in general or due to unfamiliarity with a
programming language.
There are four general kinds of errors: syntax errors, run-time errors,
overflow errors and logic errors.
The interpreter reads the Python source file and translates it into a executable
form. This is the translation phase. If the interpreter detects an invalid program
statement during the translation phase, it will terminate the program’s execution
and report an error.
Such errors result from the programmer’s misuse of the language. A syntax
error is a common error that the interpreter can detect when attempting to
translate a Python statement into machine language. 19
Syntax Errors
A syntax error is a common error that the interpreter can detect when
attempting to translate a Python statement into machine language.
x = 3
y = x + 5
y + 2 = x # SyntaxError
print(x # SyntaxError
z = "hello # SyntaxError
20
Run-time Errors
A syntactically correct Python program still can have problems. Some
language errors depend on the context of the program’s execution. Such
errors are called run-time exceptions or run-time errors.
x = 3
y = x + 5
y = w + 1 # Syntactically correct but raises a run-time
# error: "name w is not defined"
21
Run-time Errors
Here's another example of a run-time error.
# Get two integers from the user
print('Please enter two numbers to divide.')
dividend = int(input('Please enter the dividend: '))
divisor = int(input('Please enter the divisor: '))
# Divide them and report the result
print(dividend, '/', divisor, "=", dividend/divisor)
23
Logic Errors
The interpreter can detect syntax errors during the translation phase and uncover
run-time exceptions during the execution phase. Both kinds of problems
represent violations of the Python language.
Such errors are the easiest to repair because the interpreter indicates the exact
location within the source code where it detected the problem.
24
Logic Errors
Consider the effects of replacing the expression dividend/divisor with
divisor/dividend.
The program runs, and unless the user enters a value of zero for the dividend, the
interpreter will report no errors. However, the answer it computes is not correct
in general.
The only time the program will print the correct answer is when dividend equals
divisor.
The program contains an error, but the interpreter is unable detect the problem.
An error of this type is known as a logic error.
25
AP Exam Info
26
AP Exam Info
29