Code Avengers Answers Python 2 New -
def rect_area(length, width): return length * width def rect_perimeter(length, width): return 2 * (length + width)
temps = [32, 35, 28, 30, 40, 29] hot_temps_f = [(c * 9/5) + 32 for c in temps if c > 30] print(hot_temps_f) [89.6, 95.0, 104.0] Module 4: Dictionaries – Real-World Data (The "New" Practical Approach) The old curriculum asked you to create static dictionaries. The new course requires user-built dictionaries from multiple inputs. Challenge 4.1: Phonebook Builder Prompt: “Write a program that repeatedly asks for a 'name' and 'phone number'. Store them in a dictionary. Stop when the user types 'done' for name. Then print the full dictionary.” code avengers answers python 2 new
def is_palindrome(word): cleaned = word.lower() return cleaned == cleaned[::-1] print(is_palindrome("Racecar")) # True def rect_area(length, width): return length * width def
original = [1, 2, 3, 4, 5] squared = [x**2 for x in original] print(squared) [1, 4, 9, 16, 25] Challenge 3.2: Filtering and Modifying (New Combined Logic) Prompt: “From temps = [32, 35, 28, 30, 40, 29] , create a new list hot_temps_f that contains only temperatures above 30°C, but converted to Fahrenheit (multiply by 9/5 and add 32).” Store them in a dictionary
numbers = [12, 3, 5, 8, 10, 7] total = 0 for num in numbers: if num % 2 == 0: total += num print(total) # Output: 30 The modulo operator % returns the remainder. Even numbers have num % 2 == 0 . Module 3: Intermediate Lists & List Comprehensions (New to Python 2) Previously, list comprehensions were taught in Python 3 courses only. The new Python 2 curriculum introduces them as an "advanced but preferred" method. Challenge 3.1: Squaring Numbers with Comprehension Prompt: “Take original = [1, 2, 3, 4, 5] and create a new list squared where each number is squared. Then print squared . Do not use a traditional for loop.”
area = rect_area(5, 3) perimeter = rect_perimeter(5, 3) print(f"Area: area, Perimeter: perimeter") Prompt: “Create a function is_palindrome(word) that returns True if the word reads the same forwards and backwards, ignoring case. Test it with 'Racecar'.”