Python coding for kids โ fun, easy, and step by step!
Let's make games and apps together!
Meet the special language that talks to computers!
๐ After this chapter โ you can write your very first Python program and watch it run!
Python is a programming language โ a way to give instructions to a computer!
Just like we use English to talk to each other, we use Python to tell computers what to do. And Python is one of the easiest languages to learn!
| Category | Examples |
|---|---|
| ๐ฎ Games | Minecraft servers, puzzle games |
| ๐ค Artificial Intelligence | ChatGPT, image recognition |
| ๐ Websites | Instagram, YouTube, Netflix |
| ๐ Data Analysis | Charts, number crunching |
| ๐ค Robots | Drones, self-driving cars |
| ๐ฌ Movies | Visual effects (VFX) |
Try Python right now โ no installation needed!
Q1. What is Python?
Q2. Where does the name "Python" come from?
Use print() to show text and numbers!
๐ After this chapter โ you can make the screen display any message you want!
print() is a command that displays text or numbers on the screen.
It's the very first thing every Python programmer learns! ๐
print("Hello, World!")
You can print text, numbers, and more!
print("Hello!") # print text
print(123) # print a number
print(3.14) # print a decimal
print(True) # print True or False
print("My name is", "Alex", "!")
print("I am", 10, "years old")
Use sep to change the separator and end to change the line ending!
# sep: change what goes between values (default: space)
print("apple", "banana", "grape", sep=" / ")
# end: change what goes at the end (default: newline)
print("Hello", end="! ")
print("I am Python!")
Put f before a string to insert variables inside {}!
name = "Alex"
age = 11
print(f"Hi! My name is {name} and I am {age} years old!")
Q1. What does print("Python") display?
Q2. What does the # symbol mean in Python?
Store and retrieve information with named boxes!
๐ After this chapter โ you can store names, scores, and ages and use them in your programs!
A variable is like a labeled box that stores information!
Give the box a name, put something inside, and you can take it out and use it later.
Use = to create a variable. Left side is the name, right side is the value!
name = "Alex" # store a name
age = 10 # store age
height = 140.5 # store height
is_student = True # store True or False
print(name)
print(age)
print(height)
print(is_student)
Variables can store different types of data!
| Type | Name | Example | Description |
|---|---|---|---|
| str | String | "Hello", "Alex" | Text / words |
| int | Integer | 10, -5, 100 | Whole numbers |
| float | Float | 3.14, 1.5 | Decimal numbers |
| bool | Boolean | True, False | True or False only |
# Check the type with type()
print(type("Hello")) # <class 'str'>
print(type(10)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type(True)) # <class 'bool'>
| โ OK | โ Not OK |
|---|---|
| name, age, my_score | 1name (can't start with a number) |
| score1, total_count | my-score (no hyphens!) |
| isStudent, bestScore | class, if (reserved keywords!) |
score = 0
print("Start:", score) # 0
score = 100
print("After:", score) # 100
score = score + 10 # add 10 to current value
print("Final:", score) # 110
Q1. Which of these is a valid variable name?
Q2. In age = 10, what data type is 10?
Let's build a calculator with Python!
๐ After this chapter โ you can build a calculator that does math for you automatically!
| Symbol | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 10 - 4 | 6 |
| * | Multiplication | 3 * 4 | 12 |
| / | Division | 10 / 4 | 2.5 |
| // | Floor Division | 10 // 3 | 3 |
| % | Remainder (Modulo) | 10 % 3 | 1 |
| ** | Power (Exponent) | 2 ** 10 | 1024 |
print(5 + 3) # 8
print(10 - 4) # 6
print(3 * 4) # 12
print(10 / 4) # 2.5
print(10 // 3) # 3 (drops the decimal)
print(10 % 3) # 1 (remainder of 10 รท 3)
print(2 ** 10) # 1024 (2 to the power of 10)
price = 2 # price of one candy (dollars)
count = 5 # number of candies
total = price * count
print("Total:", total, "dollars")
print(f"Buying {count} candies costs ${total}!")
Use input() to ask the user to type something!
name = input("What is your name? ")
print("Hello,", name, "!")
# Getting a number
num1 = int(input("First number: "))
num2 = int(input("Second number: "))
print("Sum:", num1 + num2)
str(10) # number โ string: "10"
int("123") # string โ integer: 123
float("3.14") # string โ decimal: 3.14
int(3.7) # decimal โ integer: 3 (decimal dropped!)
Q1. What is the result of 10 % 3?
Q2. What is the result of 2 ** 3?
Make decisions based on different situations!
๐ After this chapter โ you can write smart programs that react differently to different situations!
These compare two values and give back True or False!
| Symbol | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| > | Greater than | 7 > 5 | True |
| < | Less than | 3 < 5 | True |
| >= | Greater or equal | 5 >= 5 | True |
| <= | Less or equal | 3 <= 5 | True |
Code inside if only runs when the condition is True!
temperature = 95 # degrees Fahrenheit
if temperature > 85:
print("It's really hot outside! โ๏ธ")
print("Stay hydrated!")
money = 3 # dollars
if money >= 5:
print("You can buy the toy! ๐งธ")
else:
print("Not enough money. You need $2 more. ๐ข")
Check multiple conditions in order!
score = 85
if score >= 90:
print("Grade A! Amazing! โญโญโญ")
elif score >= 80:
print("Grade B! Great job! โญโญ")
elif score >= 70:
print("Grade C! Keep it up! โญ")
else:
print("Keep studying! You can do it! ๐ช")
age = 12
height = 55 # inches
# and: BOTH must be True
if age >= 10 and height >= 54:
print("You can ride! ๐ข")
# or: AT LEAST ONE must be True
if age >= 15 or height >= 54:
print("One condition is met!")
# not: flips True to False (and False to True)
is_raining = False
if not is_raining:
print("No umbrella needed! โ๏ธ")
Q1. What is the result of 5 == 5?
Q2. If score = 95, what does this print?if score >= 90: print("A")else: print("B")
Repeat things automatically โ no copy-pasting!
๐ After this chapter โ you can do 100 repetitive steps with just 3 lines of code!
Want to print "Hello!" 100 times? Without loops you'd write print("Hello!") 100 times!
for i in range(5):
print(i, "- Hello!")
| Code | Numbers |
|---|---|
| range(5) | 0, 1, 2, 3, 4 |
| range(1, 6) | 1, 2, 3, 4, 5 |
| range(0, 10, 2) | 0, 2, 4, 6, 8 (step by 2) |
| range(5, 0, -1) | 5, 4, 3, 2, 1 (count down!) |
# Times tables: 3's
for i in range(1, 10):
print(f"3 ร {i} = {3 * i}")
Keeps repeating as long as the condition is True!
count = 1
while count <= 5:
print(count, "- Go!")
count += 1 # same as: count = count + 1
# break: exit the loop immediately
for i in range(10):
if i == 5:
break # stop at 5!
print(i, end=" ")
# Output: 0 1 2 3 4
print() # new line
# continue: skip this round, move to next
for i in range(10):
if i % 2 == 0:
continue # skip even numbers
print(i, end=" ")
# Output: 1 3 5 7 9
Q1. What numbers does range(1, 5) include?
Q2. What does for i in range(3): print(i) output?
Build your own custom commands!
๐ After this chapter โ you can create your own custom commands to reuse anytime!
A function is a named block of code that does a specific job!
The print() we've been using? That's a function Python built for us. Now let's make our own!
# Define the function
def greet():
print("Hello! ๐")
print("Welcome to the Python world!")
# Call (use) the function
greet()
greet() # call it again!
Pass values into your function!
def greet(name):
print(f"Hello, {name}! Nice to meet you! ๐")
greet("Alex")
greet("Emma")
greet("Liam")
def add(a, b):
result = a + b
return result # send the answer back!
answer = add(5, 3) # store the returned value
print(answer) # 8
print(add(10, 20)) # 30
print(add(100, 200)) # 300
def introduce(name, age, hobby):
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Hobby: {hobby}")
print("---")
introduce("Alex", 11, "Soccer โฝ")
introduce("Emma", 10, "Drawing ๐จ")
numbers = [5, 2, 8, 1, 9, 3]
print(len(numbers)) # 6 (count)
print(max(numbers)) # 9 (largest)
print(min(numbers)) # 1 (smallest)
print(sum(numbers)) # 28 (total)
print(abs(-10)) # 10 (absolute value)
print(round(3.7)) # 4 (rounded)
Q1. What keyword do you use to create a function?
Q2. What does add(3, 4) return?def add(a, b): return a + b
Store multiple values in one place!
๐ After this chapter โ you can manage lists of names, scores, or anything โ all in one place!
A list is like a big box with numbered slots โ it holds many values in order!
fruits = ["apple", "banana", "strawberry", "grape"]
scores = [90, 85, 78, 92, 88]
mixed = ["Alex", 11, True, 3.14] # different types OK!
print(fruits)
print(scores)
Each item has a number called an index. Counting starts at 0!
fruits = ["apple", "banana", "strawberry", "grape"]
# index: 0 1 2 3
print(fruits[0]) # apple (first)
print(fruits[1]) # banana (second)
print(fruits[-1]) # grape (last)
print(fruits[-2]) # strawberry (second from last)
fruits = ["apple", "banana"]
fruits.append("strawberry") # add to end
print(fruits) # ['apple', 'banana', 'strawberry']
fruits.insert(1, "grape") # insert at index 1
print(fruits) # ['apple', 'grape', 'banana', 'strawberry']
fruits.remove("banana") # remove by value
print(fruits) # ['apple', 'grape', 'strawberry']
print(len(fruits)) # 3 (count items)
fruits.sort() # sort alphabetically
print(fruits) # ['apple', 'grape', 'strawberry']
fruits = ["apple", "banana", "strawberry", "grape"]
for fruit in fruits:
print(fruit, "is yummy! ๐")
# With index numbers
for i, fruit in enumerate(fruits):
print(f"{i+1}. {fruit}")
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
print(combined) # [1, 2, 3, 4, 5, 6]
zeros = [0] * 5
print(zeros) # [0, 0, 0, 0, 0]
Q1. In fruits = ["apple", "banana", "cherry"], what is fruits[1]?
Q2. Which method adds a new item to the END of a list?
Use everything you've learned to build cool programs!
๐ After this chapter โ you'll have real working programs: star patterns, a guessing game, rock-paper-scissors, and more!
Draw a triangle using loops!
size = 5
for i in range(1, size + 1):
print("โญ" * i)
range(size, 0, -1))number = int(input("Which times table? "))
print(f"\n{'='*15}")
print(f" {number} Times Table")
print(f"{'='*15}")
for i in range(1, 10):
print(f" {number} ร {i:1} = {number * i:2}")
Use the random module to create a random number!
import random # load the random module
secret = random.randint(1, 10) # random number 1โ10
attempts = 0
print("๐ฎ Number Guessing Game!")
print("I'm thinking of a number between 1 and 10!\n")
while True:
guess = int(input("Your guess: "))
attempts += 1
if guess < secret:
print("Too low! โฌ๏ธ")
elif guess > secret:
print("Too high! โฌ๏ธ")
else:
print(f"\n๐ Correct! You got it in {attempts} tries!")
break
subjects = ["Math", "English", "Science", "History", "Art"]
scores = []
print("=== ๐ Grade Calculator ===\n")
for subject in subjects:
score = int(input(f"{subject} score: "))
scores.append(score)
total = sum(scores)
average = total / len(scores)
print(f"\n{'='*20}")
print(f"Total: {total} pts")
print(f"Average: {average:.1f} pts")
print(f"{'='*20}")
if average >= 90:
print("๐ Outstanding! Amazing work!")
elif average >= 80:
print("๐ Great job! Keep it up!")
elif average >= 70:
print("๐ Good effort! Keep studying!")
else:
print("๐ช Don't give up! You can do it!")
def calculator(a, op, b):
if op == "+":
return a + b
elif op == "-":
return a - b
elif op == "*":
return a * b
elif op == "/":
if b == 0:
return "Can't divide by zero!"
return a / b
else:
return "Unknown operator!"
# Test it!
print(calculator(10, "+", 5)) # 15
print(calculator(10, "-", 3)) # 7
print(calculator(4, "*", 6)) # 24
print(calculator(15, "/", 3)) # 5.0
print(calculator(10, "/", 0)) # Can't divide by zero!
Play against the computer! First to win 3 rounds wins the game!
import random
choices = ["Rock", "Paper", "Scissors"]
win_count = 0
lose_count = 0
draw_count = 0
print("๐ฎ Rock Paper Scissors!")
print("First to win 3 rounds wins!\n")
while True:
print("1:Rock 2:Paper 3:Scissors 0:Quit")
user_input = int(input("Your choice: "))
if user_input == 0:
print("Game over!")
break
if user_input not in [1, 2, 3]:
print("Please choose 1, 2, or 3!\n")
continue
user = choices[user_input - 1]
computer = random.choice(choices)
print(f"\n๐ You: {user}")
print(f"๐ค Computer: {computer}")
if user == computer:
print("๐ It's a draw!")
draw_count += 1
elif (user == "Scissors" and computer == "Paper") or \
(user == "Rock" and computer == "Scissors") or \
(user == "Paper" and computer == "Rock"):
print("๐ You win!")
win_count += 1
else:
print("๐ข You lose...")
lose_count += 1
print(f"Score: {win_count}W {lose_count}L {draw_count}D\n")
if win_count == 3:
print("๐ 3 wins! You're amazing!")
break
elif lose_count == 3:
print("๐ช 3 losses. Try again!")
break
print(f"\n=== Final Score ===")
print(f"{win_count}W {lose_count}L {draw_count}D")
Q1. What is the simplest way to print 5 stars on one line?
Q2. How do you load the random module in Python?