πŸ“š

Β >Β 

⌨️ 

Β >Β 

πŸ“±

3.2 Data Abstraction

2 min readβ€’june 18, 2024

Minna Chow

Minna Chow

Milo Chang

Milo Chang


AP Computer Science Principles ⌨️

80Β resources
See Units

Lists in AP CSP Pseudocode

Here's what a list looks like in AP Pseudocode
.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-goG5CZLHYkZR.png?alt=media&token=593af9f4-35a5-48ac-86fa-95886dfbba9e
An element is an individual value in a list: in the example above, value1, value2, and value3 are all elements. Each element is assigned an index value, or a number representing their place in the list.
In the AP CSP Pseudocode, index numbers start at 1, so in the example above value1 would have an index of 1, value2 an index of 2, and so on. Index values are commonly used to reference and to work with the elements in a list.
This is a key difference from Python, where the index values start at 0.
You saw in the last example how you can assign a filled-in list to a variable.
You can also assign an empty list to a variable...
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-dlh3Ow2YvS2R.png?alt=media&token=4e7047b8-2b1b-4be9-9c43-2db969b3e079
and one list to another.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-gj2UKOFPi0Cu.png?alt=media&token=1b36c419-7eff-4c7d-a238-695b5ad49819
Lists can be used to create data abstraction. Data abstraction simplifies a set of data by representing it in some general way. You can then work with that representation instead of each piece of data itself.
For a very basic example, let's say you want to make a program that will print your to-do list out. You could manually insert values into your program to print, like so:
print ("My To-Do List is:" " Write essay", "Read Chapter 2", "Finish Math HW", "Attend Fiveable Class")
But what if you made a list instead?
to_do = ["Write essay", "Read Chapter 2", "Finish Math HW", "Attend Fiveable Class"] print ("My To Do List is:") print (to_do)
Now, instead of working with the data itself (the things on your to-do list), you're working with a variable, to_do, that represents the data.
There are several advantages to this. For one, it makes your code neater and easier to read. It also allows you to edit the data easily. If you wanted to print a different list in the first example, you would have to erase your code entirely and rewrite it.
By using a variable to represent the list, all you have to do is create a new list under a different name and use that variable instead.
new_to_do = ["Read Gatsby", "Practice Driving", "Go for walk"] print ("My To Do List is:") print (new_to_do)
Using a list also allows you to work easily with individual elements within it.
Strings are an ordered list of characters. You can navigate through them using index values, just like how you can navigate through a list using index values. We'll see this in more detail later.
By using data abstraction, you can create programs that are easier to build and maintain.

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.