Loops & Lists — From Variables to Student Records

Introduce `for` loops and Python lists; refactor a student “mini-automation” from many variables to list-based data.

Learning Objectives

  • Iterate with for loops using range() and direct iteration over sequences.
  • Create and manipulate Python lists: indexing, slicing, len(), append, insert, remove, pop.
  • Recognize the limitations of “one-variable-per-student” and refactor to list-based structures.
  • Use parallel lists (e.g., names, surnames, grades) and iterate over them (with range or zip).

Session Plan (theory)

  1. Motivation: the scaling problem
    • Naive approach:
      name1 = "Ali"; surname1 = "Yılmaz"; grade1 = 86
      name2 = "Ayşe"; surname2 = "Demir"; grade2 = 92
      # … quickly becomes unmanageable as student count grows
      
    • Why this doesn’t scale; benefits of sequences.
  2. for loops
    • Iterating with for x in sequence: vs for i in range(n):.
    • Off-by-one pitfalls; range(start, stop, step).
  3. Lists
    • Creating lists; indexing & slicing; len().
    • Adding/removing items: append, insert, remove, pop.
    • Iterating over values and over indices; using zip for parallel lists.
  4. Parallel lists for simple records
    • names = [...], surnames = [...], grades = [...].
    • Printing aligned rows; computing statistics (min, max, average).

Hands-on

  • Mini Project: Student Registry v1 (Lists & Loops)
    • Start from an empty repo folder or continue last week’s.
    • Build three lists: names, surnames, grades (seed a few entries).
    • Tasks:
      1. Print all students as # index - Full Name - Grade.
      2. Compute class size, average grade, best/worst grade (show name + value).
      3. Add a new student (append to each list) and reprint the table.
      4. Remove a student by index (use pop) and reprint.
      5. Bonus: use zip(names, surnames, grades) to print full rows without indices.