professor-curious-logo
professor-curious-logo
  1. AP Computer Science Principles
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What are the steps to insert an element into a list at a specific index?

  1. Call the insert() method on the list. 2. Provide the index where you want to insert the element. 3. Provide the value of the element to be inserted.
Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What are the steps to insert an element into a list at a specific index?

  1. Call the insert() method on the list. 2. Provide the index where you want to insert the element. 3. Provide the value of the element to be inserted.

What are the steps to remove the first occurrence of a specific value from a list?

  1. Call the remove() method on the list. 2. Provide the value you want to remove. 3. The method searches for the first instance of the value and removes it.

What are the steps to traverse a list using a for loop?

  1. Initialize the list. 2. Start a for loop that iterates through each element in the list. 3. Access and process each element within the loop.

What are the steps to traverse a list using a while loop?

  1. Initialize the list. 2. Initialize a counter variable (e.g., i = 0). 3. Start a while loop that continues as long as the counter is less than the length of the list. 4. Access the element at the current index (list[i]). 5. Increment the counter (i += 1).

What are the steps to perform a linear search in a list?

  1. Start at the first element of the list. 2. Compare the current element with the target value. 3. If they match, return the index of the current element. 4. If they don't match, move to the next element and repeat steps 2-4. 5. If the target value is not found after checking all elements, return -1 (or a similar indicator).

What are the steps to add an element to the end of a list?

  1. Call the append() method on the list. 2. Provide the value of the element to be added.

What are the steps to assign a value to an element at a specific index?

  1. Access the element using its index: list_name[index]. 2. Use the assignment operator (=) to assign the new value: list_name[index] = new_value.

What are the steps to find the length of a list?

  1. Call the len() function. 2. Pass the list as an argument to the function: len(list_name). 3. The function returns the number of elements in the list.

What are the steps to initialize a list?

  1. Choose a name for the list. 2. Use square brackets [] to define the list. 3. Add elements inside the square brackets, separated by commas.

What are the steps to access an element of a list?

  1. Write the name of the list. 2. Write the index of the element you want to access inside square brackets: list_name[index].

What are the differences between append() and insert()?

append(): Adds to the end | insert(): Adds at a specific index.

What are the differences between for and while loops for list traversal?

for loop: Iterates through elements directly | while loop: Requires manual index management.

What are the differences between complete and partial traversal?

Complete: Visits all elements | Partial: Visits a subset of elements.

What are the differences between accessing and assigning list elements?

Accessing: Retrieves the value at an index | Assigning: Modifies the value at an index.

What are the differences between lists in Python and AP Pseudocode?

Python: Indexing starts at 0 | AP Pseudocode: Indexing starts at 1.

What are the differences between remove() and assigning None to a list element?

remove(): Changes the length of the list | Assigning None: Keeps the list length the same but replaces the element with None.

What are the differences between modifying a list in-place versus creating a new list?

In-place: Changes the original list | New list: Creates a copy with modifications, leaving the original unchanged.

What are the differences between a list and a string?

List: Mutable, can hold different data types | String: Immutable, holds characters only.

What are the differences between using a list and a set?

List: Ordered, allows duplicates | Set: Unordered, does not allow duplicates.

What are the differences between linear search and binary search?

Linear search: Works on unsorted lists, checks each element one by one | Binary search: Requires sorted list, divides search interval in half.

What does the following code output?

python
my_list = [10, 20, 30]
print(my_list[1])

20

What does the following code output?

python
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)

[1, 2, 3, 4]

What does the following code output?

python
my_list = [5, 10, 15]
my_list.insert(1, 7)
print(my_list)

[5, 7, 10, 15]

What does the following code output?

python
my_list = ['a', 'b', 'c']
my_list.remove('b')
print(my_list)

['a', 'c']

What does the following code output?

python
my_list = [10, 20, 30, 20]
my_list.remove(20)
print(my_list)

[10, 30, 20]

What does the following code output?

python
my_list = [1, 2, 3]
print(len(my_list))

3

What does the following code output?

python
my_list = [1, 2, 3, 4, 5]
for i in range(2):
  print(my_list[i])

1 2

What does the following code output?

python
my_list = [10, 20, 30, 40]
i = 1
while i < 3:
  print(my_list[i])
  i += 1

20 30

What does the following code output?

python
my_list = [1, 2, 3]
my_list[0] = my_list[2]
print(my_list)

[3, 2, 3]

Identify the error in the following code:

python
my_list = [1, 2, 3]
print(my_list[3])

IndexError: list index out of range. The valid indices are 0, 1, and 2.