Python: The user's manual (I)

Variables

Modern computers store, process and transmit data. Programs running on a computer often need a way to store data temporarily and access it quickly. This might be how far down a webpage you have scrolled, the current song you are listening to or the matrix representing the content currently on your screen. This type of data is stored in random-access memory, which is storage that is easily read from and written to. RAM is not persistent – once you switch off the computer all the data stored in using RAM is wiped from the disk.

RAM is a little bit like a cloakroom. Imagine that in our hypothetical cloakroom there is a row of cupboards and each cupboard has a unique number. Lots of different people want to get items out of the cupboard. Let's imagine that there are a number of cloakroom assistants who will retrieve items from, and place items into the different cupboards. When someone wants to place an item into a cupboard they give the cloakroom assistant a number as well as the item they want placed in that cupboard. The cloakroom assistant goes along and places the item in the cupboard. Let's assume that we have a very single-minded cloakroom assistant who will take the item you have and try and place it inside the specified cupboard. It doesn't matter whether or not the item will fit inside the cupboard or, worse, if there is already something inside the cupboard. The cloakroom assistant will just try to place the item inside the cupboard. This situation would cause a real problem for all the different users of the cloakroom; one solution to this problem is that the cloakroom assistant keeps an index stating what is stored in each cloakroom which all the people wanting to store data in can take and read. It would be even more helpful if the cloakroom assistant just took the items you gave them and placed them somewhere in the cloakroom before giving them back to you when you ask for them.

In this RAM analogy the cloakroom assistant is the operating system (e.g. Windows, Mac OS, Linux or Unix). The operating system helps to manage the memory of a computer. The cloakroom assistant who takes the items you give them and places them in an empty in the cloakroom automatically is essentially the Python interpreter.

To store data in Python, the programming language offers what is known as a variable. A variable is essentially an easy to read name referring to a position (think a single cupboard) or several positions (think several cupboards) in memory. Before you can use a variable you need to tell Python that it exists. This is known as variable declaration.

x = 0 # Text which follows a hashtag is ignored by the interpreter and is known as a comment

Behind the scenes, the program running your program is doing something a bit like this:

Oh look, I found an x. This must be a variable name. Next to this x I've found an equal sign – this must mean that someone is trying to store a value in a variable called x. To the right of this equals sign is a value – it's a number! Let me find a place where I can store this number. Let me ask the operating system whereabouts I can find a place to store this data. Oh look, the operating system has told me that there's a place in memory at position 1101 that's empty that I can use. Let me store this data there. Next to this number there's a hashtag – that's a comment so I can ignore that, after all it's only there to help humans understand the code better.

There are lots of different types of data you can store.

a_list = [1, 12, 42, 19, 12, 3, 14, 15]
a_string = "Hello World!"
an_integer = 150 # cannot store decimals
a_float = 12.243 # can store decimals