Practice Tasks
Practice Tasks
# Example usage
check_age(15) # Output: Minor
check_age(30) # Output: Adult
check_age(70) # Output: Senior
Minor
Adult
Senior
1
# Example usage
print(sum_even_numbers([1, 2, 3, 4, 5, 6])) # Output: 12
12
# Example usage
countdown(5)
5
4
3
2
1
Liftoff!
# Example usage
fizz_buzz(15)
2
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
# Example usage
print(factorial(5)) # Output: 120
120
3
return s_replaced, vowel_count
# Example usage
print(string_manipulation("Hello World")) # Output: ('HELLO_WORLD', 3)
('HELLO_WORLD', 3)
# Example usage
print(list_operations([4, 2, 3, 1, 5])) # Output: [1, 3, 4, 5, 42]
[1, 3, 4, 5, 42]
# Example usage
print(tuple_operations((1, 2, 3), (4, 5, 6))) # Output: ((1, 2, 3, 4, 5, 6),␣
↪6, 1)
((1, 2, 3, 4, 5, 6), 6, 1)
4
Instructions: 1. Implement a function named dictionary_operations that takes a dictionary
and a key-value pair. 2. Perform the following operations: - Add the key-value pair to the dictionary.
- If the key already exists, update its value. - Remove a key called “remove_me” if it exists. 3.
Return the modified dictionary.
# Example usage
print(dictionary_operations({"a": 1, "b": 2}, "c", 3)) # Output: {'a': 1, 'b':␣
↪2, 'c': 3}
# Example usage
print(set_operations({1, 2, 3}, {3, 4, 5})) # Output: ({1, 2, 3, 4, 5}, {3},␣
↪{1, 2})
0.3 3 - Combined
0.3.1 Task 1: Grade Calculator
Objective: Use conditionals, lists, and functions to calculate grades.
Instructions: 1. Write a function named calculate_grade that takes a list of numerical scores.
2. Calculate the average score. 3. Use conditionals to assign a letter grade based on the average
score: - 90-100: “A” - 80-89: “B” - 70-79: “C” - 60-69: “D” - Below 60: “F” 4. Return the letter
grade.
5
[11]: def calculate_grade(scores):
average = sum(scores) / len(scores)
if average >= 90:
return "A"
elif average >= 80:
return "B"
elif average >= 70:
return "C"
elif average >= 60:
return "D"
else:
return "F"
# Example usage
print(calculate_grade([85, 90, 78, 92, 88])) # Output: "B"
def average_scores(records):
averages = {}
for student, scores in records.items():
averages[student] = sum(scores) / len(scores)
return averages
# Example usage
students = {}
add_student(students, "Alice", [90, 85, 88])
add_student(students, "Bob", [70, 75, 80])
print(average_scores(students)) # Output: {'Alice': 87.66666666666667, 'Bob':␣
↪75.0}
6
the string to lowercase and split it into words. 3. Use a dictionary to count the frequency of each
word. 4. Return the dictionary with word frequencies.
# Example usage
print(word_frequency("This is a test. This test is only a test.")) # Output:␣
↪{'this': 2, 'is': 2, 'a': 2, 'test.': 2, 'only': 1}
def calculate_total(cart):
total = sum(cart.values())
return total
# Example usage
cart = {}
add_to_cart(cart, "apple", 1.5)
add_to_cart(cart, "banana", 2.0)
add_to_cart(cart, "apple", 1.5)
print(calculate_total(cart)) # Output: 5.0
5.0
7
Instructions: 1. Write a function named add_book that takes a set representing a library collection
and a tuple representing a book (title, author). 2. Add the book to the collection. 3. Write another
function named find_books_by_author that takes the collection and an author’s name, and returns
a list of books by that author.
# Example usage
library = set()
add_book(library, ("1984", "George Orwell"))
add_book(library, ("Animal Farm", "George Orwell"))
add_book(library, ("Brave New World", "Aldous Huxley"))
print(find_books_by_author(library, "George Orwell")) # Output: [('1984',␣
↪'George Orwell'), ('Animal Farm', 'George Orwell')]