Watch Me Code 1: List Enumeration & Aggregates


In [6]:
quizzes = [5,5,4,2,2,4,5,3]

In [2]:
# definiate loop
for quiz in quizzes:
    print("Grade:", quiz)


Grade: 5
Grade: 5
Grade: 4
Grade: 2
Grade: 2
Grade: 4
Grade: 5
Grade: 3

In [3]:
# definite loop with indexes
print(len(quizzes))
for i in range(len(quizzes)):
    print("Quiz %d Grade: %d" %( i, quizzes[i]))


8
Quiz 0 Grade: 5
Quiz 1 Grade: 5
Quiz 2 Grade: 4
Quiz 3 Grade: 2
Quiz 4 Grade: 2
Quiz 5 Grade: 4
Quiz 6 Grade: 5
Quiz 7 Grade: 3

In [7]:
# indexes
#print(quizzes[0]) # first
#print(quizzes[-1]) # last

print(quizzes)
# mutable
quizzes[1] = 0
print(quizzes)

#slices 
print(quizzes[0:3])
print(quizzes[3:])


[5, 5, 4, 2, 2, 4, 5, 3]
[5, 0, 4, 2, 2, 4, 5, 3]
[5, 0, 4]
[2, 2, 4, 5, 3]

In [8]:
# aggregations
print("Count",len(quizzes))
print("Total", sum(quizzes))
print("Average", sum(quizzes)/len(quizzes))


Count 8
Total 25
Average 3.125

In [9]:
grades = ["A", "B+", "A", "C+", "B-"]
grades[:2]


Out[9]:
['A', 'B+']

In [10]:
print(grades)


['A', 'B+', 'A', 'C+', 'B-']

In [ ]:
grades.append('')