Python Primer
Python for Kids: A Fun Beginner’s Guide (Age 10β12)
What is Python?
Python is a programming language that lets you talk to a computer and tell it what to do. It’s like giving instructions to your robot friend. Python is easy to read and fun to learn!
How to Start?
- Go to https://replit.com
- Or install Python from https://python.org
- Open an editor and start coding!
Your First Program
print("Hello, world!")
1. Python as a Calculator
print(3 + 2) print(5 - 1) print(4 * 2) print(8 / 2)
2. Variables
name = "Alex" age = 10 print(name) print(age)
3. Strings (Words)
greeting = "Hi there!"
print(greeting)
print("My name is " + name)
4. Numbers
apples = 5
oranges = 3
total = apples + oranges
print("Total fruits:", total)
5. If Statements (Making Decisions)
age = 12
if age >= 10:
print("You're allowed to play!")
else:
print("Sorry, you're too young.")
6. Loops (Repeat Things)
For Loop
for i in range(5):
print("This is number", i)
While Loop
count = 0
while count < 3:
print("Counting...", count)
count += 1
7. Functions (Little Machines)
def say_hello():
print("Hello there!")
say_hello()
Function with a Parameter
def greet(name):
print("Hello", name)
greet("Sam")
greet("Ella")
8. Lists (Groups of Items)
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
print(len(fruits))
for fruit in fruits:
print("I like", fruit)
9. Random Fun!
import random
number = random.randint(1, 10)
print("Your lucky number is:", number)
10. Build a Simple Game: Guess the Number
import random
secret = random.randint(1, 5)
guess = int(input("Guess a number between 1 and 5: "))
if guess == secret:
print("π You got it right!")
else:
print("Oops! The number was", secret)
11. Bonus Fun: Turtle Graphics
import turtle
t = turtle.Turtle()
for i in range(4):
t.forward(100)
t.right(90)
turtle.done()
12. Doing More Math with Python
# Math with operators print(10 % 3) # Modulus (Remainder) print(2 ** 4) # Exponent (2^4 = 16) print(15 // 4) # Floor Division
import math print(math.sqrt(64)) # Square root print(math.factorial(5))# Factorial print(math.sin(0)) # Trigonometric functions
13. Meet NumPy: Fast Math with Arrays
import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(a + b) print(np.sqrt(a))
14. Meet pandas: Play with Data Tables
import pandas as pd
data = {
"Name": ["Ava", "Ben", "Cara"],
"Marks": [88, 92, 85]
}
df = pd.DataFrame(data)
print(df)
print(df["Marks"].mean())
Great Python Books for Kids
- Python for Kids by Jason R. Briggs
- Coding Projects in Python by DK
- Teach Your Kids to Code by Dr. Bryson Payne
- Adventures in Python by Craig Richardson
- Mission Python by Sean McManus
Python for Kids: Math, NumPy & pandas Guide
Basic Python Math
print(10 + 5) # Addition print(10 - 2) # Subtraction print(4 * 3) # Multiplication print(9 / 3) # Division print(10 % 3) # Remainder (modulus) print(2 ** 3) # Exponents (2^3 = 8) print(15 // 4) # Floor division
import math print(math.sqrt(25)) # Square root print(math.pow(2, 5)) # 2 to the power of 5 print(math.pi) # Value of Pi print(math.sin(0)) # Sine function print(math.factorial(5)) # Factorial of 5
NumPy: Vectors and Matrices
NumPy is a library used for fast math on big lists of numbers called arrays.
Arrays and Vectors
import numpy as np
v = np.array([1, 2, 3])
print("Vector:", v)
print("Add 5:", v + 5)
print("Double:", v * 2)
print("Squared:", v ** 2)
Matrices (2D Arrays)
matrix = np.array([[1, 2], [3, 4]])
print(matrix)
print("Transpose:", matrix.T)
print("Multiplied:", matrix * 2)
Matrix Multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print("Dot Product:", np.dot(A, B))
Other NumPy Functions
arr = np.array([4, 7, 2, 9, 5])
print("Max:", np.max(arr))
print("Min:", np.min(arr))
print("Mean:", np.mean(arr))
print("Sum:", np.sum(arr))
print("Sorted:", np.sort(arr))
pandas: Work with CSV Data
pandas is like Excel in Python! It helps organize and analyze data tables.
Read a CSV
import pandas as pd
df = pd.read_csv("students.csv")
print(df.head())
Explore and Describe
print(df.columns)
print(df.shape)
print(df.describe())
print("Average Score:", df["Score"].mean())
Filter and Select
high_scores = df[df["Score"] > 80] print(high_scores) print(df[["Name", "Score"]])
Add and Edit Columns
df["Passed"] = df["Score"] > 50 df["Score"] = df["Score"] + 5
Save Updated CSV
df.to_csv("updated_students.csv", index=False)
Python with NumPy: Vectors and Matrices (For Kids & Beginners)
NumPy is a powerful library for doing math with vectors and matrices. Letβs explore how it works step by step!
Getting Started
import numpy as np
Creating Vectors and Matrices
# 1D Vector
v = np.array([1, 2, 3])
print("Vector v:", v)
# 2D Matrix
m = np.array([[1, 2], [3, 4]])
print("Matrix m:")
print(m)
Vector Operations
v = np.array([10, 20, 30]) print(v + 5) # Add 5 to each element print(v - 2) # Subtract 2 print(v * 2) # Multiply print(v / 10) # Divide print(v ** 2) # Power print(np.sqrt(v)) # Square Root
Matrix Operations
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Element-wise operations
print(A + B)
print(A - B)
print(A * B)
print(A / B)
# Transpose
print("Transpose of A:")
print(A.T)
Matrix Multiplication (Dot Product)
A = np.array([[1, 2], [3, 4]])
B = np.array([[2, 0], [1, 2]])
# Dot Product
dot_result = np.dot(A, B)
print("Dot product of A and B:")
print(dot_result)
Norms and Magnitudes
v = np.array([3, 4])
# Magnitude (Euclidean norm)
print("||v|| =", np.linalg.norm(v))
Identity, Inverse, and Determinant
# Identity matrix
I = np.eye(3)
print("Identity Matrix:")
print(I)
# Determinant
D = np.array([[4, 7], [2, 6]])
print("Determinant:", np.linalg.det(D))
# Inverse
inv_D = np.linalg.inv(D)
print("Inverse of D:")
print(inv_D)
Solving Systems of Equations
# Solve Ax = b
A = np.array([[2, 1], [1, 3]])
b = np.array([8, 13])
x = np.linalg.solve(A, b)
print("Solution x:", x)
Extra Functions and Tips
arr = np.array([10, 20, 30, 40, 50]) print(np.mean(arr)) # Average print(np.median(arr)) # Median print(np.std(arr)) # Standard Deviation print(np.max(arr)) # Max print(np.min(arr)) # Min print(np.sort(arr)) # Sort
Go to Core Learning
