Find the minimum value in a list

The min() function is a function that returns the smallest item from an iterable (like a list). Here, min(my_list) scans through all the elements in aList and identifies the minimum value.

# Python

aList = [3, 1, 4, 1, 5, 9, 2]
minimum_value = min(aList)
print(minimum_value)  # Output: 1

You can also find the minimum through a loop. First, we initialize the variable minimum_value with the first element of the list, which is 3. This serves as our starting point for comparison. The for begins a for loop that iterates through each element in aList. The variable num will take on the value of each element in the list during each iteration. Inside the loop, we check if the current element (num) is less than the current minimum_value. If num is smaller, we update minimum_value to be num.

# Python
aList = [3, 1, 4, 1, 5, 9, 2]
minimum_value = aList[0]  # Start with the first element

for num in aList:
    if num < minimum_value:
        minimum_value = num

print(minimum_value)  # Output: 1

Sum of Even Numbers of a list

This code effectively demonstrates how to iterate through a list, check for even numbers, and calculate their sum using a simple conditional statement.

# 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)
%%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);

Homework

  • Find the sum of even numbers in the list aList = [2, 5, 6, 1, 9, 3]
  • Find the minimum and maximum values in the list aList = [2, 5, 6, 1, 9, 3]