Thursday, October 22, 2015

Python for Kids

I am a believer that kids should start early on programming.  I started my son with raspberry pi and he showed a lot of interest in Scratch programming. That was relatively easy for him. I find that modern days programming is much easier than before. No need to check or track the if statement, for statement etc.... The editor is even graphic base and he just need to drag and drop. Everything comes with graphics user interface. He can easily create a few games for himself.

Now that his major exam is over, I started him with Python as I read that Python is a relatively easy book.



I download the program from this website. I installed the program on Mac while he installed on another windows machine. Everything was very smooth sailing. In fact, perhaps he could have done that all by himself. The next question is, how to teach him the program, as I don't have much time to do coding anymore. So, I took up a promotion from Udemy and sign up tthe "Python for Beginner" course for $10. I am sure that there are other good courses too.  To be fair, the program is quite good, easy to understand. But, my son just skip everything!

He did those exercise provided by the course. He just wants challenge. So, I told him to write a program to generate as many prime number as a user wanted. Immediately he came up with this program:

-------first program---------------
import time
print('How many prime numbers would you like me to list for you?')
limit=int(input())
t1=time.time()
prime_numbers=[2]
a=2
numbers=1
while True:
    a=a+1
    b=prime_numbers[0]
    x=0
    while True:
        ans=(a/b)
        if ans==int(ans):
            break
        b=b+1
        if a==b:
           prime_numbers.append(a)
           numbers=numbers+1
    if numbers==limit:
        print(prime_numbers)
        break
t2=time.time()
print('I took %s seconds, definitely faster than you!' % (t2-t1) )

----------------------

He picks up the language and become familiar with this much faster than I do. I did not touch programming for many years and I was struggling to complete the program. I told him about the concept of programming time and taught him about algorithm that he did wrongly above. Just 30 minutes later, he rectify with this version which is much faster than the previous one:

 --------------faster version-------
import time
print('How many prime numbers would you like me to list for you?')
limit=int(input())
t1=time.time()
prime_numbers=[2]
a=2 #a is the number to be tested
numbers=1 #number is the no. of numbers in the list
while True:
    a=a+1
    x=0
    d=0
    for y in range(0,numbers):
        c=prime_numbers[x]
        ans=(a/c)
        if ans==int(ans):
            break
        else:
            d=d+1
        x=x+1
    if d==numbers:
        numbers=numbers+1
        prime_numbers.append(a)
    if numbers==limit:
        print(prime_numbers)
        break
t2=time.time()
print('I took %s seconds, definitely faster than you!' % (t2-t1) )

--------------faster version-------
What I want to share here is not about the achievement. But about how fast younger generation can learn. They no longer look at books.  As you notice, he use "list". For me, I was worrying whether he knows how to use that. I mentioned "list", he found that from his book, and "tah-dah".... 30 min later, the first program was done using list.

While he completed the faster version in 30min, I was still struggling to debug mine. So, lesson learned, we parents should not write program with them.  But what we can contribute is the algorithm part.

So, I challenged him to write a multiplication training program for his sister. He breezed through that and came up with the following:

--------------------
import time
import random
import pickle
from tkinter import*
def start_now():
    print('3')
    time.sleep(1)
    print('2')
    time.sleep(1)
    print('1')
    time.sleep(1)
    print('START!')
    t1=time.time()
    score=0
    for x in range(0,10):
        a=random.randint(2,9)
        b=random.randint(2,9)
        ans=a*b
        print('_____')
        print('  %s' % (a) )
        print('x %s' % (b) )
        i=int(input())
        if ans==i:
            score=score+1
    t2=time.time()
    y=t2-t1
    y=round(y,2)
    if (t2-t1)<=30 and score>8.9:
        print("Not bad, you took %s seconds" % (y))
    else:
        print("You took %s seconds, better luck next time" % (y))
    print("Your score is %s" % (score))
    with open("progress.txt", "a") as f:
        f.write("On %s:\n" % (time.asctime()))
        f.write("-ykh completed the drill in %s seconds\n" % (y))
        if score==10:
            f.write("-ykh got all correct! :)\n")
        else:
            f.write("-ykh got %s correct\n" % (score))
        f.write('\n')
tk=Tk()
btn=Button(tk,text='start drill', command=start_now)
btn.pack()

--------------------
Luckily I  did not kill his interest by giving him a class, or insist him to go through step by step all those functions through Udemy courses. Millennium kids really learn programming very differently. So, we just show the way.  Running out of idea, I direct him to this book: 



I think I better get lost and leave him alone when he plays with his new play ground using Python. I am sure he will like minecraft and get a lot of ideas on what to do from the book below. So, I hope this post gives you an idea if you want to teach your kids programming.





No comments: