A value is a concrete piece of data in a program. Numbers, strings, and Boolean results are values. An object has a value, and an expression is code that can be reduced to one value. section 4.3 section 5.3
Values appear in several closely related programming ideas:
- A variable is a name bound to an object, whose value can be used later. section 4.3
- An expression such as 3 + 2 produces the value 5; assignment and printing are statements, not expressions. section 5.3
- A dictionary stores key-value pairs, where a key identifies the value returned by a lookup. section 27.1
- A dictionary value can itself be one object, such as a tuple or a list of grades. section 27.1 section 27.4
Values in a grocery dictionary
grocery = {"milk": 1, "eggs": 12, "bread": 2} Here, "milk", "eggs", and "bread" are keys. The values 1, 12, and 2 record each quantity. Looking up a key retrieves its corresponding value. section 27.1
Listing 27.3. Keeping track of student grades by using a dictionary
grades = {} #1
grades["Chris"] = [100, 70] #1
grades["Angela"] = [90, 100] #1
grades["Bruce"] = [80, 40] #1
grades["Stacey"] = [70, 70] #1
for student in grades.keys(): #2
print(student) #2
for quizzes in grades.values(): #3
print(sum(quizzes)/2) #2
for student in grades.keys(): #4
scores = grades[student] #5
grades[student].append(sum(scores)/2) #6
print(grades) #7
Trace how a dictionary stores student grade lists, reads values with a loop, calculates averages, and appends new values. section 27.4






