Categories
Python Programming

Input and Output

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.

Receiving input in the Python programming language.

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 in Python

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.

Categories
Drawing Adinkra Symbols using Python

Abusua Pa

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
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:

  1. Lift up the pen
  2. Set the pensize to 40 pixels
  3. Set the position of the pen to the location (-100, 100)
  4. Place the pen down
  5. Find the length of the side of the outer square
  6. Draw the outer square
  7. Draw the inner centre lines
  8. Draw the outer semi-circles
  9. Reduce the pensize to 5
  10. Draw the inner squares

Using Turtle Graphics

We will use the template.py file and rename it to abusuapa.py.

The code for steps 1 to 4 is given below:

turtle.penup()
turtle.pensize(40)
turtle.setposition(-100, 100)
turtle.pendown()

To find the length between the two points, we use the coordinateDistance function which is shown below:

def coordinateDistance(x1, y1, x2, y2):
dx = x1 – x2
dy = y1 – y2
D = math.sqrt((dx * dx) + (dy * dy))
return D

We calculate the length between the two points using the code shown below:

length = coordinateDistance(-100, -100, 100, -100)

We then draw a square using the drawSquare function.

drawSquare(length)

The generated image is shown below:

Outer Square
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:

turtle.setposition(-100, 0)
turtle.pendown()
turtle.forward(length)
turtle.penup()
turtle.setheading(90)
turtle.setposition(0, -100)
turtle.pendown()
turtle.forward(length)

The generated image is shown below:

Centre Lines
Centre Lines

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:

turtle.penup()
turtle.setposition(50, 120)
turtle.pendown()
turtle.circle(50, 180)

The generated image is shown below:

First Semi-Circle
First Semi-Circle

To draw the remaining semi-circles, we move clockwise and also change the heading of our turtle accordingly. The code to do this is shown below:

turtle.penup()
turtle.setposition(120, -50)
turtle.setheading(0)
turtle.pendown()
turtle.circle(50, 180)

turtle.penup()
turtle.setposition(-50, -120)
turtle.setheading(270)
turtle.pendown()
turtle.circle(50, 180)

turtle.penup()
turtle.setposition(-120, 50)
turtle.setheading(180)
turtle.pendown()
turtle.circle(50, 180)

The generated image is shown below:

Outer Semi-Circles
Outer Semi-Circles

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.

The code below will draw all the vertical lines:

turtle.penup()
turtle.pensize(5)
turtle.setposition(-60, -100)
turtle.setheading(90)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(-40, -100)
turtle.setheading(90)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(40, -100)
turtle.setheading(90)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(60, -100)
turtle.setheading(90)
turtle.pendown()
turtle.forward(length)

The generated image is shown below:

Vertical Lines
Vertical Lines

To draw the remaining horizontal lines, I shall start from the bottom of the symbol and work my way up.

The code to do this is shown below:

turtle.penup()
turtle.setposition(-100, -60)
turtle.setheading(0)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(-100, -40)
turtle.setheading(0)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(-100, 40)
turtle.setheading(0)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(-100, 60)
turtle.setheading(0)
turtle.pendown()
turtle.forward(length)

The generated image is shown below:

Final Symbol
Final Symbol

Complete Code

"""
Project Name: Drawing Adinkra Symbols using Python
Developer Name: Truston Ailende
Email Address: trustonailende@gmail.com
"""
import turtle
import math

# Square
def drawSquare(length):
    turtle.penup()
    turtle.setposition(-length/2.0, length/2.0)
    turtle.pendown()
    for i in range(0, 4):
        turtle.forward(length)
        turtle.right(90)
    turtle.penup()
    turtle.home()

# Horizontal lines
def drawHorizontalLine(length, division):
    pixelSpace = int(length / division)
    half = int(length / 2)
    for j in range((-half + pixelSpace), half, pixelSpace):
        turtle.penup()
        turtle.setposition(-half, j)
        turtle.pendown()
        turtle.forward(length)
    turtle.penup()
    turtle.home()

# Vertical lines
def drawVerticalLine(length, division):
    pixelSpace = int(length / division)
    half = int(length / 2)
    turtle.right(90)
    for k in range((-half + pixelSpace), half, pixelSpace):
        turtle.penup()
        turtle.setposition(k, half)
        turtle.pendown()
        turtle.forward(length)
    turtle.penup()
    turtle.home()

# Draw the grid
drawSquare(400)
drawHorizontalLine(400, 40)
drawVerticalLine(400, 40)

# Change the colour mode
turtle.colormode(255)

# Change the pencolor to red
turtle.pencolor(255, 0, 0)

# Draw the horizontal centre line
turtle.setposition(-200, 0)
turtle.pendown()
turtle.forward(400)
turtle.penup()

# Draw the vertical centre line
turtle.setposition(0, 200)
turtle.setheading(270)
turtle.pendown()
turtle.forward(400)

# Reset all the properties
turtle.home()
turtle.pencolor(0, 0, 0)

# Place code here
turtle.penup()
turtle.pensize(40)
turtle.setposition(-100, 100)
turtle.pendown()

