Part 1 Part 2 Part 3 Part 4

Lists

In Python, a list is an ordered, mutable collection that can hold different types of data. This makes lists versatile, as they can store integers, strings, or even other lists. One key feature of lists in Python is that they are indexed starting from 0, meaning the first element in a list is at index 0. For example, in the list my_list = ['apple', 'banana', 'cherry'], the first element is 'apple', located at my_list[0].

Learning Objectives,

  • You will be able to write expressions that use list indexing and list procedures.
  • For algorithms involving elements of a list, you will be able to write iteration statements to traverse a list.

Key Points

  • Ordered: The elements have a defined order, meaning you can refer to an item by its index.
  • Mutable: You can change the items within a list (unlike tuples which are immutable).

List Operations

Python provides several operations to interact with lists, allowing you to access, modify, and remove elements.

  • aList[i]: Accesses the element at index i in the list. Index is a numeric value which tells you where an element is within the data. The first item is at index 1, so aList[1].
  • x <- aList[i]: Assigns the value of aList[i] to the variable x.
  • aList[i] <- x: Assigns the value of x to aList[i].
  • aList[i] <- aList[j]: Assigns value of aList[j] to aList[i].
  • INSERT(aList, i, value): Inserts the value into aList at index i.
  • APPEND(aList, value): Adds value to the end of the list.
  • REMOVE(aList, i): Removes the item at index i from aList.
  • LENGTH(aList): Evaluates the number of elements in aList.

Here’s an example of these operations in action aList = [1, 2, 3, 4] aList[2] = 5 # Changes the 3rd element from 3 to 5 aList.append(6) # Adds 6 at the end aList.insert(1, 10) # Inserts 10 at index 1 print(aList) # Output: [1, 10, 2, 5, 4, 6]

Find the minimum value in a list

  • Create or access the list.
  • Make a variable to hold the minimum and set it to a potential minimum value.
  • Loop through the list.
  • Check each element to see if it is less than the minimum variable.
  • If the element is less than the minimum variable, update the minimum.
  • After all elements of the list have been checked, display the minimum value.

Psuedocode

nums ← [65, 89, 92, 35, 84, 78, 28, 75] min ← nums [1] FOR EACH score IN nums: { IF(score < min) {
min = score } } DISPLAY (min)

Sum of Even Numbers of a list in Python

nums ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sum ← 0 FOR EACH score IN nums { IF (score MOD 2 = 0) { even_sum ← even_sum + score } } DISPLAY (“Sum of even numbers in the list:”, even_sum)

%%js
// Javascript

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let even_sum = 0;

for (let score of nums) {
    if (score % 2 === 0) {
        even_sum += score; // Add score to even_sum if it's even
    }
}

console.log("Sum of even numbers in the list:", even_sum);
<IPython.core.display.Javascript object>
# python

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_sum = 0
for score in nums:
    if score % 2 == 0: # Check if the remainder when divided by 2 is 0 (even number)
        even_sum += score # If previous requirement is fulfilled, add to sum
print("Sum of even numbers in the list:", even_sum)
Sum of even numbers in the list: 30