learn python with the White Room analogy

Monty Will Help You Learn Python Coding, But Who’s Monty? (The White Room Series #1)

When you learn Python coding, you start by learning about the tools you’ll need and the rules for each one of those tools. You learn the syntax of the for loop and what it does, for example. With time, you also learn when to use this tool and when not to use it.

What’s more challenging in the early and not-so-early days when you learn Python coding is to join the dots between the various topics you learn and to view them as a coherent set of tools all working together.

Making the transition from knowing how to use lots of separate programming tools to seeing those tools as aspects of the same story is one of the rites of passage to move from beginner to intermediate, whichever way you define beginner and intermediate.

Learn Python With Monty and The White Room

OK, let’s get to the main point of this article, then. This blog post will introduce a narrative that describes how a computer program really works behind the scenes. Often, the best way to understand abstract concepts is by using analogies. So let me introduce you to The White Room analogy.

Let’s start by personifying the computer program. I call the program Monty. If you don’t get the reference, you can look up the origins of the name Python.

Monty is the one who’s busy doing things, performing all the actions required in your computer program. But Monty/the computer program needs some infrastructure to be able to operate. This infrastructure is the White Room: an empty room with white walls and white ceilings–a blank canvas.

However, the White Room is not entirely empty. There is a set of shelves on one of the walls where Monty can store things he may need later on.

The shelves are also not entirely empty. There is a small, red booklet on the bottom shelf with the label “built-in” on it. This booklet has several things that Monty can start using right away, such as print() and True.

Let’s Start Writing A Python Program

Whenever you write any word in your computer program, Monty will look around the White Room to try and find this word. He’ll look inside the red “built-in” booklet, and he’ll also look at anything else that might be on the shelves or elsewhere in the room. However, when you start writing a program, there’s nothing other than the red “built-in” booklet in the room. Therefore this is the only place Monty can look.

You write the following:

print("Hello World")

Monty will look around the White Room. There’s only the red “built-in” booklet, so he looks through it until he finds the name print. This tells him what to do. I’ll fully describe how functions fit into the White Room analogy in a later blog post in this series.

You now try writing the following:

hurray

But this time, you get the following error:

NameError: name 'hurray' is not defined

Monty looked everywhere in the White Room, but he couldn’t find the name hurray anywhere, so he responds by saying: “I don’t know what the word hurray is, sorry!”

Assigning Data

What happens if you assign data to a name:

my_number = 10

Monty sees the assignment operator = and he knows what he needs to do. He brings an empty box and sticks a label that says my_number on the outside of the box. He then puts the number 10 inside the box and places the box on one of the shelves in the White Room.

Monty can’t see the number 10 as it’s inside the box. However, he can see the box’s label which says my_number. So, from now on, whenever you use the name my_number in your program, Monty will look around the White Room, and he’ll see the label on the box. Therefore, he’ll bring the box down and look at what’s inside the box, which in this case is the integer 10.

Importing A Module

Let’s take this one step further:

import random
​
my_number = random.randint(1, 10)
print(my_number)

Let’s look at what happens when the program goes through these three lines of code.

import random

When Monty sees the keyword import, he knows he needs to go for a walk. He leaves the White Room and walks around the block until he sees a large building. This building is the town’s library. When he steps inside, he finds himself in a large hall with very high ceilings and bookshelves full of books covering every inch of every wall, from floor to ceiling.

There are over a hundred thousand books in this library. But Monty is looking for the book named random. Once he finds it, he takes it with him back to the White Room and places it on one of the shelves in the White Room. The book’s name, random, is visible on the spine of the book.

my_number = random.randint(1, 10)

Monty then moves on to the following line. He sees the assignment operator, so he gets a box and labels it my_number. As Monty reads what’s on the right-hand side of the = sign, he reads the name random. Monty looks around the White Room and finds a book called random. It’s the one he just brought from the library.

The dot after random tells Monty to open the book, and he looks for the name randint inside the random book. Monty knows what to do when he finds this function, and he ends up with a number, let’s say 7.

Monty places the number 7 inside the box labelled my_number and puts the box on one of the shelves in the White Room.

print(my_number)

Monty moves to the final line of code. This line starts with the name print, which Monty finds in the red booklet called “built-in”. This tells him what to do, but first, he needs to look for something with the name my_number, which will be needed in print(). This is where he spots the box with that label. He looks inside the box and fetches the number 7, which is stored in the box. This is the number he’ll use in print().

Final Words

In the next blog post in The White Room Series, I’ll discuss functions in more detail and introduce the Function Room. If you wish, you can read the detailed description of the White Room analogy in the following standalone chapter of The Python Coding Book: The White Room: Understanding Programming.

I find analogies key to help me understand and learn Python. If you too like to visualise abstract concepts, you’ll hopefully find the White Room analogy a useful tool to learn Python coding.

Further Reading


Subscribe to

The Python Coding Stack

Regular articles for the intermediate Python programmer or a beginner who wants to “read ahead”


2 thoughts on “Monty Will Help You Learn Python Coding, But Who’s Monty? (The White Room Series #1)”

  1. I am around 35 and during the COVID-19 pandemic I did some soul searching. Eventually I understood my honest passion lies in Technology and Computer programming. So, six months ago I started learning and exploring. Currently, I am practicing sample projects using HTML and CSS. And on the other hand, I have started learning Python.

    There was a period I was confused about which one to pick, JavaScript or Python! Eventually I decided to go with Python. Today I came across your article and I just finished reading this page. I really liked it and the way you explained it was very helpful for me. Thus I decided to leave a comment and some good wishes for you.

    Thank you for this. I look forward to reading more.

    1. Thank you for your comment. It’s nice to see that people found what I write useful. Good luck with your journey learning Python, and hopefully the text on these pages will help a bit along the way!

Leave a Reply