Wizard Toolbox Tool map Feedback
BETAStill being built. Things may break. Tell us

Python Blocks

Drop Python code here

A .py file, a Jupyter notebook, or a .zip of a whole project · click to choose

Python Blocks

Snap blocks together and the Python is written beside them, or paste Python and get it back as blocks. Then run it, right here.

How to use it

Press Start blank for a fresh program that says hello: one print block to run straight away. The first time, Getting started shows the four steps: drag a block from the left into the middle, snap blocks under each other (they run from the top down), press Run and see what it prints under Output, and read the Python on the right, which is your program written as code. The ? button beside Undo, or Help, shows it again. File > Examples holds finished programs to run and take apart: a guessing game, a turtle drawing, a quiz, a game to play with the arrow keys. Drop a .py file on the page, or click the box to choose one, and it opens as blocks; a .zip of a whole project opens with all its scripts, pictures and sounds. When this browser keeps a program from before, Continue with beside Start blank opens it again as you left it.

How it is laid out

The categories run down the left, each with its colour: Input & output, Logic, Loops, Math, Text, Variables, Lists, Dictionaries and Functions first, then a line and More: Objects, Classes & objects, Errors, Files, Python and Libraries, and the sections of the modules you add. Beside them the palette holds every block, one category after another: scroll through it, or click a category to jump there. Type in Search blocks to find one by its words, its Python or another name it is often called (say, forever, broadcast), and press Enter to add the first one found. Drag a block into the middle and snap it to others; drag it back onto the palette to delete it. One let go of outside the blocks comes back into view. Right-click a block (or hold it on a phone) to copy, delete, fold it to its one line of Python or add a note beside it; a click opens a folded block again. The + and − buttons in the corner zoom, and = puts the size back. On the right sit Run, Stop and the output, with the Python under it. Drag the line between any two parts to make one bigger, and double-click a line to put it back; the sizes are kept on this device. On a phone, pull the Output tab at the foot of the blocks up to see the output under them.

Blocks and Python side by side

The Python is written as you snap blocks together. Blocks, Both and Python above the blocks choose what shows. Press Edit to change the Python by hand, then Use as blocks to turn it back into blocks; meanwhile the blocks wait under a veil, and one Undo afterwards brings back the blocks you had. Anything with no block of its own becomes a Python block holding that line, so nothing is lost. Where two ways of building something write the same line, the block changes as you drop it, glows and says why: a not dropped into repeat while makes it repeat until, and a colour typed into a turtle's colour slot becomes the colour picker.

Running it

Run (F5) runs the program in the page, from the top of main.py. What it prints shows under Output, and input() asks you there. Turtle drawings and game windows show under Screen. Stop (Shift+F5) ends a program that runs too long. The output is ordinary text: select any of it and copy it, or press the copy button beside it to take all of it, error messages included.

Variables and lists

A variable keeps a value under a name, like score = 0. Press Make a variable in Variables and name it, or type a new name in the box on a set block. set gives it a value, change adds to it (or takes away, multiplies or divides: pick which on the block), and its orange pill goes wherever you want its value. To watch its value over the output while the program runs, right-click its pill and choose Watch, or click the eye beside its name in a name box. A list keeps many values in order, like ["eggs", "milk"]: press Make a list in Lists, then add, insert, replace and delete its items. A variable you only ever set to a list becomes a list, its blocks with it, and a list set to anything else becomes a variable again. Positions count from 0, as in Python, so the first item is at position 0. A function's inputs are pills on its define block, and a loop's variable is the pill on its loop: drag a copy out to use it inside. A variable made only inside one function is listed under that function's name. Type a new name in a variable's box on a block and press Enter to rename it everywhere, or pick Only this block for a new variable. Right-click a pill to rename or delete its variable: deleting one that blocks use says how many first, and Undo brings them back. Each script has its own variables: from main.py, player.py's x is x of player in Objects. Variables that hold game things (the screen, a clock, a picture, a Rect) also show at the top of Games, and those holding objects at the top of Objects.

Making games with pygame

The Games (pygame) blocks open a window, run a game loop, draw shapes, text and pictures, read the arrow keys, the mouse or a finger, and test what touches what. Click the game window to play with the keys; on a phone, arrow and Space buttons show under it. Add pictures and sounds (PNG, JPG, GIF, BMP, WAV, OGG, MP3) with File > Add a picture or sound, and pygame.image.load("player.png") finds them. They are listed under the Media tab, after the script tabs, where each can be renamed or deleted, and Use in code puts it in the script: set hero to picture hero.png for a picture, to draw with draw picture … on screen in the game loop, or play sound for a sound.

More than one script

A program starts as main.py, the first tab over the blocks, with a small start tag: Run always starts there. Press + beside the tabs to add another script, such as player.py, with blocks and Python of its own. import player or from player import Player in main.py uses it, as on a computer, and its functions and classes turn up as blocks under Libraries. Right-click a tab, hold it, or press its ⋯ to rename or delete it (or use File). An error in another script opens its tab and lights the block. Each tab keeps its own Undo. A .py dropped on an open program, or added with Add files, becomes a new tab; File > Open opens a .py as the program instead, in place of this one.

Adding more blocks

Press Modules under the categories for Add more blocks: every Python module the program can use, each with a line on what it is for, Turtle, Games and Time first. Type to find one: rand finds random, draw finds turtle, game finds pygame. Add puts its import line at the top of the program and its section of blocks under More in the palette, below the categories you know, so nothing you use moves; Remove takes both out again, unless blocks still use it. A block that needs a module, such as square root or any turtle block, brings its import block to the top of the program when you drop it, and Undo takes both away. Modules with no blocks of their own, such as fractions or csv, add just the import, to use in a Python block. Searching the blocks finds a module's blocks by its name too, before it is added.

