Examples that run right now¶
This page is seven working d/Python programs. Each card loads a real program into the playground and runs it, so you can watch the language work before you write a line yourself. The sizes shown are the optimized compiled program files, .dbc, that the shipped compiler produced when this site was built, measured by the same test harness that runs the conformance corpus, the set of test programs. Support libraries such as dpython.integers and dpython.text are separate shared library files, .dbl, each bound by hash, that the compiler links only when a program uses them.
UI.SYNC. The engine does the drawing and keeps track of what changed. Click the button or press Space.
A graphics windowFour decorated functions: open sets the title, draw paints with FILL_RECT and DRAW_TEXT, a key press adds one to a counter and asks for a redraw, close does nothing. Click the canvas, then press any key.
Ask for inputinput() pauses the program on the shared console service until you answer; an empty line counts as a real answer, and End input raises EOFError. 2,799 bytes compiled.
Big integersThe hundredth Fibonacci number and two to the hundredth power, exact, through the shared integer library. The same demo as the landing page. 1,561 bytes compiled.
Shared intentsThe SHA-256 hash of abc through dos.intent("COMPUTE", "SHA256", …) with fixed little-endian byte buffers. The hashing code is part of the shared runtime, so it works here in the browser too; this program links two libraries. 3,909 bytes compiled.
Hello, PythonTwo prints and a halt: a console program, its exit status, and the smallest compiled program there is. 183 bytes compiled.
Read the source, then change it¶
Each example is a plain .py file you can read before you run it: hello.py, fibonacci.py, console_input.py, console_hash.py, window.py, retained_window.py, tictactoe.py.
Sixty-six more programs, each with its reviewed expected output and the result from this exact browser build, are on the Conformance Corpus page.
Five more, in the reference¶
These run in the playground too. They were written for the chapters that explain them.
# Word counts in encounter order: text methods and an ordered dictionary.
text = """
Small programs, useful programs.
Python words become DBC programs.
"""
counts = {}
for word in text.split():
word = word.strip(".,:;!?")
counts[word] = counts.get(word, 0) + 1
for word, count in counts.items():
print(word, count)
print("Unique words:", len(counts))
# A stateful callable, sorting keys, and extrema: classes and builtins together.
class Magnitude:
def __init__(self):
self.seen = [0]
self.seen.clear()
def __call__(self, value):
self.seen.append(value)
return abs(value)
key = Magnitude()
values = [-3, 1, -1, 2]
print("Sorted:", sorted(values, key=key))
print("Extrema:", min(values, key=key), max(values, key=key))
print("Key calls:", key.seen)
# Closures keep their cells; each definition gets fresh defaults.
def make_counter(step=1):
count = 0
def bump():
nonlocal count
count += step
return count
return bump
ones = make_counter()
tens = make_counter(10)
print(ones(), ones(), tens(), ones(), tens())
# A context manager, an exception, and cleanup order.
class Scope:
def __enter__(self):
print("enter")
return "resource"
def __exit__(self, kind, value, traceback):
print("exit:", value)
return kind is ValueError
with Scope() as resource:
print("using", resource)
raise ValueError("handled inside")
print("continued")
# stdin: "Zoë 🐍\n"
# The console face asks; the shared service answers.
try:
name = input("Your name: ").strip()
if not name:
name = "friend"
print(f"Hello, {name}!")
except EOFError:
print("No input was provided.")
When you have made something of your own, press Copy share link in the playground: the whole program travels inside the link itself, and nothing is uploaded anywhere.
Continue with Windows & Events, Retained Widgets or Your First Game.