Skip to main content

Python Playground

Run Python code in your browser with printed output, input() and tracebacks, with several files and optional packages, and a zip download.

# Count how often each word appears
text = "the quick brown fox jumps over the lazy dog the end"

squares = [n * n for n in range(1, 6)]
print("Squares:", squares)


def count_words(sentence):
counts = {}
for word in sentence.split():
counts[word] = counts.get(word, 0) + 1
return counts


counts = count_words(text)
for word, number in sorted(counts.items(), key=lambda item: -item[1])[:3]:
print(f"{word!r} appears {number} time(s)")

Output

What your program prints appears here. Press Run.

Shortcuts: Ctrl+Enter run · / search (use ⌘ on Mac)

Free Online Python Playground

Run Python code in your browser. Type a program on the left, press Run, and what it prints appears on the right, with input boxes for input() and short error messages that point at the line. Your code runs on your device and is never uploaded, so it is a safe place to try ideas, learn, or check a small script. Run Python in your browser: print output, answer input() prompts and use several files, without installing anything or uploading your code.

How to use the playground

  1. Write Python in main.py. The sample counts words, so you can change it and see what happens.
  2. Press Run (or Ctrl+Enter). The first time, the Python engine loads for a few seconds.
  3. If the program calls input(), a box appears under the output: type your answer and press Enter.
  4. Use Stop if a program does not finish. After the time limit it is stopped for you.

Several files and packages

Press + to add a file such as helpers.py, then write import helpers in main.py. Run starts the file you have open. The ⋯ menu downloads every file in one zip. For packages that are not part of Python itself, such as numpy, press Add packages: it downloads package files from cdn.jsdelivr.net and sends none of your code. Drawing with matplotlib is not available. If you also work with text patterns, the Regex Tester is handy, and the JavaScript Playground runs the same kind of experiment in another language.

Worked example

Paste this into main.py and press Run:

name = input("Your name? ")
scores = {"Ana": 3, "Ben": 5}
scores[name] = len(name) * 2
print(f"{name} scores {scores[name]}")
print(sorted(scores, key=scores.get, reverse=True))

The program asks for a name. If you type Mia, the output is:

Your name? Mia
Mia scores 6
['Ben', 'Mia', 'Ana']

Common mistakes

  • Mixing tabs and spaces to indent. Python needs the lines of one block to line up the same way.
  • Forgetting the colon at the end of if, for, def and while lines.
  • Importing a package such as numpy and expecting it to be there. Press Add packages first.
  • Pressing Run while helpers.py is open: Run starts the open file, so open main.py.

Frequently asked questions

Is my Python code sent to a server?
No. Python itself runs inside your browser, so your code and what it prints stay on your device. The only thing that can leave is the optional package download, which asks for package files and sends none of your code.
Why does the first run take a few seconds?
The first run loads the Python engine, about 12 MB. Your browser keeps it, so later runs and later visits start much faster. If you press Stop, the engine is started again the next time you run.
How do I use input()?
When the program reaches input(), a box appears under the output. Type your answer and press Enter. The program then runs again from the top with your answers, so do not rely on the time or on files that change between runs before an input() call.
Can I use numpy or pandas?
Yes, once you press Add packages. It downloads the files for the packages your code imports from cdn.jsdelivr.net. Nothing downloads without that button, and pictures from matplotlib are not offered because there is no drawing canvas here.
My program stopped with "Stopped after 10 seconds". Why?
Python was still running when the time limit ended, most often because of a loop that never finishes, such as while True without a break. Fix the loop, or raise the time limit next to Auto-run for work that really takes longer.
How do I split my code over several files?
Press + beside the tabs and name the file, for example helpers.py. In main.py write import helpers and call helpers.name(). Run starts the file that is open, so open main.py before you press Run.
Can I run Python without installing it?
Yes. Python runs inside your browser, so there is nothing to install and your code is never uploaded.

Related tools

More Playground

View all Playground tools →