Python Kids Extended Course
Python Kids Extended Course
Total Sessions: 5
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
Topics Covered:
- Calling a function
Examples:
1. Simple function:
def say_hello():
print("Hello!")
say_hello()
def greet(name):
greet("Ali")
return a + b
result = add(3, 4)
print(result)
Practice Activities:
What is a GUI?
A GUI (Graphical User Interface) allows users to interact with programs using windows, buttons, and
text fields.
Examples:
1. Basic window:
window = Tk()
window.mainloop()
def say_hello():
window = Tk()
Label(window, text="Welcome!").pack()
3. Entry Example:
def show_name():
name = entry.get()
window = Tk()
entry = Entry(window)
entry.pack()
window.mainloop()
Objective: Make the GUI interactive using functions and input data
Topics Covered:
Examples:
name = name_entry.get()
age = age_entry.get()
window = Tk()
name_entry = Entry(window)
age_entry = Entry(window)
name_entry.pack()
age_entry.pack()
result_label = Label(window)
result_label.pack()
window.mainloop()
2. GUI calculator:
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()
result_label = Label(window)
result_label.pack()
window.mainloop()
Practice:
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:
db = TinyDB('students.json')
print(db.all())
Examples:
1. Add student info:
db = TinyDB('data.json')
print(db.all())
Practice:
- Create a program that asks for name and score and saves them
Topics Covered:
Example:
db = TinyDB('students.json')
def save_data():
name = name_entry.get()
grade = grade_entry.get()
result_label.config(text="Saved!")
name_entry.delete(0, END)
grade_entry.delete(0, END)
def show_data():
records = db.all()
output = ""
result_label.config(text=output)
window = Tk()
name_entry = Entry(window)
grade_entry = Entry(window)
name_entry.pack()
grade_entry.pack()
result_label = Label(window)
result_label.pack()
window.mainloop()
Final Project:
- Save to TinyDB