Code Avengers Answers Python 2 New Site
Code Avengers Python Level 2 (often referred to as the updated "new" version) is an intermediate course covering lists, dictionaries, functions, and control flow for advanced logic. The curriculum aligns with educational standards, focusing on practical skills like creating custom functions for area calculations and building interactive quizzes. For more details, visit Code Avengers Blog Code Avengers Learn Python With Code Avengers
Our level 2 course, will teach you how to make your code more versatile by looking at data structures like lists and dictionaries, Code Avengers Learn Python With Code Avengers
If you're looking for general guidance or answers to common questions about Python 2, here are a few areas that might be helpful:
Challenge 2.2: Sum of Even Numbers (New Data Filtering)
Prompt:
“Given a list numbers = [12, 3, 5, 8, 10, 7], use a for loop to calculate and print the sum of only the even numbers.”
Answer:
numbers = [12, 3, 5, 8, 10, 7]
total = 0
for num in numbers:
if num % 2 == 0:
total += num
print(total) # Output: 30
Explanation: The modulo operator % returns the remainder. Even numbers have num % 2 == 0.
Lists
Lists are ordered collections of items.
# create a list
my_list = [1, 2, 3, 4, 5]
# access elements
print my_list[0] # prints 1
# modify elements
my_list[0] = 10
print my_list # prints [10, 2, 3, 4, 5]
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.”
Answer:
phonebook = {} while True: name = input("Enter name (or 'done'): ") if name == 'done': break number = input("Enter phone number: ") phonebook[name] = number
print(phonebook)
Scenario C: The Loop Challenge
Task: Use a for loop to print the numbers 1 to 5.
Answer:
for i in range(1, 6):
print(i)
Pro Tip: The range function stops before the second number. That's why we use 6 to print up to 5.
Introduction
Python 2 is a high-level, interpreted programming language. Although it's an older version, understanding Python 2 can still be beneficial for specific tasks or working with legacy code.
Table of Contents
- Introduction
- Basic Syntax
- Variables and Data Types
- Control Structures
- Functions
- Lists and Tuples
- Dictionaries
3. Loops (for and while)
Level 2 introduces how to repeat code so you don't have to copy-paste lines 100 times.
- For Loops: Used when you know exactly how many times you want to repeat something (e.g., "Repeat 5 times").
- While Loops: Used when you want to repeat until a condition changes (e.g., "Keep playing while lives > 0").
Lists
- Q: How do you access and modify list elements?
- A: You can access elements by their index and modify them by assigning a new value to that index. For example:
my_list = [1, 2, 3] print(my_list[0]) # Accessing the first element my_list[0] = 10 print(my_list) # Modifying the first element
- A: You can access elements by their index and modify them by assigning a new value to that index. For example: