0% found this document useful (0 votes)
6 views46 pages

AP CSP 2024-25 - Week 7 Slides

The document outlines the content covered in Week 7 of the AP Computer Science Principles course, focusing on binary numbers, Python functions, and loops. Key topics include multi-byte representation, overflow and roundoff errors, and the use of functions in Python programming. The session also introduces different types of loops and emphasizes the importance of understanding these concepts for effective programming.

Uploaded by

shaashwatgupta23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views46 pages

AP CSP 2024-25 - Week 7 Slides

The document outlines the content covered in Week 7 of the AP Computer Science Principles course, focusing on binary numbers, Python functions, and loops. Key topics include multi-byte representation, overflow and roundoff errors, and the use of functions in Python programming. The session also introduces different types of loops and emphasizes the importance of understanding these concepts for effective programming.

Uploaded by

shaashwatgupta23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

AP Computer Science Principles

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

This one has 16 bits—two bytes. Three things to take away:


• You always can get an overflow
When I fill it with ones, what is the largest error if you count high enough.
integer that is created? 216 – 1 = 65,535 • You can change the range of
numbers by varying how the
When you create integers, the computer typically bits are valued.
reserves four bytes for them—that means you • More bits means you can count
can count up to 232 – 1, or about 4.3 billion. higher… or more precisely.
Thought exercise: Round-off
Imagine you work at a local store. Or, if you do already, imagine you’re at work.

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?

10 10 10 1 1 1 10¢ 10¢ 10¢

10 10 10 1 1 1 10¢ 10¢ 10¢

10 10 10 1 1 1 10¢ 10¢ 10¢


One more way to look at binary: the Flippy Do PRO
It still has 8 bits.
(I.e., it’s still a byte.)

Now that you know how to


make the 8-bit integers,
you should understand that
we can make the bits worth
greater or lesser values—
but they will always be
powers of 2.

Ask me about the Flippy Do Pro Gold


for only three additional installments
of $29.95!
Your Flippy Do PRO
The bits
that were
here have
moved.

Whole numbers on this side Fractions on this side


Flippy Do PRO Questions—for discussion in tables*
1. Make the smallest (non-zero) number possible using your Flippy Do PRO.
What is the number in binary? What is its decimal equivalent?

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?

3. Using your Flippy Do PRO, make the binary equivalents of


a. 0.75 b. 1.50 c. 12.25 d. 64.00

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:

3. What is the total amount of pie left, in binary and decimal?

* 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.

Why is it a problem for a computer if different groups


(different computers) reach different answers?

3. What is the total amount of pie left, in binary and decimal?

* 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).

2. Big Numbers: No matter how I configure my memory/storage, there will always be


numbers too big for the computer to represent. If that happens, it is an overflow error.

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 ??? ??? ??? ??? ??? ???

1.48: Round-off error


11: overflow error
Digression: Floats … since you know integers, don’t you wonder?
It turns out, if you trade some precision (bits), you can gain precision by splitting the bits a different way,
based on scientific notation:
In Python, a 64-bit float is split into one bit for the sign, 11 bits for the exponent, and 52 bits for the
fractional number. The math is a bit more complicated
(Friday talk? If you cover IEEE754),
but you should know this much.

This means that you can represent many numbers that


would not otherwise be representable purely using binary.
AP CSP Week 7
• Find Focus
• Python 1.5 (Functions),
1.6 (Loops)
TSHS AP CSP 2023-24

Python 1.5
Python 1.5:
Agenda
Functions:
Bits of code that have a name

Learning Target:

I can create and use functions


to improve my programs.

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:

… and lines 4 and 5 are calls to the function.