def coordinateDistance(x1, y1, x2, y2):
    dx = x1 - x2
    dy = y1 - y2
    D = math.sqrt((dx * dx) + (dy * dy))
    return D

length = coordinateDistance(-100, -100, 100, -100)
drawSquare(length)

turtle.setposition(-100, 0)
turtle.pendown()
turtle.forward(length)
turtle.penup()
turtle.setheading(90)
turtle.setposition(0, -100)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(50, 120)
turtle.pendown()
turtle.circle(50, 180)

turtle.penup()
turtle.setposition(120, -50)
turtle.setheading(0)
turtle.pendown()
turtle.circle(50, 180)

turtle.penup()
turtle.setposition(-50, -120)
turtle.setheading(270)
turtle.pendown()
turtle.circle(50, 180)

turtle.penup()
turtle.setposition(-120, 50)
turtle.setheading(180)
turtle.pendown()
turtle.circle(50, 180)

turtle.penup()
turtle.pensize(5)
turtle.setposition(-60, -100)
turtle.setheading(90)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(-40, -100)
turtle.setheading(90)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(40, -100)
turtle.setheading(90)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(60, -100)
turtle.setheading(90)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(-100, -60)
turtle.setheading(0)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(-100, -40)
turtle.setheading(0)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(-100, 40)
turtle.setheading(0)
turtle.pendown()
turtle.forward(length)

turtle.penup()
turtle.setposition(-100, 60)
turtle.setheading(0)
turtle.pendown()
turtle.forward(length)

# End the program
turtle.done()

Where Can It Be Found?

A brief Google search led to only one significant reference online which is the Abusua Pa Experience.

Abusua Pa Experience is a travel and tour experience company based in Cape Coast, Ghana.

Summary

At the end of this post, we have successfully used the Python Turtle environment to draw the Abusua Pa symbol.

The code for this series is available on GitHub. Please feel free to check it out.

Next time, we will look at the Akoma Ntoaso Adinkra symbol.

Support this Series

Using the Adinkra symbols, I created the Adinkra Notebooks Collection.

You can support this series by buying one of them.

Categories
Python Programming

Algorithms

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.

  1. Ask the user to input a string
  2. Find the length of the string
  3. Loop from 0 to 1 less than the length of the string
  4. At every 4th position, print out the string on a new line
  5. 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:

12345678901234567890

Will output:

1234
5678
9012
3456
7890

Let me know how you fared in the comments below.

Categories
Drawing Adinkra Symbols using Python

Aban

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:

  1. Lift up the pen
  2. Set the heading to 45 degrees
  3. Move the pen to the position (0, 80)
  4. Set the pen size to half the width of the shape
  5. Place the pen down
  6. Move forward to draw the first line
  7. Turn right by 90 degrees
  8. Move forward to draw half of the first line
  9. Turn left by 90 degrees
  10. Move forward to draw the protruding line at a distance of 5/8 of the length
  11. Lift up the pen
  12. Reverse the turtle by moving back the same distance as you moved forward
  13. Turn right by 90 degrees
  14. Place the pen down
  15. Draw the second half of the line
  16. Turn right by 90 degrees
  17. Move forward to draw the third line
  18. Change the heading to 315 and repeat steps 6 – 17
  19. Change the heading to 225 and repeat steps 6 – 17
  20. 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.

The code for steps 1 to 3 is given below:

turtle.penup()
turtle.setheading(45)
turtle.setposition(0, 80)

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:

width = coordinateDistance(120, 0, 40, 80)
length = coordinateDistance(0, 80, 80, 160)

The code for steps 4 and 5 is shown below:

turtle.pensize(width / 2)
turtle.pendown()

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.

The code for steps 6 to 10 is given below:

turtle.forward(length)
turtle.right(90)
turtle.forward(length / 2)
turtle.left(90)
turtle.forward(length * 5 / 8)

The generated image is shown below:

Half Tong
Half Tong

The code to lift the pen is straightforward enough. To reverse the shape, we need to move backward the same number of steps as forward.

The code to do this is shown below:

turtle.penup()
turtle.backward(length * 5 / 8)
turtle.right(90)
turtle.pendown()
turtle.forward(length / 2)
turtle.right(90)
turtle.forward(length)

The generated image is shown below:

Full Tong
Full Tong

Now that we have drawn one part of the symbol, we can use the code to create the drawTong function so that we don’t have to repeat ourselves.

The code for the function is shown below:

def drawTong():
turtle.forward(length)
turtle.right(90)
turtle.forward(length / 2)
turtle.left(90)
turtle.forward(length * 5 / 8)
turtle.penup()
turtle.backward(length * 5 / 8)
turtle.right(90)
turtle.pendown()
turtle.forward(length / 2)
turtle.right(90)
turtle.forward(length)

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:

drawTong()
turtle.setheading(315)
drawTong()
turtle.setheading(225)
drawTong()
turtle.setheading(135)
drawTong()

The generated image is shown below:

Completed Symbol
Completed Symbol

Complete Code

"""
Project Name: Drawing Adinkra Symbols using Python
Developer Name: Truston Ailende
Email Address: trustonailende@gmail.com
"""
import turtle
import math

