📄 Working Code

Python Code Library

The exact code from every episode. Click Copy, paste it into the editor or your own Python, and press Run.

Part 1 — Beginner

From your first print() to a full Tic-Tac-Toe game.

4

Your First print

↗ Open text
print("My name is Coderboy")
5

Variables Boxes for Values

↗ Open text
name = "Maya"
print(name)
6

Numbers and Strings

↗ Open text
city = "Dubai"
print(city + 1)
7

Math in Python Part 1

↗ Open text
apples = 5
oranges = 3
print(apples + oranges)
8

Math in Python Part 2

↗ Open text
print((2 + 3) * 4)
9

input Part 1

↗ Open text
name = input("What is your name: ")
print("Hello", name)
10

input Part 2

↗ Open text
name = input("What is your name: ")
age = int(input("How old are you: "))
print("Hi", name, "next year you will be", age + 1)
11

if and else Part 1

↗ Open text
age = 5
if age >= 9:
    print("Old enough to code!")
12

if and else Part 2

↗ Open text
print(5 == 5)
print(5 != 5)
print(9 > 4)
print(8 < 2)
print(9 >= 9)
print(7 <= 4)
13

Loops Part 1

↗ Open text
for i in range(5):
    print(i)
14

Loops Part 2

↗ Open text
for i in range(3):
    print("for", i)

n = 3
while n > 0:
    print("while", n)
    n = n - 1
15

Strings Joining and Repeating

↗ Open text
print(len("Coderboy"))
16

Lists A Box of Many Things

↗ Open text
fruits = ["apple", "banana", "cherry", "date"]
for fruit in fruits:
    print(fruit)
17

Random Numbers

↗ Open text
import random
colors = ["red", "blue", "green"]
print(random.choice(colors))
18

Build a Greeting Story project

↗ Open text
name = input("What is your name? ")
age = int(input("How old are you? "))
color = input("What is your favorite color? ")

if age < 7:
    print("You are still very young")
elif age < 10:
    print("You are a young coder")
else:
    print("You are growing fast")

print("Hello " + name + "!")
print("Your favorite color is " + color + "!")
19

Number Guessing Game project

↗ Open text
import random
secret = random.randint(1, 10)
guess = int(input("Pick a number from 1 to 10: "))
if guess == secret:
    print("Correct! The number was", secret)
elif guess < secret:
    print("Too low. The number was", secret)
else:
    print("Too high. The number was", secret)
20

Rock Paper Scissors project

↗ Open text
import random
choices = ["rock", "paper", "scissors"]
computer = random.choice(choices)
user = input("rock, paper, or scissors? ")
if user == computer:
    print("Tie! Both chose", computer)
elif (user == "rock" and computer == "scissors") or \
     (user == "paper" and computer == "rock") or \
     (user == "scissors" and computer == "paper"):
    print("You win! Computer chose", computer)
else:
    print("Computer wins! It chose", computer)
21

Mad Libs Story Generator project

↗ Open text
name = input("What is your name? ")
animal = input("A funny animal? ")
place = input("A place? ")
verb = input("A verb ending in -ing? ")

print("One day, " + name + " went to " + place + " and started " + verb + " with a " + animal + ".")
22

Simple Calculator project

↗ Open text
a = int(input("First number? "))
b = int(input("Second number? "))
op = input("Operation (plus, minus, times, divide)? ")

if op == "plus":
    print(a + b)
elif op == "minus":
    print(a - b)
elif op == "times":
    print(a * b)
elif op == "divide":
    print(a / b)
else:
    print("Unknown operation")
23

Multiplication Tables project

↗ Open text
n = int(input("Which table? "))
for i in range(1, 11):
    print(n, "x", i, "=", n * i)
24

Quiz Game project

↗ Open text
questions = [
    ["How many legs does a dog have? ", "4"],
    ["What sound does a cat make? ", "meow"],
    ["What is a baby sheep called? ", "lamb"],
]

score = 0
for item in questions:
    answer = input(item[0])
    if answer == item[1]:
        print("Correct!")
        score = score + 1
    else:
        print("Wrong. The answer was", item[1])

print("Your score is", score, "out of 3")
25

Password Strength Checker project

↗ Open text
password = input("Type a password: ")

if len(password) < 5:
    print("Very weak")
elif len(password) < 8:
    print("Okay")
else:
    print("Strong password")
26

Tic Tac Toe project

↗ Open text
board = [" "] * 9

def mark(i):
    return board[i] if board[i] != " " else str(i + 1)


def show():
    print("   TIC  TAC  TOE")
    print()
    for row in range(3):
        a, b, c = mark(row*3), mark(row*3+1), mark(row*3+2)
        print("   " + a + " | " + b + " | " + c)
        if row < 2:
            print("  ---+---+---")
    print()

def clear():
    print("\n" * 30)

WIN_LINES = [
    [0,1,2], [3,4,5], [6,7,8],
    [0,3,6], [1,4,7], [2,5,8],
    [0,4,8], [2,4,6],
]

player = "X"
for turn in range(9):
    clear()
    show()
    cell = int(input("Player " + player + ", choose 1-9: ")) - 1
    board[cell] = player
    won = False
    for line in WIN_LINES:
        if board[line[0]] == board[line[1]] == board[line[2]] != " ":
            clear()
            show()
            print("   Player " + player + " wins!")
            won = True
            break
    if won:
        break
    player = "O" if player == "X" else "X"
else:
    clear()
    show()
    print("   It's a tie!")