AP CSP 2024-25 - Week 7 Slides
AP CSP 2024-25 - Week 7 Slides
Week 7 Slides
(Finishing Binary/numbers, Python)
AP CSP Week 7
• Find Focus
• Binary II: multi-byte FlippyDo;
FlippyDo PRO; a/k/a overflow
errors and roundoff errors
Last Week
• In Python, we added functions to our arsenal of programming tools.
• We created a text file (hello.txt) and a binary file (hello.docx). Each had 5
bytes of data; the Word doc had about 12,000 bytes of metadata.
• After reviewing a few counting systems, e.g., Roman numerals, octal (base
8--SpongeBob’s & Mickey’s counting system), trinary (3 shapes), and
hexadecimal (base 16 [A, B, C, D, E, F]), we focused in on binary and it was
pretty easy once you made and understood how to use the Flippy Do .
Today, we will extend that.
• From here, we will finish numbers, then cover Booleans and text (ASCII and
Unicode). Then we will move to images and sound.
Do these numbering systems still confuse a bit?
Do you like being mesmerized?
tinyurl.com/binaryodometer
tinyurl.com/binaryodo
https://inventwithpython.com/odometer/
The 2-byte Flippy Do: Big Numbers, Overflow Error
215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20
32,768 16,384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
This one has 16 bits—two bytes. It’s basically two Flippy Dos taped together.
When I fill it with ones, what is the largest integer that is created?
When you create integers, the computer typically reserves four bytes for them—
that is four Flippy Dos taped together…
… which means you can count up to 232 – 1, or about 4.3 billion.
In Java, a “long” integer has 8 bytes. (C handles these with large floats called “doubles”.)
Python is dynamic: if your integer needs more memory, Python automatically makes it bigger.
The 2-byte Flippy Do: Big Numbers, Overflow Error
215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20
32,768 16,384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
In the register all you have are nine $10 bills, nine $1 bills, and nine dimes.
• What’s the largest amount of change that you can give someone?
• What’s the smallest amount?
• What would you do if someone needed 7 cents in change?
2. Increase the number you made in Question 1 to the next value that can be
made using your Flippy Do PRO.
What is the number in binary? What is its decimal equivalent?
4. What is the largest number you can make with the Flippy Do PRO?
* Express all binary answers using 8 bits and an appropriately-placed decimal point.
Flippy Do PRO Questions—for discussion in your groups*
1. Can you make the binary number for 0.3910 (0.39 in decimal)?
2. Using your Flippy Do PRO, determine how much of each of the pies
below is left at the end of Halloweengiving:
* Express all binary answers using 8 bits and an appropriately-placed decimal point.
Flippy Do PRO Questions—for discussion in your groups*
1. Can you make the binary number for 0.3910 (0.39 in decimal)? NO.
2. Using your Flippy Do PRO, determine how much of each of the pies
below is left at the end of Halloweengiving:
Roundoff error occurs when an exact
value cannot be made with available place values.
* Express all binary answers using 8 bits and an appropriately-placed decimal point.
Summing Up: Numbers in Binary
1. Big Numbers: I can make bigger numbers by increasing the number of bits used to store
the number or by assigning larger binary values to each bit (but I will lose precision).
3. Small Numbers: I can use binary to represent very small numbers using fractions based
on the negative powers of two. If a fraction cannot be represented precisely, roundoff
error occurs
Numbers that can be represented with 3 bits
and the value
is rounded.
0 1 2 3 4 5 6 7 8 9 10 11 12 13
000 001 010 011 100 101 110 111 ??? ??? ??? ??? ??? ???
Python 1.5
Python 1.5:
Agenda
Functions:
Bits of code that have a name
Learning Target:
Python 1.5 17
Functions in Python
When you have code that you will reuse, it probably is useful to put it into a function.
If you do that, you give that block of code a name and then you use that name to call the
function from wherever you want/need to. Essentially, it is a program within your program.
Here is the syntax for defining (def is the keyword) and calling a function:
This is Python’s built-in function, print. Well, it’s a bunch of calls to that function.
Each line above is a function call to the print function. The print function can take from 0 to
(allegedly—try it!) 256over 100,000 arguments.
The function then takes those arguments, converts them to parameters, and prints them to the
console or wherever output is supposed to go.
Does this help you understand what arguments are? Questions?
You know functions (sometimes also
called procedures): print(), round(), input(), …
A function is a set of instructions with a name—print(), round(), input()…
Really, it is a unit of reusable code that you can call again and again using its
name. When a function is called, program control runs the function code and
then returns control (the Eye of Sauron—just like in IPSO!) to where the
function was called.
Syntax: in Python, all functions begin with “def” (short for “define”), the name of
the function, any parameters in parentheses, and a colon:
def <functionName>(<parameters>):
<code in the body of the function>
Python 1.5 21
Worth a digression: When a function is called…
1. The arguments are evaluated and the parameters of the function are assigned to the
resulting values.
For example, in the function call round(math.pi(), 1), the numbers pi and 1 are the
arguments that would be assigned to the two parameters of the function round().
2. The point of execution (the next instruction in the program to be executed, also called the
flow of control or the Eye of Sauron) moves from the point where the function is called to
the first statement (first line of code) in the body of the function).
4. The point of execution (flow of control, Eye of Sauron) moves back to the code
immediately following where the function was called.
Worth an extra digression: return
1. When a return statement is run, the function ends immediately and the flow of
control returns to the line of code right after the one that called the function.
This cannot be overemphasized. It is tested in AP CSP and AP CSA!
Python 1.6
Python 1.6:
Agenda
Loops:
For loops,
for-each loops,
while loops
Learning Targets:
I can create and use the three
basic types of loops—and apply
the right one in the right
circumstances..
Python 1.6 27
Python 1
öďď ď ďų ďŌ >>> print(“I am not posting all of the ”)
>>> print(“Python 1 slides because all of ”)
ď ŷ ć Ōć
>>> print(“the material is in the handout, ”)
ŌŲ Ō ġ ďď Ō Ō >>> print(“and based on the quiz, many of ”)
Qŷ ď ŲŌ Ō ć >>> print(“you need to do the examples, try ”)
ŷ ďď Ō ġ ď ćď >>> print(“other things, and read the left-text.”)
3. Small Numbers: I can use binary to represent very small numbers using fractions based
on the negative powers of two. If a fraction cannot be represented precisely, roundoff
error occurs
and the value Numbers that can be represented with 3 bits
is rounded.
0 1 2 3 4 5 6 7 8 9 10 11 12 13
000 001 010 011 100 101 110 111 ??? ??? ??? ??? ??? ???
The following slides are optional review for those of you who are
new to Python or who want a review of lists and how they work.
[Note: like the packets, if you need to learn this or gain greater
mastery, type in the code in the example above, have a hypothesis, and
follow what happens.]
Creating, Using a List
This is my list, teachers.
Lists work just like strings:
• They are indexed, starting at 0. E.g., teachers[1] = ‘schafer’
• You can use negative indexing and slicing!
One big difference: you can change individual items of a list.
You can’t do that with strings (they are immutable). Examples are in the code above…
After line 3, teachers = [‘bliss’, ‘ranieri’, ‘travis’].
After line 4, teachers =
Creating, Using a List
This is my list, teachers.
Lists work just like strings:
• They are indexed, starting at 0.
E.g., initially, teachers[1] = ‘schafer’
• You can use negative indexing and slicing!
One big difference: you can change individual items of a list.
You can’t do that with strings (they are immutable). Examples are in the code above…
After line 3, teachers = [‘bliss’, ‘ranieri’, ‘travis’].
After line 4, teachers = [‘bliss’, ‘ranieri’, ‘ivy’, ‘travis’].
After line 5, teachers =
Creating, Using a List
This is my list, teachers.
Lists work just like strings:
• They are indexed, starting at 0. E.g., teachers[1] = ‘schafer’
• You can use negative indexing and slicing!
One big difference: you can change individual items of a list.
You can’t do that with strings (they are immutable). Examples are in the code above…
After line 3, teachers = [‘bliss’, ‘ranieri’, ‘travis’].
After line 4, teachers = [‘bliss’, ‘ranieri’, ‘ivy’, ‘travis’].
After line 5, teachers = [‘leslie’, ‘ostlie’, ‘nguyen’, ‘fort’, ‘ivy’, ‘travis’].
After line 6, teachers =
Creating, Using a List
This is my list, teachers.
Lists work just like strings:
• They are indexed, starting at 0. E.g., teachers[1] = ‘schafer’
• You can use negative indexing and slicing!
One big difference: you can change individual items of a list.
You can’t do that with strings (they are immutable). Examples are in the code above…
After line 3, teachers = [‘bliss’, ‘ranieri’, ‘travis’].
After line 4, teachers = [‘bliss’, ‘ranieri’, ‘ivy’, ‘travis’].
After line 5, teachers = [‘leslie’, ‘ostlie’, ‘nguyen’, ‘fort’, ‘ivy’, ‘travis’].
After line 6, teachers = [‘leslie’, ‘bonomo’, ‘travis’].
After line 7, teachers =
Creating, Using a List
This is my list, teachers.
Lists work just like strings:
• They are indexed, starting at 0. E.g., teachers[1] = ‘schafer’
• You can use negative indexing and slicing!
One big difference: you can change individual items of a list.
You can’t do that with strings (they are immutable). Examples are in the code above…
After line 3, teachers = [‘bliss’, ‘ranieri’, ‘travis’].
After line 4, teachers = [‘bliss’, ‘ranieri’, ‘ivy’, ‘travis’].
After line 5, teachers = [‘leslie’, ‘ostlie’, ‘nguyen’, ‘fort’, ‘ivy’, ‘travis’].
After line 6, teachers = [‘leslie’, ‘bonomo’, ‘travis’].
After line 7, teachers = [‘leslie’, ‘bonomo’, ‘travis’, ‘zebrack’].
After line 8, teachers =
Creating, Using a List
This is my list, teachers.
Lists work just like strings:
• They are indexed, starting at 0. E.g., teachers[1] = ‘schafer’
• You can use negative indexing and slicing!
One big difference: you can change individual items of a list.
You can’t do that with strings (they are immutable). Examples are in the code above…
After line 3, teachers = [‘bliss’, ‘ranieri’, ‘travis’].
After line 4, teachers = [‘bliss’, ‘ranieri’, ‘ivy’, ‘travis’].
After line 5, teachers = [‘leslie’, ‘ostlie’, ‘nguyen’, ‘fort’, ‘ivy’, ‘travis’].
After line 6, teachers = [‘leslie’, ‘bonomo’, ‘travis’].
After line 7, teachers = [‘leslie’, ‘bonomo’, ‘travis’, ‘zebrack’].
After line 8, teachers = [‘leslie’, ‘bonomo’, ‘travis’, ‘zebrack’, ‘bonomo’].
After line 9, teachers =
Creating, Using a List
This is my list, teachers.
Lists work just like strings:
• They are indexed, starting at 0. E.g., teachers[1] = ‘schafer’
• You can use negative indexing and slicing!
One big difference: you can change individual items of a list.
You can’t do that with strings (they are immutable). Examples are in the code above…
After line 3, teachers = [‘bliss’, ‘ranieri’, ‘travis’].
After line 4, teachers = [‘bliss’, ‘ranieri’, ‘ivy’, ‘travis’].
After line 5, teachers = [‘leslie’, ‘ostlie’, ‘nguyen’, ‘fort’, ‘ivy’, ‘travis’].
After line 6, teachers = [‘leslie’, ‘bonomo’, ‘travis’].
After line 7, teachers = [‘leslie’, ‘bonomo’, ‘travis’, ‘zebrack’].
After line 8, teachers = [‘leslie’, ‘bonomo’, ‘travis’, ‘zebrack’, ‘bonomo’].
After line 9, teachers = [‘leslie’, ‘travis’, ‘zebrack’, ‘bonomo’].
After line 10, teachers = [].