Lesson 2 practice questions
Question 1
Generate a list called twelve that contains numbers 1 through 12 and then afterwards, subset it to a list called even_numbers that contains only the even entries.
Hint
Google how to find the remainder of a division operation.
solution
twelve=[1,2,3,4,5,6,7,8,9,10,11,12]
even_numbers=list()
for i in twelve:
if i % 2 == 0:
even_numbers.append(i)
OR
even_numbers=list()
even_numbers=[i for i in twelve if i % 2 == 0]
OR
even_numbers=list(filter(lambda i: i % 2 == 0, twelve))
Question 2
Create a list called numeric grades that contains 90, 75, 80, 95, and 100. Then loop through numeric_grades and print the student's letter grade using the following criteria.
>=90: A<90 but >=80: B<80 but >=70: C<70 but >=60: D- Below 60: Failed
Hint
Use Google to find out how to make multiple comparisons within Python's elif statement.
numeric_grades=[90,75,80,95,100]
student_name=['Yoda', 'Cat', 'Dog', 'Mouse', 'Spock']
solution
for i in range(len(numeric_grades)):
if numeric_grades[i]>=90:
print(student_name[i], "got an A")
elif (numeric_grades[i]<90) & (numeric_grades[i]>=80):
print(student_name[i], "got a B")
elif (numeric_grades[i]<80) & (numeric_grades[i]>=70):
print(student_name[i], "got a C")
elif (numeric_grades[i]<70) & (numeric_grades[i]>=60):
print(student_name[i], "got a D")
else:
print(student_name[i], "Failed")