# Square
def drawSquare(length):
    turtle.penup()
    turtle.setposition(-length/2.0, length/2.0)
    turtle.pendown()
    for i in range(0, 4):
        turtle.forward(length)
        turtle.right(90)
    turtle.penup()
    turtle.home()

# Horizontal lines
def drawHorizontalLine(length, division):
    pixelSpace = int(length / division)
    half = int(length / 2)
    for j in range((-half + pixelSpace), half, pixelSpace):
        turtle.penup()
        turtle.setposition(-half, j)
        turtle.pendown()
        turtle.forward(length)
    turtle.penup()
    turtle.home()

# Vertical lines
def drawVerticalLine(length, division):
    pixelSpace = int(length / division)
    half = int(length / 2)
    turtle.right(90)
    for k in range((-half + pixelSpace), half, pixelSpace):
        turtle.penup()
        turtle.setposition(k, half)
        turtle.pendown()
        turtle.forward(length)
    turtle.penup()
    turtle.home()

# Draw the grid
drawSquare(400)
drawHorizontalLine(400, 40)
drawVerticalLine(400, 40)

# Change the colour mode
turtle.colormode(255)

# Change the pencolor to red
turtle.pencolor(255, 0, 0)

# Draw the horizontal centre line
turtle.setposition(-200, 0)
turtle.pendown()
turtle.forward(400)
turtle.penup()

# Draw the vertical centre line
turtle.setposition(0, 200)
turtle.setheading(270)
turtle.pendown()
turtle.forward(400)

# Reset all the properties
turtle.home()
turtle.pencolor(0, 0, 0)

# Place code here
turtle.penup()
turtle.setheading(45)
turtle.setposition(0, 80)

def coordinateDistance(x1, y1, x2, y2):
    dx = x1 - x2
    dy = y1 - y2
    D = math.sqrt((dx * dx) + (dy * dy))
    return D

width = coordinateDistance(120, 0, 40, 80)
length = coordinateDistance(0, 80, 80, 160)

turtle.pensize(width/2)
turtle.pendown()

def drawTong():
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(length / 2)
    turtle.left(90)
    turtle.forward(length * 5 / 8)
    turtle.penup()
    turtle.backward(length * 5 / 8)
    turtle.right(90)
    turtle.pendown()
    turtle.forward(length / 2)
    turtle.right(90)
    turtle.forward(length)

drawTong()
turtle.setheading(315)
drawTong()
turtle.setheading(225)
drawTong()
turtle.setheading(135)
drawTong()

# End the program
turtle.done()

Where Can It Be Found?

A brief Google search led me to the following places where you can get branded Aban products:

  1. Aban Zanzibar Trinity Rings on Simbasante
  2. Socks by Adinkra Republic
  3. Protection Adinkra Earrings by Ahima
  4. Aban Gold Pendant on Amazon
  5. Aban Wooden Stamp on Etsy

Summary

At the end of this post, we have successfully used the Python Turtle environment to draw the Aban symbol.

The code for this series is available on GitHub. Please feel free to check it out.

Next time, we will look at the Abusua Pa Adinkra symbol

Support this Series

Using the Adinkra symbols, I created the Adinkra Notebooks Collection.

You can support this series by buying one of them.

Categories
Python Programming

Programming Languages

Computer programming languages allow us to give instructions to a computer in a language the computer understands.

The diagram above shows how programming languages hide details from the user.

At the base of the diagram, we have the computer hardware.

The hardware refers to the part of the computer that we can see and touch.

To control a computer, we need to use a machine language. Each computer has it’s own machine language and programmers found it hectic to have to learn because it was in binary.

As time when on, the Assembly Language was developed. Assembly Language provided a symbolic representation of the machine code needed to program a given CPU architecture.

The real breakthrough in computer programming would start with the arrival of high level languages.

High level languages were closer to English and in addition to making the task of computer programming easier, they also opened the field to a lot more people.

The top four from that period in computing history are C, BASIC, FORTRAN and COBOL.

Applications running this languages are still in use today.

We will be learning the Python programming language.

According to the TIOBE Index as at January 2020, the Python programming language is the 3rd most popular programming language in the world.

Each language has it’s strength and weakness but for absolute beginners, I will give my reasons why recommend learning the Python programming language first.

Simplicity

When you are starting out on the journey of learning to code, you don’t want to deal with problems that occur while setting up your computer.

While learning Java at the University, one of the biggest issues we would run into was the setting of classpath.

The issue of classpath would stop many beginners dead in their tracks.

In Python, once you have installed the interpreter, you are ready to start programming.

In addition, the language is simple and readable. The commands in the language look like English.

Versatility

Python is a general purpose language that caters to every use case that you can imagine.

It might not beat a specialized language in its domain but it will allow you get started in any domain.

Large Community

The true secret weapon of the Python programming language is the community.

The versatility of the Python programming language is due to the libraries developed by the community.

Conclusion

As technology become more integrated with everyday life, learning how to program will become important in being a member of the society.

For absolute beginners, the Python programming language is great for starting because it is simple and easy to use.

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.