Sum of Even Numbers of a list

Pseudocode

Intro:

  • The code calculates the sum of even numbers from a list of numbers (nums). It uses a loop to iterate through the list, checks if each number is even (i.e., divisible by 2), and adds it to the variable even_sum if true. After completing the loop, it displays the sum of all even numbers in the list.

Step by Step process:

  1. A list nums contains the numbers 1 through 10.
  2. The variable even_sum is initialized to 0.
  3. The code iterates through each number in nums.
  4. If the number is even (checked using score % 2 == 0), it’s added to even_sum.
  5. After all even numbers are summed, the result (30) is printed.

Homework:

  1. Create a list: Create a list which has a yes or no answer and 2 questions.
  2. Convert the code: Make the code in Python for easier accessibility.
sum -> 15
nums <- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
even_sum  0

FOR EACH score IN nums
    IF score MOD 2 = 0 THEN
        even_sum  even_sum + score
    END IF
END FOR

DISPLAY ("Sum of even numbers in the list:", even_sum)

This code is run in pseudocode, it cannot be directly run by any interpreter or compiler. 
However, pseudocode is meant to be easily translated into real programming languages like Python, Java, C++, etc. 
It is used to describe algorithms in a simple, readable format without worrying about syntax rules of any specific language.
  Cell In[1], line 3
    even_sum ← 0
             ^
SyntaxError: invalid character '←' (U+2190)

Interpreting In python

  • Psueodocode can be used to interpret into python, below will show what this code looks like in python
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
even_sum = 0

for score in nums:
    if score % 2 == 0:
        even_sum += score

print("Sum of even numbers in the list:", even_sum)

Sum of even numbers in the list: 56
Creating a List
Python: my_list = [1, 2, 3]  # Initializes a list with elements 1, 2, and 3
Pseudo Code: my_list  [1, 2, 3]  # Initializes a list with elements 1, 2, and 3

Accessing Elements
Python: value = my_list[index]  # Retrieves the element at the specified index from my_list
Pseudo Code: value <- my_list[index]  # Retrieves the element at the specified index from my_list

Appending Elements
Python: my_list.append(new_value)  # Adds new_value to the end of my_list
Pseudo Code: Append new_value to my_list  # Adds new_value to the end of my_list

Inserting Elements
Python: my_list.insert(index, new_value)  # Inserts new_value at the specified index in my_list
Pseudo Code: Insert new_value at index in my_list  # Inserts new_value at the specified index in my_list

Removing Elements
Python: my_list.remove(value)  # Removes the first occurrence of value from my_list
Pseudo Code: Remove value from my_list  # Removes the first occurrence of value from my_list

Modifying Elements
Python: my_list[index] = new_value  # Replaces the element at the specified index with new_value
Pseudo Code: my_list[index] <- new_value  # Replaces the element at the specified index with new_value

Checking Length
Python: length = len(my_list)  # Gets the number of elements in my_list
Pseudo Code: length <- Length of my_list  # Gets the number of elements in my_list

Iterating through a List
Python: for item in my_list:  # Loops through each item in my_list
{ <block of statement> }  # Executes the block of statements for each item
Pseudo Code: For each item in my_list:  # Loops through each item in my_list
{ <block of statement> }  # Executes the block of statements for each item

Checking for Existence
Python: if value in my_list:  # Checks if value exists in my_list
{ <block of statement> }  # Executes the block of statements if value is found
Pseudo Code: If value is in my_list:  # Checks if value exists in my_list
{ <block of statement> }  # Executes the block of statements if value is found

List Slicing
Python: sub_list = my_list[start:end]  # Creates a new list from my_list starting at start index to end index (exclusive)
Pseudo Code: sub_list <- my_list[start:end]  # Creates a new list from my_list starting at start index to end index (exclusive)

  Cell In[1], line 3
    Pseudo Code: my_list ← [1, 2, 3]  # Initializes a list with elements 1, 2, and 3
                         ^
SyntaxError: invalid character '←' (U+2190)

Homework

  • Make a list in pseudocode of 30 numbers and find the sum of all even numbers, then the sum of all odd numbers.
  • After doing this, convert the code into Python.