Look at bytecode with dis
Six small instructions for one line of Python. Change the function to return price * 2 and run again to see the bytecode change.
When you run a file, Python first compiles your source code into bytecode, a compact set of simple instructions, and then a virtual machine carries those instructions out one by one. The standard Python that does this is called CPython; knowing these steps explains Python's error behaviour and its speed.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Use compile() and co_consts to show what the compiler stores for the source "x = 60 * 60 * 24".
Write a program that prints Running on CPython if platform.python_implementation() is "CPython", and Running on something else otherwise.
A file has a SyntaxError on line 50 and a print("start") on line 1. What appears when you run it, and why?
Explain it without notes
What are the two steps Python performs when you run a file?
Why does a syntax error on the last line stop the first line from running, while a NameError on the last line does not?
What is CPython, and how does it relate to PyPy and Pyodide?
What is in __pycache__, and what happens if you delete it?
Give two reasons pure Python code runs slower than C, and two ways Python programs avoid the problem.
Expected output
3 RESUME 0
4 LOAD_FAST_LOAD_FAST 0 (price, price)
LOAD_CONST 1 (0.18)
BINARY_OP 5 (*)
BINARY_OP 0 (+)
RETURN_VALUE