Python 3D Academyবাংলা

How Python Stores Things

Chapter 13: How Python Stores Things

5 short lessons, with 3D animations, quizzes and coding tasks.

The lessons

  1. Names point to objects

    A variable is a name tag with an arrow to an object, so b = a doesn't copy anything: it ties a second tag to the same object.

  2. Things that can change, and things that can't

    Mutable objects (lists, dictionaries, sets) can be changed in place; immutable ones (numbers, strings, tuples) can't, so "changing" them really makes a new object and moves the tag.

  3. Making copies

    a.copy(), list(a) and a[:] make a new outer list, so changing the copy leaves the original alone; copy.deepcopy(a) also copies everything inside.

  4. is, == and None

    Use == to compare values, and keep is for checking whether something is None, Python's "no value here".

  5. Project: a class grade book

    At the end of term, Ms Rahman has a sheet of marks: each student's Bangla, English and Maths. She needs each student's average and grade, the top student, the class average for each subject, and who needs extra help because they scored below 33 in a subject. By hand, that's an evening with a calculator. In this project you'll build her grade book. It uses everything from World 2: loops, lists, unpacking, dictionaries and comprehensions. The plan in one sentence: read the marks into a dictionary that maps each name to a list of marks, then answer every question with one pass over it. Its words: data structure: how the data is arranged in memory; here, {"Rimi": [78, 85, 90], ...} record: everything about one student: their name and their marks report: the lines the program prints Step by step, the plan: 1. Read: the first line is how many students; each next line is a name and three marks, like Rimi 78 85 90. 2. Store: name, marks = line.split() splits a record; the marks become numbers with a comprehension. 3. *Per student: average, grade, and the best average so far. 4. **Per subject: the class average of each column, and everyone with a mark below 33. After step 3 the averages could sit in their own dictionary, one locker per student: Steps 1 and 2: read and store. A starred name collects the leftovers (Lesson 9.2), so name, marks = "Rimi 78 85 90".split() gives name = "Rimi" and marks = ["78", "85", "90"]. They're still strings, so a comprehension turns them into numbers. A dictionary is the right home: you can look a student up by name instantly, and each name appears once. *Step 3: grades and the top student.* An if/elif chain turns each average into a grade, strictest first. The top student is the "best so far" pattern from Lesson 11.3. Starting best at -1 is safe because no average can be below 0. *Step 4: subjects.* Mark i of every student is one subject's column: [marks[i] for marks in book.values()]. A filtered comprehension finds everyone with a mark below 33. *Why build it this way, and what it costs.* The reading loop runs once per student. Each report is one more pass: per student, then per subject. For a class of n students, that's a few passes of n steps, so even a whole school of 2,000 students is instant. Looking a student up by name (book["Rimi"]) is one quick dictionary lookup, however big the school. *Mistakes and edge cases.* *Two names for three marks.* name, marks = line.split() gives ValueError: too many values to unpack (expected 2, got 4). The star collects the rest. *Forgetting int().* sum(["78", "85"]) gives TypeError: unsupported operand type(s) for +: 'int' and 'str'. *A student with no marks.* sum([]) / len([]) divides by zero; the input format promises three marks, but real data might not. *Two students with the same name.* The second one overwrites the first in the dictionary. Real systems use a roll number as the key. *Ties for top.** With >, the first student with the best average wins.