{"course":{"slug":"python-for-ai","title":"Python, from Zero, for AI","url":"https://addaly.com/learn/python-for-ai","level":"beginner","track":"build"},"lesson":{"slug":"how-python-runs-code","title":"What happens between your file and the answer","url":"https://addaly.com/learn/python-for-ai/how-python-runs-code","position":10,"of":89,"minutes":8,"answer":"Python compiles the whole file to bytecode before running any of it, and its slowness comes from every value being a typed heap object.","keyIdea":"Python compiles the whole file to bytecode before running any of it, and its slowness comes from every value being a typed heap object, which is why moving a numeric loop into NumPy wins fifty-fold where rewriting it in Python wins thirty per cent."},"body":{"format":"markdown","text":"## Python is not read line by line as it runs\n\nThe common picture (Python reads line 1, does it, reads line 2) is wrong in a way that matters later.\n\nWhen you run `python3 budget.py`, four things happen:\n\n1. **The whole file is read and tokenised.** This is why a missing bracket on line 40 stops line 1 from running: the file never became a program.\n2. **It is parsed into a tree** representing the structure of the code.\n3. **It is compiled to bytecode**: a compact instruction set for a made-up machine.\n4. **A virtual machine executes that bytecode**, one instruction at a time.\n\nThe bytecode is not a secret. You can look at it:\n\n```python\nimport dis\ndis.dis(compile(\"total = a + b\", \"<demo>\", \"exec\"))\n```\n\n```\n  LOAD_NAME                0 (a)\n  LOAD_NAME                1 (b)\n  BINARY_OP                0 (+)\n  STORE_NAME               2 (total)\n```\n\nThis excerpt follows four steps: fetch `a`, fetch `b`, add them, and store the result. The full output also includes offsets and other instructions. Instruction names and the exact listing can differ across CPython versions. This is a real skill occasionally: `dis` settles arguments about which of two ways of writing something does less work.\n\n## The `__pycache__` folder\n\nImport a module and Python writes the compiled bytecode into a folder called `__pycache__`, as a `.pyc` file, so the next import can skip steps 1 to 3. It checks the source file's timestamp and size and recompiles when they change.\n\nTwo practical consequences. It is always safe to delete `__pycache__`; the worst outcome is one slightly slower start. And it belongs in `.gitignore`: committing it puts machine-specific compiled files in your repository for no benefit.\n\nThe file you run directly never gets cached, only the modules it imports. That is why a large program starts faster the second time and your one-file script does not.\n\n## Why Python is slow, stated properly\n\n\"Python is slow\" is repeated everywhere and almost never explained. The mechanism is this: **every value is a heap-allocated object carrying its type**.\n\nWhen C adds two integers, the compiler already knows they are integers and emits one CPU instruction. When Python evaluates `a + b`, the virtual machine must: look up what `a` currently refers to, look up its type, find that type's addition method, do the same for `b`, check they are compatible, allocate a **new** object for the result, and update reference counts. That is dozens of machine instructions for one addition, and none of it can be skipped, because `a` could have been an int on the last iteration and a string on this one.\n\nMeasure it in an environment with NumPy installed. The arrays module covers that setup. On Windows, use `python` or `py` in these commands.\n\n```bash\npython3 -m timeit -s \"xs=range(1000000)\" \"sum(x*x for x in xs)\"\npython3 -m timeit -s \"import numpy as np; xs=np.arange(1000000)\" \"(xs*xs).sum()\"\n```\n\nOn an ordinary laptop the first is roughly 60 to 90 milliseconds and the second roughly 1 to 2 milliseconds. That is a 50-fold difference for the same arithmetic. NumPy is not cleverer Python; the loop runs inside compiled C over a block of raw 64-bit integers with no per-value type checks and no object allocation.\n\n> Figure: Squaring ten million numbers, four ways. Bars (milliseconds on an ordinary laptop): for loop, appending to a list 900; List comprehension 620; map() with a lambda 700; NumPy array, one line 15. The first three are all Python doing the arithmetic, and they differ by less than a factor of two. The fourth moves the loop out of Python entirely. This is why the fix for slow numeric code is almost never a better-written loop.\n\nThis is the whole reason a later module in this course teaches arrays and dataframes. **You do not make a numeric Python loop fast by writing better Python.** You move the loop somewhere else. Rewriting it more elegantly buys perhaps 30 per cent; moving it into NumPy buys a factor of fifty.\n\n## What this does and does not license\n\nIt does not mean Python is a bad choice. For an AI script, almost all the wall-clock time is spent waiting for a network reply or for a GPU that a C library is driving. The Python layer is co-ordinating, not computing, and its overhead disappears into the noise. That is exactly why the field standardised on it.\n\nIt does mean you should notice when your own code is doing the arithmetic in a loop over hundreds of thousands of items, because that is the one situation where the language choice shows up in the clock.\n\n## The other implementations\n\nCPython, from python.org, is the one you have. There are others:\n\n- **PyPy** (free, `pypy.org`) runs a just-in-time compiler and can be several times faster on pure-Python loops, at the cost of slower startup and imperfect support for some C extensions.\n- CPython itself has been getting faster since 3.11: the specialising interpreter gained roughly 25 per cent across a broad benchmark suite, and 3.13 added an experimental build without the global lock.\n\nNone of these change the picture above. They shrink the constant; the per-object cost is structural.\n\n> If you take one thing from this lesson: the difference between fast and slow numeric Python is almost never how the loop is written. It is whether the loop is in Python at all."},"check":{"question":"A script spends 40 seconds looping over 2 million rows doing arithmetic. A developer rewrites the loop more elegantly with comprehensions and helper functions and gets 34 seconds. What does this outcome tell you?","options":[{"text":"The rewrite failed to remove the real cost (per-value type lookup and object allocation) which only disappears when the loop itself moves into compiled code such as NumPy.","correct":true},{"text":"Comprehensions are slower than explicit loops, so reverting to a plain `for` would give a larger gain.","correct":false,"tempting":"Inverts a real but tiny effect; comprehensions are usually marginally faster, and either way the difference is nowhere near the factor needed."},{"text":"The remaining time is bytecode compilation, which can be avoided by shipping precompiled `.pyc` files.","correct":false,"tempting":"Mistakes a one-off compilation cost of milliseconds for the running cost of two million iterations."},{"text":"A 15 per cent gain shows the approach is working and repeating the same refactoring will eventually reach acceptable speed.","correct":false,"tempting":"Extrapolates linearly from one improvement, when the remaining cost is a fixed per-operation overhead that further Python-level tidying cannot remove."}]},"prev":{"slug":"if-and-branching","title":"Making a decision: if, elif, else","url":"https://addaly.com/learn/python-for-ai/if-and-branching"},"next":{"slug":"lists-and-dicts","title":"Lists and dicts, and knowing which one you need","url":"https://addaly.com/learn/python-for-ai/lists-and-dicts"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'What happens between your file and the answer', in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/how-python-runs-code (Accessed: 21 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"What happens between your file and the answer","courseTitle":"Python, from Zero, for AI","module":{"index":1,"slug":"getting-python-running","title":"Getting Python running, and your first working code"},"answer":null,"keyIdea":"Python compiles the whole file to bytecode before running any of it, and its slowness comes from every value being a typed heap object, which is why moving a numeric loop into NumPy wins fifty-fold where rewriting it in Python wins thirty per cent.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"caption":"The first three are all Python doing the arithmetic, and they differ by less than a factor of two. The fourth moves the loop out of Python entirely. This is why the fix for slow numeric code is almost never a better-written loop.","series":[{"label":"for loop, appending to a list","value":900},{"label":"List comprehension","value":620},{"label":"map() with a lambda","value":700},{"highlight":true,"label":"NumPy array, one line","value":15}],"title":"Squaring ten million numbers, four ways","type":"bars","unit":"milliseconds on an ordinary laptop"},"alt":"Figure: Squaring ten million numbers, four ways. Bars (milliseconds on an ordinary laptop): for loop, appending to a list 900; List comprehension 620; map() with a lambda 700; NumPy array, one line 15. The first three are all Python doing the arithmetic, and they differ by less than a factor of two. The fourth moves the loop out of Python entirely. This is why the fix for slow numeric code is almost never a better-written loop."}},"blocks":[{"t":"h2","id":"python-is-not-read-line-by-line-as-it-runs","runs":[{"text":"Python is not read line by line as it runs"}]},{"t":"p","runs":[{"text":"The common picture (Python reads line 1, does it, reads line 2) is wrong in a way that matters later."}]},{"t":"p","runs":[{"text":"When you run "},{"text":"python3 budget.py","code":true},{"text":", four things happen:"}]},{"t":"ol","start":1,"items":[[{"text":"The whole file is read and tokenised.","b":true},{"text":" This is why a missing bracket on line 40 stops line 1 from running: the file never became a program."}],[{"text":"It is parsed into a tree","b":true},{"text":" representing the structure of the code."}],[{"text":"It is compiled to bytecode","b":true},{"text":": a compact instruction set for a made-up machine."}],[{"text":"A virtual machine executes that bytecode","b":true},{"text":", one instruction at a time."}]]},{"t":"p","runs":[{"text":"The bytecode is not a secret. You can look at it:"}]},{"t":"code","lang":"python","label":"Python","text":"import dis\ndis.dis(compile(\"total = a + b\", \"<demo>\", \"exec\"))"},{"t":"code","lang":"","label":"Text","text":"  LOAD_NAME                0 (a)\n  LOAD_NAME                1 (b)\n  BINARY_OP                0 (+)\n  STORE_NAME               2 (total)"},{"t":"p","runs":[{"text":"This excerpt follows four steps: fetch "},{"text":"a","code":true},{"text":", fetch "},{"text":"b","code":true},{"text":", add them, and store the result. The full output also includes offsets and other instructions. Instruction names and the exact listing can differ across CPython versions. This is a real skill occasionally: "},{"text":"dis","code":true},{"text":" settles arguments about which of two ways of writing something does less work."}]},{"t":"h2","id":"the-pycache-folder","runs":[{"text":"The "},{"text":"__pycache__","code":true},{"text":" folder"}]},{"t":"p","runs":[{"text":"Import a module and Python writes the compiled bytecode into a folder called "},{"text":"__pycache__","code":true},{"text":", as a "},{"text":".pyc","code":true},{"text":" file, so the next import can skip steps 1 to 3. It checks the source file’s timestamp and size and recompiles when they change."}]},{"t":"p","runs":[{"text":"Two practical consequences. It is always safe to delete "},{"text":"__pycache__","code":true},{"text":"; the worst outcome is one slightly slower start. And it belongs in "},{"text":".gitignore","code":true},{"text":": committing it puts machine-specific compiled files in your repository for no benefit."}]},{"t":"p","runs":[{"text":"The file you run directly never gets cached, only the modules it imports. That is why a large program starts faster the second time and your one-file script does not."}]},{"t":"h2","id":"why-python-is-slow-stated-properly","runs":[{"text":"Why Python is slow, stated properly"}]},{"t":"p","runs":[{"text":"“Python is slow” is repeated everywhere and almost never explained. The mechanism is this: "},{"text":"every value is a heap-allocated object carrying its type","b":true},{"text":"."}]},{"t":"p","runs":[{"text":"When C adds two integers, the compiler already knows they are integers and emits one CPU instruction. When Python evaluates "},{"text":"a + b","code":true},{"text":", the virtual machine must: look up what "},{"text":"a","code":true},{"text":" currently refers to, look up its type, find that type’s addition method, do the same for "},{"text":"b","code":true},{"text":", check they are compatible, allocate a "},{"text":"new","b":true},{"text":" object for the result, and update reference counts. That is dozens of machine instructions for one addition, and none of it can be skipped, because "},{"text":"a","code":true},{"text":" could have been an int on the last iteration and a string on this one."}]},{"t":"p","runs":[{"text":"Measure it in an environment with NumPy installed. The arrays module covers that setup. On Windows, use "},{"text":"python","code":true},{"text":" or "},{"text":"py","code":true},{"text":" in these commands."}]},{"t":"code","lang":"bash","label":"Bash","text":"python3 -m timeit -s \"xs=range(1000000)\" \"sum(x*x for x in xs)\"\npython3 -m timeit -s \"import numpy as np; xs=np.arange(1000000)\" \"(xs*xs).sum()\""},{"t":"p","runs":[{"text":"On an ordinary laptop the first is roughly 60 to 90 milliseconds and the second roughly 1 to 2 milliseconds. That is a 50-fold difference for the same arithmetic. NumPy is not cleverer Python; the loop runs inside compiled C over a block of raw 64-bit integers with no per-value type checks and no object allocation."}]},{"t":"p","runs":[{"text":"This is the whole reason a later module in this course teaches arrays and dataframes. "},{"text":"You do not make a numeric Python loop fast by writing better Python.","b":true},{"text":" You move the loop somewhere else. Rewriting it more elegantly buys perhaps 30 per cent; moving it into NumPy buys a factor of fifty."}]},{"t":"h2","id":"what-this-does-and-does-not-license","runs":[{"text":"What this does and does not license"}]},{"t":"p","runs":[{"text":"It does not mean Python is a bad choice. For an AI script, almost all the wall-clock time is spent waiting for a network reply or for a GPU that a C library is driving. The Python layer is co-ordinating, not computing, and its overhead disappears into the noise. That is exactly why the field standardised on it."}]},{"t":"p","runs":[{"text":"It does mean you should notice when your own code is doing the arithmetic in a loop over hundreds of thousands of items, because that is the one situation where the language choice shows up in the clock."}]},{"t":"h2","id":"the-other-implementations","runs":[{"text":"The other implementations"}]},{"t":"p","runs":[{"text":"CPython, from python.org, is the one you have. There are others:"}]},{"t":"ul","items":[[{"text":"PyPy","b":true},{"text":" (free, "},{"text":"pypy.org","code":true},{"text":") runs a just-in-time compiler and can be several times faster on pure-Python loops, at the cost of slower startup and imperfect support for some C extensions."}],[{"text":"CPython itself has been getting faster since 3.11: the specialising interpreter gained roughly 25 per cent across a broad benchmark suite, and 3.13 added an experimental build without the global lock."}]]},{"t":"p","runs":[{"text":"None of these change the picture above. They shrink the constant; the per-object cost is structural."}]},{"t":"quote","runs":[{"text":"If you take one thing from this lesson: the difference between fast and slow numeric Python is almost never how the loop is written. It is whether the loop is in Python at all."}]}],"check":{"question":[{"text":"A script spends 40 seconds looping over 2 million rows doing arithmetic. A developer rewrites the loop more elegantly with comprehensions and helper functions and gets 34 seconds. What does this outcome tell you?"}],"options":[{"text":[{"text":"The rewrite failed to remove the real cost (per-value type lookup and object allocation) which only disappears when the loop itself moves into compiled code such as NumPy."}],"correct":true,"tempting":null},{"text":[{"text":"Comprehensions are slower than explicit loops, so reverting to a plain "},{"text":"for","code":true},{"text":" would give a larger gain."}],"correct":false,"tempting":[{"text":"Inverts a real but tiny effect; comprehensions are usually marginally faster, and either way the difference is nowhere near the factor needed."}]},{"text":[{"text":"The remaining time is bytecode compilation, which can be avoided by shipping precompiled "},{"text":".pyc","code":true},{"text":" files."}],"correct":false,"tempting":[{"text":"Mistakes a one-off compilation cost of milliseconds for the running cost of two million iterations."}]},{"text":[{"text":"A 15 per cent gain shows the approach is working and repeating the same refactoring will eventually reach acceptable speed."}],"correct":false,"tempting":[{"text":"Extrapolates linearly from one improvement, when the remaining cost is a fixed per-operation overhead that further Python-level tidying cannot remove."}]}],"order":[2,0,1,3],"why":null},"prev":{"slug":"if-and-branching","title":"Making a decision: if, elif, else","minutes":7},"next":{"slug":"lists-and-dicts","title":"Lists and dicts, and knowing which one you need","minutes":7}}}