Functions in Python
This is my function, multiply.
It multiplies two numbers. I prolly
didn’t need to create this function…
It takes two parameters, number1 and
number2. Parameters are local
variables that are created when the
function is called. They can ONLY be used while the function runs; when it is done, so are they.
I call my function 4 times, on lines 15-17. In each function call, I provide two arguments. An
argument is a value (or a string, Boolean, list, etc.) you provide to the function. The function
takes each argument, IN ORDER, and assigns it to the corresponding parameter name.
Here: in the first call, the arguments 62 and 47 are assigned to number1 and number2.
Then the arguments 115325 and 939958 are assigned to number1 and number2… then, 3 & 5.
Note the return statement, which sends a number from the function back to the program.
Confused? Keep practicing and use the vocabulary—you’ll get there.
Functions in Python: an example you already know

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).

3. The code in the body of the function is executed until either


(1) a return statement is encountered, in which case the value of the expression following
the return (if any) becomes the value of the function call, or
(2) there are no more statements in the function to execute, in which case the function
returns the value None.
In either case, the function ends immediately. This will be tested.

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!

2. Something is always returned.


If it looks like nothing is being returned, then it is None.

3. If you do not “catch” what is returned—in a variable assignment or a print


statement or something similar—then that value (or thing returned) is lost
forever! This should sound familiar: the input() function, which you already
know, works the same way.
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.”)

>>> print(“That said, if you want an upperclass”)


>>> print(“tutor for CS or anything, email me.”)

>>> print(“Don’t buy a tutor—we have lots of ”)


>>> print(“qualified jrs & srs at TSHS.”)
AP CSP Week 7
• Find Focus
• Python 1.6 (Loops)
TSHS AP CSP 2023-24

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.”)

>>> print(“That said, if you want an upperclass”)


>>> print(“tutor for CS or anything, email me.”)

>>> print(“Don’t buy a tutor—we have lots of ”)


>>> print(“qualified jrs & srs at TSHS.”)
AP CSP Week 7
• Find Focus
• Review SQI
SuperQuiz I: MCQ
Average 27.1 / 26.4
AP CSP
Median 28 / 27
Week 7
High 38 / 38
Today: SuperQuiz I
Average score (Target 100) 82
Review
Median XP 84
High XP 114

SuperQuiz I: FRQ Targets


CT 13
F 12
 12
P 13
Why did I do that to people I like (you)?

• You should know, EARLY, the level of thinking the AP


Exam requires.
• You should understand the type of time pressure
that you will be under in the exam.
• You should take away that this exam is as much
about your ability to read carefully as it is about your
knowledge of computer science. It is.
Rules of Review
• Some of these are actual AP exam questions.
• Because of that, I cannot let you keep the actual
exams and I can’t post them in the class slides . I will
also keep your answer sheets.
• If you have concerns about your specific score, come
to my office hours and we will review them together.
• If you want to review any of the quizzes once I post
grades, just show up at office hours.
AP CSP Week 7
• Find Focus
• Review SQI
• Python 1.6 (Loops)
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).

2. Big Numbers: No matter how I configure my memory/storage, there will always be


numbers too big for the computer to represent. If that happens, it is an overflow error.

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 ??? ??? ??? ??? ??? ???

1.48: Round-off error


11: overflow error
Review: 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.
Here is the syntax for defining (def is the keyword) and calling a function:

… and lines 4 and 5 are calls to the function.


BONUS Extra Material: Lists in Python

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.

If you don’t need this material, disregard it.


Lists in Python
Lists are the only data structure that you have to know for the AP Exam.
For completeness, there are four “collections” built into Python:
• Lists are ordered (indexed) and changeable. Duplicates are allowed. Created with [brackets]
• Tuples are ordered, but not changeable. Duplicates are allowed. Created with (parentheses)
• Sets are unordered (unindexed) and unchangeable. No duplicates allowed. Created with { }
• Dictionaries are ordered pairs of keys and values that are changeable. No duplicates.
We only are required to learn lists. If you want to learn the others, see packet 4.
Why use collections of data?
By assigning a collection of data to a single variable (each list or collection will have a name ),
you can do useful things, like searching for a target item, manipulating the values of a list of
numbers, ordering formerly-unordered data, etc.
The following examples use strings, but remember, lists can hold anything.
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 =

[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 = [].

You might also like