Outline of Content and Hacks

3.10. Lists

3.10.1 List Operations - Alex Gustaf

  • Assigning Values: Assign values to list elements.
  • Inserting Elements: Insert elements at specific positions in the list.
  • Appending Elements: Append new elements to the end of the list.
  • Removing Elements: Remove elements from the list.
  • Calculating Length: Calculate the length of the list.

3.10.2 Pseudocode: Sum of Even Numbers in a List - Daksha Gowda

  • Variables: Define necessary variables for calculation.
  • Create Number List: Initialize a list of numbers to work with.
  • Modulus Operator: Use the modulus operator to identify even numbers.
  • Control Structures: Implement for loops and if statements for iteration and condition checking.

3.10.3 Hacks: Functions - Darsh

  • Find Max and Min: Create a function to identify the largest and smallest numbers in a list.
  • Iterate Through the List: Utilize loops to process each element effectively.

3.10.4 Hacks: Lists - Zachary Peltz

  • User Input: Accept input from the user to populate the list.
  • Element Attributes/Values: Explore the attributes or values associated with list elements.

Popcorn Hack #1

  • Take user input with input() function and append data to a list
  • Add all numbers in list and print sum
  • Allow removing of values using .pop method (removes last element in list)
print("Adding Numbers In List Script")
print("-"*25)
numlist = []
while True:
    start = input("Would you like to (1) enter numbers (2) add numbers (3) remove last value added or (4) exit: ")
    if start == "1":
        val = input("Enter a numeric value: ") # take input while storing it in a variable
        try: 
            test = int(val) # testing to see if input is an integer (numeric)
        except:
            print("Please enter a valid number")
            continue # 'continue' keyword skips to next step of while loop (basically restarting the loop)
        numlist.append(int(val)) # append method to add values
        print("Added "+val+" to list.")
    elif start == "2":
        sum = 0
        for num in numlist: # loop through list and add all values to sum variable
            sum += num
        print("Sum of numbers in list is "+str(sum))
    elif start == "3":
        if len(numlist) > 0: # Make sure there are values in list to remove
            print("Removed "+str(numlist[len(numlist)-1])+" from list.")
            numlist.pop()
        else:
            print("No values to delete")
    elif start == "4":
        break # Break out of the while loop, or it will continue running forever
    else:
        continue



Adding Numbers In List Script
-------------------------
Sum of numbers in list is 0
Sum of numbers in list is 0
Sum of numbers in list is 0
No values to delete
No values to delete

Popcorn Hack #2

using Pseuodcode, make a list of the number 1 through 100 and find the sum of all even numbers. Then use python and change your code into python

See Answer


### Pseudocode
   nums ← 1 to 100
odd_sum ← 0

FOR EACH score IN nums
    IF score MOD 2 ≠ 0 THEN
        odd_sum ← odd_sum + score
    END IF
END FOR

DISPLAY ("Sum of odd numbers in the list:", odd_sum)

### now in python
nums = range(1, 101)  # This creates a range of numbers from 1 to 100
odd_sum = 0

for score in nums:
    if score % 2 != 0:
        odd_sum += score

print("Sum of odd numbers in the list:", odd_sum)

Popcorn Hack #3

Tasklist Generator

This Python code creates an empty list and prompts the user to append or add to the list by typing in Classes which they may have homework for. The code then prompts to finish the input whenever they feel like it. This helps as a task planner that can give an overall and concise representation of the classes with homeworks inputted.

Code

def gather_tasks():
    tasks = []
    print("Enter your tasks (type 'done' to finish):")
    
    while True:
        task = input("Task: ")
        if task.lower() == 'done':
            break
        tasks.append(task)
    
    return tasks

def display_tasks(tasks):
    count = len(tasks)
    print(f"You have {count} task(s) on your list:")  
    for task in tasks:No col
        print(f"- {task}")  

if __name__ == "__main__":
    user_tasks = gather_tasks()
    display_tasks(user_tasks)

Enter your tasks (type 'done' to finish):
You have 5 task(s) on your list:
- Homework for calc
- Homework for APEL
- Homework for CSP
- Homework for chem
- Homewok for history