Here's what a list looks like in AP Pseudocode
.
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...
and one list to another.
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.