This is the second session of my computer programming class at Shishuvan.
The way we declare variables and functions in Python is called snake case. It means that you join words with an underscore (_), like this, snake_case. We had talked about snake case in the first class, and how Python is a snake, and we how we were talking to the computer in snake language. And everyone knows who can talk with snakes, Harry Potter!
We started the class with quick recap of what we did last time, that is variables, functions and lists. We then decided write a function that used lists.
>>> def add_marks(marks): return marks + marks + marks >>> add_marks([20, 30, 40]) 90
The kids understood this alright. They recapped how we use indexes to address elements of a list, and how the first index is always zero and not one. Then I added another element to the list:
>>> add_marks([20, 30, 40, 10]) 90
I made them re-write the function for summing a list with four elements. There had to be a better way to do this, and we quietly introduced loops.
>>> def add_marks(marks): total_marks = 0 for mark in marks: total_marks = total_marks + mark return total_marks >>> add_marks([20, 30, 40, 10]) 100
As expected, there was a flurry of questions
Repetition and Induction
This is when it struck me that the kids were understanding what the function is doing, what they were figuring out is the how. They were learning by induction. And isn’t that how we learn anything? By repetition and induction. As humans we are hard-wired to mimic other people and then we bring in our own variations so that we start learning what we are actually doing.
That was what these kids were doing. They had no idea what loops were or what the for statement did, but they kind of, got it.
They had soon typed it out and then asked a whole bunch of questions, like about indentation. Like why the return statement needs to be out-dented and other things.
I challenged everyone to make a function that returns the average of the list. And someone did actually come up with this:
>>> def make_average(marks): return add_marks(marks) / len(marks)
This was super cool. They could now write functions that called other functions!
Working with Strings
To reinforce loops, we did another example, this time, we added strings. I first told them that we could add strings like numbers with a few examples and then we made a function.
>>> def say_hello(students): student_names = '' for student in students: student_names = student_names + ' ' + student + ',' return 'Hello' + student_names[:-1] + '. How are you?' >>> say_hello(['Harry', 'Hermione', 'Ron']) Hello Harry, Hermione, Ron. How are you?
As you expect, the were already starting to make their variations, in function names, student names, the output text. Repetition, induction, then variation.
Since the function got longer, many students were getting a bunch of error messages, and this was a good way to learn reading them. They were mostly related to typos and passing strings without the quotes, but this was great too, because they were starting to learn nuances, like, you can’t make spelling mistakes, strings are different from names and commands.
Finally it was time to teach them Python modules. We copied some of our functions into a text editor and then we called the file “wizard.py”. Then we imported the file and called the functions.
>>> import wizard >>> wizard.add_marks([20, 30, 40])
This was also real cool, because now they realized that they could “teach” the computer a bunch of tricks and the computer can remember them.
I asked if they were feeling like wizards already, and a few hands shot up!
This session went of pretty well too. We have already started to see a lot of variation in the kids. Some kids were just zooming ahead, they understood the repetition-induction-variation system. Many were afraid to try, and this system works really well only if you feel its okay to fail. For most of us, the cost of failure is very high (exams!) and that means that we end up becoming dumb followers and not-trying. Some of the challenges would be to get these kids to try out different stuff and fail.
Since we are already in Harry Potter mode, in the next class we have decided that we are going to make a sorting hat and divide everyone in groups! Lets see how that goes.