3.10 Homework
3.10 Lists Homework
3.10.1 List Operations
data_list = ["space hamster", "invisible penguin", 8675309, "dancing taco", "floating beard", 3.14159, "sparkly vampire", "giant rubber duck"]
print("List:", data_list)
try:
index = int(input("Enter the index to remove: "))
if 0 <= index < len(data_list):
print("Removed:", data_list.pop(index))
else:
print("Invalid index.")
except ValueError:
print("Please enter a valid number.")
print("Updated list:", data_list)
List: ['space hamster', 'invisible penguin', 8675309, 'dancing taco', 'floating beard', 3.14159, 'sparkly vampire', 'giant rubber duck']
Removed: dancing taco
Updated list: ['space hamster', 'invisible penguin', 8675309, 'floating beard', 3.14159, 'sparkly vampire', 'giant rubber duck']
3.10.2 List Pseudocode
Sum of Even Numbers of a list
sum -> 15 nums <- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, …, 29, 30] 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.
Sum of Odd Numbers of a list
sum -> 15 nums <- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, …, 29, 30] odd_sum ← 0
FOR EACH score IN nums IF score MOD 2 = 1 THEN odd_sum ← odd_sum + score END IF END FOR
DISPLAY (“Sum of odd numbers in the list:”, odd_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.
Interpreting In python
# Even sum
nums = list(range(1, 31)) # This creates a list of numbers from 1 to 30
even_sum = 0
for score in nums:
if score % 2 == 0:
even_sum += score
print("Sum of even numbers in the list:", even_sum)
# Odd sum
nums = list(range(1, 31)) # This creates a list of numbers from 1 to 30
odd_sum = 0
for score in nums:
if score % 2 == 1:
odd_sum += score
print("Sum of odd numbers in the list:", odd_sum)
Sum of even numbers in the list: 240
Sum of odd numbers in the list: 225
3.10.3 List Functions
hobbies = ["Tennis", "Basketball", "Football", "Soccer", "playing video games", "Fishing", "Golf"]
# Loop through each hobby and print it
print("My favorite hobbies:")
for hobby in hobbies:
print("I like to engage myself with " + hobby)
My favorite hobbies:
I like to engage myself with Tennis
I like to engage myself with Basketball
I like to engage myself with Football
I like to engage myself with Soccer
I like to engage myself with playing video games
I like to engage myself with Fishing
I like to engage myself with Golf
3.10.4 List Input
# List of questions with yes/no answers about daily habits
questions = [
"Do you like to play Tennis? (yes/no)",
"Do you like to play Basketball? (yes/no)"
]
# Loop through each question and get the user's response
for question in questions:
answer = input(question + " ") # Prompt user for their answer (yes/no)
# Provide feedback based on the answer
if answer.lower() == 'yes':
print("That's interesting!")
elif answer.lower() == 'no':
print("ooh that's a pretty unique opinion!")
else:
print("Please answer with 'yes' or 'no'.")
That's interesting!
ooh that's a pretty unique opinion!