Skip to content

Lakshmi Prasad

@lakshmi_teaches_py

Maths teacher (25 years) in Mysuru. Started teaching Python to my Class 9 students and now I am the one learning. Please be patient with me.

  • Mysuru, India
  • Kannada · English · Hindi
  • Joined Sep 2026
  • Last posted Oct 11, 2026
Posts
2
Followers
9
Following
13
Reputation
72

Posts

Everything this author has published.

8

  • Accepted answer

    Lakshmi, Anitha is right, and you are in good company: this has caught professional programmers for thirty years. Python evaluates a default value once, when the def line runs, not each time the function is called. So there is exactly one list, created when the function was defined, and every call that does not pass marks gets that same list and appends to it. You can show the children the shared list directly. After your three calls: print(add_mark.__defaults__) # ([45, 78, 62],) The usual fix is to use None as the default and make a fresh list inside: def add_mark(mark, marks=None): if marks is None: marks = [] marks.append(mark) return marks For fourteen year olds, try this: the default list is one notebook kept on the teacher's desk. Every student who does not bring their own notebook writes in the desk notebook. By the third student, the page is already full of other people's marks. The fix gives each student a new notebook when they arrive. It is also in the official Python tutorial, in the section on default argument values, with a warning in a box saying the default is evaluated only once. Showing them that box is a nice lesson in itself: the docs knew before we did. Cheers, G.
    1 reply
30 votes · Ended

  • Dear Lakshmi, Think Python. It is written by a teacher, for people who want to understand rather than just make things work, and it is free to read online. With your maths background you will enjoy the chapters on recursion. Then Automate the Boring Stuff, for fun. Cheers, G.
    1 reply

Best answers

Get the best of Dynamo Geeks weekly

One email each Monday with the most useful posts. No spam; unsubscribe in one click.