Python for Kids - Extended Course
Total Sessions: 5
Target Age: 11-14
Prerequisites: Basics of Python (variables, if, for, while)
Session 1: Functions in Python
Objective: Learn how to create and use functions to organize code
What is a Function?
A function is a block of code that performs a specific task. It helps you reuse code and keep
programs organized.
Syntax:
def function_name(parameters):
# code block
return result (optional)
Topics Covered:
- Defining a function using def
- Calling a function
- Function with parameters
- Function with return value
Examples:
1. Simple function:
def say_hello():
print("Hello!")
say_hello()
2. Function with a parameter:
def greet(name):
print("Hello, " + name + "!")
greet("Ali")
3. Function with return value:
def add(a, b):
return a + b
result = add(3, 4)
print(result)
Practice Activities:
- Function to calculate the square of a number
- Function to convert Celsius to Fahrenheit
- Simple text-based calculator using functions
Session 2: GUI Basics with Tkinter (Part 1)
Objective: Create simple windows and understand the structure of a GUI
What is a GUI?
A GUI (Graphical User Interface) allows users to interact with programs using windows, buttons, and
text fields.
Basic Tkinter Components:
- Tk() - creates the main window
- Label - displays text
- Entry - allows user input
- Button - clickable buttons
- pack() - arranges the widgets
- mainloop() - keeps the window running
Examples:
1. Basic window:
from tkinter import *
window = Tk()
window.title("My First GUI")
window.mainloop()
2. Adding a Label and Button:
from tkinter import *
def say_hello():
print("Hello from GUI!")
window = Tk()
Label(window, text="Welcome!").pack()
Button(window, text="Click Me", command=say_hello).pack()
window.mainloop()
3. Entry Example:
from tkinter import *
def show_name():
name = entry.get()
print("Hello, " + name)
window = Tk()
entry = Entry(window)
entry.pack()
Button(window, text="Greet", command=show_name).pack()
window.mainloop()
Session 3: GUI with Functions (Part 2)
Objective: Make the GUI interactive using functions and input data
Topics Covered:
- Getting input with Entry.get()
- Changing Label text using .config()
- Linking buttons with functions using command=
Examples:
1. GUI with name and age:
from tkinter import *
def display_info():
name = name_entry.get()
age = age_entry.get()
result_label.config(text=f"Name: {name}, Age: {age}")
window = Tk()
name_entry = Entry(window)
age_entry = Entry(window)
name_entry.pack()
age_entry.pack()
Button(window, text="Submit", command=display_info).pack()
result_label = Label(window)
result_label.pack()
window.mainloop()
2. GUI calculator:
from tkinter import *
def add_numbers():
a = int(entry1.get())
b = int(entry2.get())
result_label.config(text=str(a + b))
window = Tk()
entry1 = Entry(window)
entry2 = Entry(window)
entry1.pack()
entry2.pack()
Button(window, text="Add", command=add_numbers).pack()
result_label = Label(window)
result_label.pack()
window.mainloop()
Practice:
- Design your own form
- Take name, age, favorite color and show them in a label
Session 4: Introduction to Databases with TinyDB (Part 1)
Objective: Learn how to save and load data using a simple database
What is a Database?
A database stores data in a structured way so it can be saved, searched, and reused.
Install TinyDB:
pip install tinydb
Basic TinyDB Usage:
from tinydb import TinyDB, Query
db = TinyDB('students.json')
db.insert({'name': 'Ali', 'score': 90})
print(db.all())
Examples:
1. Add student info:
from tinydb import TinyDB
db = TinyDB('data.json')
db.insert({'name': 'Sara', 'grade': 'A'})
2. View all students:
print(db.all())
Practice:
- Create a program that asks for name and score and saves them
- Print the whole database to verify
Session 5: Database + GUI Integration (Part 2)
Objective: Build a complete app that combines GUI and database
Topics Covered:
- Search data using Query()
- Connect GUI input fields to TinyDB
- Display stored data inside the GUI
- Clear input fields after saving
Example:
from tkinter import *
from tinydb import TinyDB
db = TinyDB('students.json')
def save_data():
name = name_entry.get()
grade = grade_entry.get()
db.insert({'name': name, 'grade': grade})
result_label.config(text="Saved!")
name_entry.delete(0, END)
grade_entry.delete(0, END)
def show_data():
records = db.all()
output = ""
for record in records:
output += f"{record['name']} - {record['grade']}\n"
result_label.config(text=output)
window = Tk()
name_entry = Entry(window)
grade_entry = Entry(window)
name_entry.pack()
grade_entry.pack()
Button(window, text="Save", command=save_data).pack()
Button(window, text="Show All", command=show_data).pack()
result_label = Label(window)
result_label.pack()
window.mainloop()
Final Project:
- Build a student info system:
- Input: name, age, grade
- Save to TinyDB
- View all saved students in the GUI