As I see it, the purpose of programming is to build machines out of bytes that are impossible or too expensive to build out of atoms. Computers are machines that can imitate any other machine. Programming is the act of making them do that.
From the above quote, we can see that a computer is a universal machine.
As we begin the journey of learning how to program on this site, the following characteristics of universal machines will shape the content for this training.
A way to keep track
A way to make decisions
A way to keep going
We achieve the first feature by using variables. The second feature is implemented using branching while the third feature is implemented using loops.
This post will focus on variables. Variables are a container for information, they are used to store values in a program.
The way variables are created in the Python programming language is similar to how they are treated in algebra.
if x = 6, what is x?
If you know algebra, you would answer 6 and you would be right. Like in algebra, the = symbol is for assignment. It assigns the value on the right to the value on the left.
A variable is created the moment you assign a value to it. Once we assign a value to a variable, the Python interpreter creates it.
In the last post about Input and Output, a variable name was created when the input from the user was assigned to it.
Like containers in the real world, the type of material a container is made out of determines what kind of material it can store. In computing, this is the concept of data types.
In the Python programming language, we have 5 standard data types:
Numbers
String
List
Tuple
Dictionary
For this program, we shall only look at numbers. However, I will illustrate Numbers and Strings in this blog post.
We have two types of numbers in computing: integers and decimals. Integers are whole numbers while decimals have a . in them for example, 1 is an integer while 1.0 is a decimal.
Strings are a sequence of characters. Your name is a sequence of characters. Strings are placed in the “”.
The type function will let you know the data type of the variable passed into it. The code below will illustrate the concept in action.
The above image shows that in the first 3 lines we declare the respective variables. Do bear in mind that the variable names are for you, not the users.
Once we use the type function, notice that it gives us the type each variable. The decimal variable is depicted as a float.
Content and Context
This is a very important concept when choosing the data type of a variable. The “label” of a variable is different from its content.
As a result of this, you cannot declare the phone number of a user as either an integer or a float because you cannot add two phone numbers together.
Rather you would define them as a string. As seen in the blog post on Input and Output, you have to convert a number to a string before you can join it to it.
This is important as we continue on our journey.
Learn Python in One Week
If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.
No program would be complete is it didn’t have a way to receive input from the user.
To receive input from the user in the Python programming language, we use the input() function.
The input() function is assigned to a value called a variable. Text is placed in between the brackets which serve as a prompt for the user.
The code above shows how to receive a value from the user. The value is then stored in a variable called name.
The value inside the name variable can then be printed out to the screen.
Concatenation
Concatenation is the joining of strings. In the Python programming language, the “+” is the concatenation operator.
In the example above, when you have a number, you must convert it to a string before you can concatenate it.
Function Overloading
length = input("Please enter the length of the square: ")
length = int(length)
area = length * length
print("The area of a square of length", length, "is", area)
Above is a simple program to find the area of a square. On line 2, the value received by the user is converted into an integer.
On line 4 when we print the output, we get the following output:
Note that the output has added spaces whereas, in the original program, we had none.
Overloading simply means that there are many ways to call a function.
Learn Python in One Week
If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.
Abusua Pa means “Good Family”. It is the symbol of the family unit. Typically in Akan culture, Abusua is the name in Akan culture for a group of people that share common maternal ancestry.
The Abusua line is considered to be passed through the mother’s blood. It is a taboo to marry someone from the same Abusua.
We will use the 5-pixel grid to trace out this image. The image of this is shown below:
Abusua Pa Adinkra Symbol on Grid
Analysing the Symbol
The symbol is a composite symbol made up of a hollowed out square. The inner part of the square consists of four sections which are lined like a window pane.
The outer part of the square intersects with 4 semi-circles. The widths of all the component shapes are the same.
The Plan to Draw the Symbol
To draw this symbol, the first task is to increase the pensize to 40 pixels. Next we move the turtle to the position (-100, 100).
We find the distance between (-100, 100) to (100, -100) and use the drawSquare function to draw the outer square.
Next we move to the position (-100, 0) and draw the horizontal centre line. We then move to the position (0, -100) and draw the vertical centre line.
Once we have completed the outer square and its centre lines, we draw the external semi-circles.
Finally, we reduce the pensize to 5 and draw the inner sections of the symbol.
Algorithm to Draw the Symbol
The algorithm to draw the Abusua Pa symbol is shown below:
Lift up the pen
Set the pensize to 40 pixels
Set the position of the pen to the location (-100, 100)
Place the pen down
Find the length of the side of the outer square
Draw the outer square
Draw the inner centre lines
Draw the outer semi-circles
Reduce the pensize to 5
Draw the inner squares
Using Turtle Graphics
We will use the template.py file and rename it to abusuapa.py.
We then draw a square using the drawSquare function.
drawSquare(length)
The generated image is shown below:
Outer Square
I realize that since we are using the drawSquare function, we no longer need the setposition code. We can comment it out.
To draw the center lines, we have to move the turtle to the left-hand side and move forward by the length of the side. Next, we move the turtle to the bottom, set its heading to 90 degrees and move up by the length of the side. The code to do this is shown below:
To draw the outer circle we will start with the top and move clockwise. To draw the upper circle, we need to move the turtle to the position (50, 120). Then we draw the semi-circle. The code to do this is shown below:
Completing this shape is easy. All we have to do is draw the lines that are within the squares. To do this we must reduce the pensize to 5 and set the orientation of the turtle appropriately to draw the lines.
This article was inspired by a problem I met while buying credits for my electricity meter in Lagos, Nigeria.
The printout for the credits comes in 20 characters as shown below:
12345678901234567890
When you want to enter the figure above into the meter, the meter will ask you for PIN 1, 2, 3, 4, 5. As a result of this, the above number has to be broken down into 5 PINs.
To enter the PINs into my meter, I had to manually separate the printout at every 4 numbers and that was when the idea hit me that this was a good candidate for automation.
In why we program, I go over the fact that we program to automate the solution to a problem. So rather than have a human being carry out a task, a program can be written for a computer to do it.
If we had to write a computer program, we would need to first consider how we would perform the above operation by hand and then breakdown the operation into a series of steps.
The series of steps describing how to carry out the task is called an algorithm.
Algorithms are a sequence of steps which when followed exactly in order will produce the same result.
One area where this is found is in cooking. You can create an infinite variety of meals by altering the ingredients and the order in which they are added while preparing a meal.
So for our problem above, how would we solve the problem? To do this by hand, I counted from the left-hand side and at every 4th number, I placed a comma after it.
So the above printout will now be:
1234,5678,9012,3456,7890
But how would we create an algorithm for this? The steps below will create a generic algorithm for separating at every fourth position.
Ask the user to input a string
Find the length of the string
Loop from 0 to 1 less than the length of the string
At every 4th position, print out the string on a new line
When the loop gets to the end, print out the remaining part of the string
Using the algorithm above, can you create a program which when given:
Aban means “fortress”. It is the symbol of strength and authority.
We will use the 5 pixel grid to trace out this image. The image of this is shown below:
Aban on a Grid
Analysing the Symbol
The symbol is a composite symbol made up of 4 symbols that look like tongs.
A single tong is symmetrical in its quadrant. This property means that we draw a tong and use the steps to draw others.
The Plan to Draw the Symbol
We shall move the pen to the position (0, 80). Once we do this, we shall set the heading of the pen to 45 degrees thus drawing the tong in the upper right quadrant.
Once the drawing of the first tong is completed, the position of the turtle will place it on the x axis on the right.
At that point, we simply set the heading of the pen to 315 degrees and draw the second tong. When the second tong is drawn, the turtle will be on the negative y axis.
At this point, we simply set the heading of the pen to 225 degrees and draw the third tong. When the third tong is drawn, the turtle will be on the negative x axis.
At this point, we simple set the heading of the pen to 135 degrees and draw the fourth tong thus completing our shape.
Algorithm to Draw the Symbol
The algorithm to draw the Aban symbol is shown below:
Lift up the pen
Set the heading to 45 degrees
Move the pen to the position (0, 80)
Set the pen size to half the width of the shape
Place the pen down
Move forward to draw the first line
Turn right by 90 degrees
Move forward to draw half of the first line
Turn left by 90 degrees
Move forward to draw the protruding line at a distance of 5/8 of the length
Lift up the pen
Reverse the turtle by moving back the same distance as you moved forward
Turn right by 90 degrees
Place the pen down
Draw the second half of the line
Turn right by 90 degrees
Move forward to draw the third line
Change the heading to 315 and repeat steps 6 – 17
Change the heading to 225 and repeat steps 6 – 17
Change the heading to 135 and repeat steps 6 – 17
Using Turtle Graphics
We will use the template.py file and rename it to aban.py.
In step 4 of the algorithm to draw Aban, we have to find the width of the shape. We will also need to find the length of the shape.
To find the width of the line, we need to find the distance between two parallel points it passes through and this are: (120, 0) and (40, 80).
To find the length of a side of the Aban symbol, we take the point (0, 80) as our starting point and get the coordinates of the centre point (80, 160) which is where a line drawing the symbol would stop and turn.
We have to introduce a function coordinateDistance to give us the distance between the two coordinates. The function definition is shown below:
def coordinateDistance(x1, y1, x2, y2):
dx = x1 – x2
dy = y1 – y2
D = math.sqrt((dx * dx) + (dy * dy))
return D
To find the width and length, we use the expressions below:
Since steps 6 to 17 will be repeated 3 more times, they are a good candidate for a function. We shall call this function drawTong.
Steps 6 to 17 can be automated using the drawTong function. We will first write out the commands for the steps to check then we will later integrate them into the drawTong function.
To draw the shape completely, we need to call this function and alternate with the setheading angle that is representative of the quadrant the function will draw in. The code to do this is shown below: