Scratch to Python: moving from blocks to real code

If you can build a game in Scratch, you already know most of the ideas in Python. This page shows the Python for every common Scratch block, the same game written both ways, and a simple plan for making the switch, at home or in class.

On this page
  1. When is a child ready to move from Scratch to Python?
  2. What changes when you switch
  3. Scratch blocks in Python
  4. The same game in Scratch and Python
  5. Your own blocks are functions
  6. A 6-week plan for moving to Python
  7. Tips for parents and teachers

When is a child ready to move from Scratch to Python?

There's no exact age, but most children are ready somewhere between 9 and 12. Look for these signs rather than a birthday:

Scratch doesn't have to stop. Many children keep using it for animations and games while they learn Python.

What changes when you switch

Scratch blocks in Python

Find a block you know on the left, and its Python on the right. import time and import random go once, at the top of the program.

Scratch blockPythonGood to know
when green flag clicked
# nothing to add
Python starts at line 1 and works down.
say Hello!
print("Hello!")
Text goes in quote marks.
say Hello! for 2 seconds
print("Hello!")
time.sleep(2)
Needs import time at the top.
ask What's your name? and wait
answer = input("What's your name? ")
You choose the variable's name.
set score to 0
score = 0
One = means "put this value in".
change score by 1
score += 1
Same as score = score + 1.
wait 1 seconds
time.sleep(1)
Needs import time.
repeat 10
for i in range(10):
Lines inside are indented 4 spaces.
forever
while True:
Use break to get out.
if <> then
if score > 10:
Ends with a colon. The inside is indented.
if <> then / else
if score > 10:
    ...
else:
    ...
else lines up with if.
repeat until <>
while not done:
while repeats while True; repeat until stops when True.
pick random 1 to 10
random.randint(1, 10)
Needs import random. Includes both ends.
join Hello answer
"Hello " + answer
f"Hello {answer}"
An f-string slots values into text.
<a = b>
a == b
Two equals signs to compare.
<<> and <>>
a > 1 and b > 1
Also or and not, in lower case.
7 mod 2
7 % 2
The remainder: 1.
length of apple
len("apple")
Works on text and lists.
add thing to bag
bag.append("thing")
The list comes first, then a dot.
item 1 of bag
bag[0]
Python counts from 0, not 1!
<bag contains thing?>
"thing" in bag
Gives True or False.
define greet name
def greet(name):
Your own block is a function.
broadcast game over
game_over()
Usually just a function call.

The same game in Scratch and Python

Here's a guess-my-number game. On the left are the Scratch blocks, and underneath is the Python, line for line. Press Run and play it.

main.py
Output
Guess my number (1-10): 3
Nope! Try again.
Guess again: 8
Nope! Try again.
Guess again: 6
You got it!

Shown with the answers '3', '8', '6'. Press Run to type your own.

Two things are new. int() turns the typed answer into a number, because input() always gives text and Scratch quietly does this for you. And Scratch's repeat until becomes while with the opposite test: repeat until it's equal is the same as repeat while it's not equal, !=.

Your own blocks are functions

In Scratch you make a block under My Blocks and give it inputs. In Python you write def, a name, and the inputs in brackets. Calling it is like using the block:

main.py
Output
Hi, Ava!
Welcome to Python.
Hi, Leo!
Welcome to Python.

A 6-week plan for moving to Python

This plan works at home or in a class, with about an hour a week. Each week takes an idea children already know from Scratch and shows it in Python.

WeekIdeaFrom ScratchMake this
1print() and textsayA greeting card made of text
2Variables and mathsset, changeA score counter, a pocket money adder
3input()ask and waitA program that asks your name and age
4if and elseif then elseA quiz with three questions
5Loopsrepeat, foreverA countdown, then guess my number
6FunctionsMy BlocksA mini project using everything

The free printable Python worksheets follow the same order, so you can hand one out each week.

Tips for parents and teachers

Scratch is a project of the Scratch Foundation, in collaboration with the Lifelong Kindergarten Group at the MIT Media Lab. This site isn't connected to Scratch.

Play the game version

Each of these games teaches one idea from this page, with a short narrated video and six levels to clear.

Questions people ask

Is Python harder than Scratch?

A little, at first, because you type everything and small slips cause errors. The ideas are the same: variables, loops, ifs and your own blocks. Children who are confident in Scratch usually pick up Python quickly.

What age should a child move from Scratch to Python?

Usually between 9 and 12. It depends less on age than on whether they use variables, loops and ifs on their own, and can type comfortably.

Can a Scratch project be converted to Python automatically?

Not usefully. There are a few experimental converters, but rewriting a small project by hand, block by block, teaches far more and gives tidier code.

Should my child learn Scratch or Python first?

For under-9s, Scratch is the gentler start. For older children and teenagers who can type, starting straight with Python works well, especially with games that show the result of their code.

What is repeat until in Python?

Use a while loop with the opposite test. repeat until score = 10 becomes while score != 10:

Learn Python with games

Every idea on this page has its own game in the course, with a short video and six levels. The three games in Module 1 are free: no account, nothing to install.

Every idea on this page has its own game in the course. Pick up where you left off.

Play the first game freeContinue the course

More teach