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
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:
- They use variables, loops and if blocks without help, and maybe make their own blocks.
- They can type reasonably well, and read instructions on their own.
- They want to make things Scratch finds awkward, like text adventures, quizzes or programs that work with numbers.
Scratch doesn't have to stop. Many children keep using it for animations and games while they learn Python.
What changes when you switch
- You type instead of dragging. Spelling, capital letters and brackets all matter.
Printisn'tprint. - Text needs quote marks.
"Hello"is text.Hellowithout quotes is a name Python goes looking for. - Indenting replaces the C-shaped blocks. The lines inside a loop or an if are pushed in by 4 spaces, after a line ending in a colon.
- Lists start at 0. Scratch's item 1 is Python's
bag[0]. This catches out almost everyone. - No stage or sprites at first. Python programs start with text on the screen. Drawing comes later, with turtle or a game library.
- Errors are messages, not just "it doesn't work". Python says what went wrong and on which line. Learning to read them is a big step.
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 block | Python | Good to know |
|---|---|---|
| when green flag clicked | | Python starts at line 1 and works down. |
| say Hello! | | Text goes in quote marks. |
| say Hello! for 2 seconds | | Needs import time at the top. |
| ask What's your name? and wait | | You choose the variable's name. |
| set score to 0 | | One = means "put this value in". |
| change score by 1 | | Same as score = score + 1. |
| wait 1 seconds | | Needs import time. |
| repeat 10 | | Lines inside are indented 4 spaces. |
| forever | | Use break to get out. |
| if <> then | | Ends with a colon. The inside is indented. |
| if <> then / else | | else lines up with if. |
| repeat until <> | | while repeats while True; repeat until stops when True. |
| pick random 1 to 10 | | Needs import random. Includes both ends. |
| join Hello answer | | An f-string slots values into text. |
| <a = b> | | Two equals signs to compare. |
| <<> and <>> | | Also or and not, in lower case. |
| 7 mod 2 | | The remainder: 1. |
| length of apple | | Works on text and lists. |
| add thing to bag | | The list comes first, then a dot. |
| item 1 of bag | | Python counts from 0, not 1! |
| <bag contains thing?> | | Gives True or False. |
| define greet name | | Your own block is a function. |
| broadcast 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.
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:
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.
| Week | Idea | From Scratch | Make this |
|---|---|---|---|
| 1 | print() and text | say | A greeting card made of text |
| 2 | Variables and maths | set, change | A score counter, a pocket money adder |
| 3 | input() | ask and wait | A program that asks your name and age |
| 4 | if and else | if then else | A quiz with three questions |
| 5 | Loops | repeat, forever | A countdown, then guess my number |
| 6 | Functions | My Blocks | A 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
- Let them type it. Copying and pasting skips the part where they learn. Typing short programs builds the habit of spelling things exactly.
- Read errors together. Ask "which line does it say?" and "what does it think is wrong?" before fixing anything.
- Start with text games. Quizzes, number guessing and choose-your-path stories feel like real programs, with no graphics needed.
- Predict, then run. Ask what the code will print before pressing Run. It's the quickest way to find out what they really understand.
- Keep sessions short. Twenty focused minutes beats an hour of frustration.
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 courseMore teach
More free ways to learn
Fun Python code
Fun, cool Python code for beginners: I love you in Python, heart code, happy birthday programs, a rocket countdown and more. Run each one in your browser.
Practice
Free Python practice online: exercises with answers you check in your browser, plus 79 coding games, one for every beginner topic.
Guides
Short guides on learning Python with games, how long it takes, and where to start.