Your own blocks and libraries

Make a function in Functions makes a block of your own: the blocks under its define block run wherever its run block is. To install a library, press Add a library from a file in Modules and choose a pure-Python wheel (.whl or .zip) or a .py module from your device: its functions turn up as blocks under Libraries, and import works as usual. It is listed in Modules, where Remove uninstalls it.

Saving

Save writes a program that is just main.py as a plain .py file that runs anywhere. With more scripts, pictures, sounds, libraries or watched variables, it saves a .zip instead: every script at the top beside the pictures and sounds, as the program loads them, so unzipping it and running python main.py works on a computer with pygame. A small python-blocks.json in it keeps which variables are watched and where your stacks of blocks are; the Python itself holds only Python. Open the .py or .zip here again and it all comes back as blocks: a .zip with the stacks where you left them, a .py in a tidy column. File > Save as gives every other way out: the Python script of the tab you are in (.py), the whole project (.zip) even when it is one script, a Jupyter notebook (.ipynb) for Jupyter, VS Code or Google Colab, with each other script as a %%writefile cell, a picture of the blocks (.png or .svg) for a worksheet or a slide, and what the program printed (.txt). A notebook opens here too: its code cells become the program and its %%writefile cells become tabs. Copy takes the Python of the tab you are in. As you work, the program is also kept in this browser, within a second of each change: if the page reloads, crashes or is closed, Continue with on the start screen brings it back. Only the last program you changed is kept, on this device alone.

For people who know Python

Each block writes one plain line of Python, and that Python opens again as the same blocks: an empty slot is written ... and an empty block pass, a one-line note on a block is the # comment at the end of its line, and count with i from 1 up to 10 is for i in range(1, 11). No two blocks write the same Python, so nothing turns into another block on the way back: append on a list from Objects becomes Lists' add, the square root in the Math module is the Math square root, and text and numbers share one change by. The Python category holds Python's own lines (import, global, match, a for over two names, any line or value typed in), and a line typed into one turns into blocks when it can. Variables whose names are worked out while the program runs, such as "enemy" + n, are set variable named, change variable named and variable named at the end of Variables, and make a list named in Lists: they write globals()[name]. On a block you have just taken from the palette, typing a new name makes a new variable rather than renaming, unless the new name puts a slip right (scor to score).

Questions

Is my code uploaded?

No. The blocks, the Python and the program's run all happen in this page, on your device. Python itself is loaded from this site, in the background while the page opens. The program is kept in this browser's own storage as you work, so a reload loses nothing; it never leaves the device.

Which Python is it?

Python 3.14 with its standard library, built for the browser (Pyodide). Packages with compiled parts, such as NumPy, are not included; pure-Python ones you open from your device work.

Is it real Python?

Yes. The blocks snap together, and every block is real Python, so you can see the code it makes and move on to typing it.

Is it the real pygame?

It is the site's own pygame-compatible module, written in Python and running in your browser. It has the same names as pygame (display, draw, event, key, mouse, time, font, image, transform, sprite, mixer, Rect, Vector2), so the usual game loop from a tutorial runs unchanged, and the same program runs with real pygame on a computer. A few rarer corners are missing and say so when used.

Does it work on a phone?

Yes. Drag blocks with a finger, and switch between Blocks, Python and Output with the switch beside Run. Pull the Output tab at the foot of the blocks up to keep the output in view while you build.

Can I install Python packages?

Yes, from a file: in Modules, Add a library from a file takes a pure-Python wheel (.whl or .zip) or a .py module from your device, and Remove uninstalls it. Nothing is downloaded from the internet, so packages with compiled parts, such as NumPy, cannot be installed. Python's own modules (math, random, json, turtle and the rest) are always there: add them from the same list.

Can I make the panels bigger or smaller?

Yes. Drag the line between two parts, or focus it with Tab and use the arrow keys. Double-click a line to put it back. Each part keeps a smallest size, so nothing disappears, and the sizes are remembered on this device.

Can a program make new variables while it runs?

Yes. Drop a text or a join, such as "row_" + y, on a variable's name box, or use make a list named in Lists: the name is worked out while the program runs. They are Python's globals()[name], so they make variables of the whole program even inside a function. A name is not the thing it names: adding the join "row_" + y to a list adds that text, not the list. A ! on the block says so, and right-clicking the name offers Use the list itself. A ! also shows when a block reads a name nothing makes, such as "row_" + x inside the loops that make "row_" + y.

Can a program have more than one file?

Yes. Each script is a tab over the blocks: main.py runs, and the others (player.py, enemy.py) are imported from it. Save gives a .py for a one-file program and a .zip for the rest, and opening the .zip brings back every tab. A .zip made by zipping the project's folder opens too.

Why does a function's Python start with global?

A function that sets a variable of the whole program needs a global line in Python, or it would make a new variable of its own. The blocks write that line for you when a set or change block inside a function names a program variable: one used outside every function, or one a function reads or changes before it sets it, so functions share it. A variable made Only inside a function gets none, nor does one that each function using it sets first, such as a counter i: each function has its own. Watched variables (the open eyes) are kept with the blocks, not in the Python.

Can I copy what my program printed?

Yes. Select the text under Output and copy it as you would any text, or press the copy button above it to copy the whole output, tracebacks included.

Your code runs on your device · nothing is uploaded