Python, from Zero, for AILesson 10 of 89

Module 1 · Getting Python running, and your first working code

What happens between your file and the answer

In this lesson

  • 8 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 10 of 10 in this chapter

This chapter’s goalInstall Python on a laptop, a phone or a free browser machine, run the same code both interactively and from a saved file, and predict what a short program prints by naming the type of every value in it and the branch a condition takes.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:49Not available
Full audiobookNot availableNot available
Squaring ten million numbers, four waysfor loop, appending to alist900List comprehension620map() with a lambda700NumPy array, one line15milliseconds on an ordinary laptopThe first three are all Python doing the arithmetic, and they differ by less than a factor of two. Thefourth moves the loop out of Python entirely. This is why the fix for slow numeric code is almostnever a better-written loop.
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.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

Python is not read line by line as it runs

The common picture (Python reads line 1, does it, reads line 2) is wrong in a way that matters later.

When you run python3 budget.py, four things happen:

  1. 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.
  2. It is parsed into a tree representing the structure of the code.
  3. It is compiled to bytecode: a compact instruction set for a made-up machine.
  4. A virtual machine executes that bytecode, one instruction at a time.

The bytecode is not a secret. You can look at it:

Python
import dis
dis.dis(compile("total = a + b", "<demo>", "exec"))
Text
  LOAD_NAME                0 (a)
  LOAD_NAME                1 (b)
  BINARY_OP                0 (+)
  STORE_NAME               2 (total)

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to 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).