Why use [] instead of () for non-list?


#1



https://www.codecademy.com/courses/python-beginner-en-qzsCL/0/4?curriculum_id=4f89dab3d788890003000096


I get the code correctly..I'm just wondering why print student(name) doesn't work and print student[name] does work. I thought that [] were only for lists and name just has one string associated with it, not a list


lloyd = {
    "name": "Lloyd",
    "homework": [90.0,97.0,75.0,92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

students = [lloyd, alice, tyler]

for student in students:
    print student["name"]
    print student["homework"]
    print student["quizzes"]
    print student["tests"]


#2

You call a function() with parens and refer to an item in a list with list[item]. In the case of a dictionary, which is like a list composed of key-value pairs, dictionary[key] returns the value that matches that key.


#3

Because the values given in the students list are dictionaries, we must treat them like so when trying to access data from them. The identifier for a value in a dictionary has this syntax: DICTIONARY[VALUE]


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.