Grade 9 Exam  >  Grade 9 Notes  >  AP Computer Science Principles  >  Chapter Notes: Lists

Lists Chapter Notes | AP Computer Science Principles - Grade 9 PDF Download

Introduction

Lists are a key concept in AP Computer Science Principles, used to store and manage multiple items in a specific order. This chapter explains how to work with lists, including accessing, changing, adding, and removing elements. It also covers how to find the length of a list and how to loop through its elements. Understanding lists helps programmers organize data efficiently and perform operations like searching or sorting, which are essential for many programs.

Basic List Operations

The AP exam tests your ability to manipulate lists using fundamental operations. Below are the core techniques you need to know.

Accessing Elements by Index

You can retrieve a specific element from a list using its index. This isolates the element for further use.
grocery_list = ["milk", "eggs", "cheese"]
print(grocery_list[0])

Output: milk

Assigning the value of an element of a list to a variable

This operation lets you replace an existing element in a list by assigning a new value to it. It doesn't add new elements, only modifies existing ones.
grocery_list = ["milk", "eggs", "cheese"]
change = "soap"
grocery_list[2] = change
print(grocery_list)

Output: ["milk", "eggs", "soap"]

Assigning a value to an element outright

You can directly update an element by assigning a new value to its index.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list[2] = "fish"
print(grocery_list)

Output: ["milk", "eggs", "fish"]

Assigning the value of one element in the list to another

You can assign the value of one list element to another index within the same list.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list[0] = grocery_list[2]
print(grocery_list) 

Output: ["cheese", "eggs", "cheese"]

Adding and Removing Elements

Inserting an Element at a Specific Index

The insert method adds an element at a chosen index, shifting all subsequent elements down. This increases the list’s length.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list.insert(2, "butter")
print(grocery_list)

Output: ["milk", "eggs", "butter", "cheese"]

Question for Chapter Notes: Lists
Try yourself:
What does the insert method do in a list?
View Solution

Appending Elements to the End

The append method adds an element to the end of the list, extending its length.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list.append("flour")
print(grocery_list)

Output: ["milk", "eggs", "cheese", "flour"]

Removing Elements

In Python, the remove method deletes the first occurrence of a specified value from the list.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list.remove("eggs")
print(grocery_list)

Output: ["milk", "cheese"]

 Determining the length of a list

The len function returns the number of elements in a list.
grocery_list = ["milk", "eggs", "cheese"]
print(len(grocery_list))

Output: 3

Looping Through Lists

Loops allow you to process each element in a list, either fully or partially. Common algorithms with lists find maximum, minimum, or average values. Linear search (sequential search) checks each element in order until a value is found or all elements are checked.

Complete Traversal

A complete traversal visits every element in the list. You can use a for loop or a while loop.

For Loop Example
grocery_list = ["milk", "eggs", "cheese"]
for element in grocery_list:
print(element)

Output:

  • milk
  • eggs
  • cheese

While Loop Example
grocery_list = ["milk", "eggs", "cheese"]
i = 0 
while i < len(grocery_list):
                  print(grocery_list[i])
    i += 1
Output:

  • milk
  • eggs
  • cheese

Partial Traversal

A partial traversal visits a subset of the list, defined by a start and end index.

For Loop Example
grocery_list = ["milk", "eggs", "cheese", "apples"]start_index = 1
end_index = 3
for i in range(start_index, end_index + 1):
    print(grocery_list[i])
Output:

  • eggs
  • cheese
  • apples

While Loop Example
grocery_list = ["milk", "eggs", "cheese", "apples"]
i = 1
while i < 4:
print(grocery_list[i])
 i += 1
Output:

  • eggs
  • cheese
  • apples

Question for Chapter Notes: Lists
Try yourself:
What method removes the first occurrence of a specified value from a list?
View Solution

Key Terms

  • Append: Adds an element to the end of a list.
  • For Loop: A control structure that executes a code block for a set number of iterations, using initialization, condition, and increment/decrement.
  • Insert: Adds an element at a specific index in a list, shifting subsequent elements.
  • List: An ordered collection of elements, accessible by their position, like a numbered shopping list.
  • Len (Function): Returns the number of elements in a list or other data structure.
  • Linear Search: A search algorithm that checks each element sequentially until the target is found or the list is fully checked.
  • Loop: A programming structure that repeats code execution based on conditions, automating repetitive tasks.
  • Partial Traversal: Visiting only a subset of elements in a data structure, skipping others.
  • Range (Function): Generates a sequence of numbers, often used in loops or to create lists.
  • Remove: Deletes a specified element from a list or other data structure.
  • Variable: A named memory location that stores a changeable value during program execution.
  • While Loop: A loop that executes code as long as a condition remains true, checking the condition before each iteration.
The document Lists Chapter Notes | AP Computer Science Principles - Grade 9 is a part of the Grade 9 Course AP Computer Science Principles.
All you need of Grade 9 at this link: Grade 9
35 docs

FAQs on Lists Chapter Notes - AP Computer Science Principles - Grade 9

1. What are list operations in Python?
Ans. List operations in Python refer to various methods and functions that can be performed on lists, such as accessing elements by index, adding or removing elements, and iterating through the list. These operations allow you to manipulate and manage the data stored in lists effectively.
2. How can I access elements in a list by index?
Ans. You can access elements in a list by using their index, which starts at 0. For example, if you have a list called `my_list`, you can access the first element with `my_list[0]`, the second element with `my_list[1]`, and so on.
3. What does it mean to directly assign a value to a list element?
Ans. Directly assigning a value to a list element means you can change the value of an existing element in the list by using its index. For example, if you have a list `my_list` and you want to change the value of the first element, you can do so by using `my_list[0] = new_value`.
4. How do I append an element to the end of a list?
Ans. To append an element to the end of a list, you can use the `append()` method. For example, if you have a list named `my_list`, you would write `my_list.append(new_element)` to add `new_element` to the end of the list.
5. What is the method to find the length of a list in Python?
Ans. You can find the length of a list in Python by using the `len()` function. For example, if you have a list called `my_list`, you would use `len(my_list)` to get the number of elements in the list.
Related Searches

Exam

,

Objective type Questions

,

video lectures

,

Previous Year Questions with Solutions

,

ppt

,

MCQs

,

pdf

,

Important questions

,

Extra Questions

,

Summary

,

shortcuts and tricks

,

Viva Questions

,

mock tests for examination

,

Semester Notes

,

Lists Chapter Notes | AP Computer Science Principles - Grade 9

,

Lists Chapter Notes | AP Computer Science Principles - Grade 9

,

Sample Paper

,

study material

,

Lists Chapter Notes | AP Computer Science Principles - Grade 9

,

Free

,

past year papers

,

practice quizzes

;