Python Programming For Businesses (2) - Algorithm and Variables

Willis WAN, Chun Yu | 2020-11-25

Programming is Writing an Algorithm for a Problem

In the previous article, I’ve made a quick definition of programming.

Programming is the action of defining a set of command and instructions for computers to execute.

To put in more jargon (that’s what programmer do), let’s rewrite the definition.

Programming is the act of developing an algorithm for computers to execute.

Confused? Let’s walk through an example.

Suppose you are going to teach a 5-year old kid how to make a cup of hot lemon tea. How will you teach them? Well, you may start by giving them some instructions to follow. You may have listed a series of steps so that the child may follow. Your list of instruction may look something like this-

  1. Grab a tea bag.
  2. Put the tea bag inside a mug.
  3. Pour hot water into the mug.
  4. Grab a lemon.
  5. (Under supervision, ) slice the lemon.
  6. Put 3 slices of lemon into the mug of tea.
  7. If you want to have a cup of sweet lemon tea (as most kids do),
    1. Grab sugar.
    2. Add a teaspoon of sugar into the tea.

You give this list to a child, and (preferably under your supervision) the child does all the above step, and viola! The kid makes a cup of lemon tea.

Now stare at the list of instructions. That is your algorithm to the problem: “how to make a cup of lemon tea.” Programming is the process of analysing the problem and producing an algorithm, but instead of feeding the instructions to a kid, we give the instructions to a computer to execute.

Unlike humans, computers execute instructions flawlessly, so long as we provide the right algorithm. To prove a point, take a look at this video, wherein Prof. David Malan explains what an algorithm is, and how two Harvard students make a peanut butter and jelly sandwich.

Video Here

Variables: Basic Building Blocks

Let’s say you need to come to HKUST to find me, but you don’t know how to go to HKUST. I can give you a set of instructions, just like the above:

  1. Go to the nearest MTR station.
  2. Get on a train and arrive at Choi Hung Station.
  3. Exit at C2.
  4. Walk straight ahead and find the bus stop for the route 91M.
  5. Get on 91M.
  6. Alight at HKUST (North).

If you have followed the instructions correctly, you would have arrived at HKUST. Following the instructions will require you to remember a few essential details: Choi Hung Station, Exit C2, and Bus 91M. Similarly, a computer needs to have some ways to remember things to execute your instructions. How does a computer remember things?

A computer has 3 major components: A Central Process Unit (CPU), Main Memory, and Secondary Storage. The CPU is responsible for executing the instructions, while the task of remembering things are shared between main memory and secondary storage. When we write a programme, data that needs to be remembered are typically stored in the main memory. The problem with such an approach is that these data are immediately discarded when the programme is terminated. Data that cannot be discarded are stored, instead, in the secondary storage.

When we programme, we will normally (1) store data in main memory, and (2) transfer the important data into secondary storage. Variables are the primary tool to store data in the main memory.

Assigning Variables

Think of variables as buckets. Whenever we want to store something, we are putting that thing inside the bucket. For example, let’s say we want to store the station name of which we alight. In Python, we will do something like this-

station_name = "Choi Hung"

The = sign doesn’t mean “equal” here; it’s called an assignment. It literally means put the word “Choi Hung” into a bucket called station_name.

Data Types

There are a few types of things that we can put inside a variable. We call these types “data types”. Different programming languages support different data types. In Python, common supported data types include -

  • int (Integer Number)

    imInteger = 10
  • float (Decimal Number)

    imFloat = 99.9
  • str (String)

    imString = "Hello"
  • bool (Boolean)

    imBoolean = True
  • list

    imList = ["Hello", "World", "❤"]
  • tuple

    imTuple = ("Python", "is", "easy")
  • range

    imRange = range(1, 10)
  • set

    imSet = {"IS", "OM"}
  • dict (Dictionary)

    imDictionary = {
    	"Maths": 90,
    	"English": 100,
    	"Chinese": 50

These data types may seem daunting at first, especially the latter ones. But don’t worry, we will work through these data types together, and I’ll show you the most important ideas to grasp.

In the next article, we will take a look at the two numeric data types (int and float). Before I procrastinate post the following article, take a guess -