JIT — Intro to Data Science · Python Lab · Problem 8 of 30

One student's record

A box where every value is filed under a name you chose — and where a mistyped name creates a new field instead of an error.

Everything about one student, in one box

A list holds many marks, but it does not know what any of them mean. marks[2] is the third mark — of what subject, for which student? You have to remember, and remembering is exactly what goes wrong at two in the morning.

A dictionary holds values the same way a list does, but each one is filed under a name you chose rather than a position you have to count to.

New word — dictionary Pairs of key and value inside curly brackets, written key first, then a colon, then the value:
student = {'name': 'Anjali More', 'roll': 17}
The key is the label you file it under. The value is the thing itself. Keys are usually short pieces of text and must be exact — 'marks' and 'Marks' are two different labels, as far apart as two different words.
Reaching a value Square brackets again, but with the key inside instead of a position: student['name'].
student[0] does not mean the first item. A dictionary has no first item to speak of — it only has labels — so Python goes looking for a key called 0, fails to find one, and stops.
Asking for something that might not be there student['grade'] stops the programme if there is no grade recorded. Often that is what you want: a missing field is a real problem and you would rather know.
But sometimes it is normal for a field to be absent. student.get('grade', 'not recorded') hands back the grade if it exists and your fallback if it does not, and the programme carries on. Choosing between these two is a judgement about your data, not about Python.
Careful — writing to a key that does not exist With a list, reaching past the end is an error. With a dictionary it is not. Writing to a key that was never there simply creates it.
That is how you add a field on purpose. It is also how a typo silently gives you a record with both 'marks' and 'Marks' in it, one of them right and one of them ignored by everything else you write.

What you are building

The whole record: {'name': 'Anjali More', 'roll': 17, 'city': 'Nagpur', 'marks': 78} Fields recorded: ['name', 'roll', 'city', 'marks'] Name: Anjali More Marks now: 78 After the recheck: {'name': 'Anjali More', 'roll': 17, 'city': 'Nagpur', 'marks': 85} Is grade recorded? False Looking up grade: not recorded With a brand new field: {'name': 'Anjali More', 'roll': 17, 'city': 'Nagpur', 'marks': 85, 'grade': 'A'}
Coming later One record is a dictionary. A whole class is a list of them — and a list of dictionaries, with the same keys in each, is very nearly a table. That is what a pandas DataFrame is, in problem 20.

Build the programme

Your plan

The steps still to place

The whole programme is laid out below. Five pieces are missing. Watch the two that involve a key you may have typed slightly wrong — a dictionary will not warn you about either of them.

Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.

Run and read

Here is the whole programme. The name, the marks and the field to look up are all yours to change. Try looking up a field that exists and one that does not.

Your programme

Output

Nothing has run yet.