Categories

## Arithmetic Operators

There are seven basic arithmetic operators in the Python programming language.

They are:

2. Subtraction
3. Multiplication
4. Division
5. Integer Division
6. Exponentiation
7. Modulus

This is represented by the + symbol. It adds two numbers together and gives their sum.

## Subtraction

This is represented by the – operator. It takes the value on the right from the value on the left to give the difference ot the two values.

## Multiplication

This is represented by the * operator. It multiplies the value on the left with the value on the right to give a product of the two.

## Division

This is represented by the / operator. It divides the value on the left with the value on the right to give the quotient.

## Integer Division

This is a special form of division represented by //. It gives the whole number value of the quotient.

## Exponentiation

This is represented by the ** operator. It gives the result of a value raised to another value.

## Modulus

This is the remainder of division of one number by another number. It is used to test if the number on the right is a factor of the number of the left.

## Conclusion

The above operators are how arithmetic is done in the Python programming language on a basic level.

Categories

## Statements and Expressions

Computers as machines deal work with data. Data is processed by the computer under the control of a program.

The program takes the data as input, processes it and produces an output.

The code that powers the programs does so using statements and expressions.

## Statements

A statement is an instruction that tells the computer to do something. They carry out an action in the code for a computer program.

On Day 1, you used the assignment statement. The assignment statement is represented by “=”. When you created a variable, the assignment statement is used to set its value.

An assignment statement associates a value with a variable.

Another assignment statement we have seen so far is the print statement. The print statement displays a value placed within it by the user.

## Expressions

An expression is a piece of code that returns data. Expressions are a sequence of values, joined by operators that can be simplified to other values.

For example, 2 + 2 is an expression representing the value 4.

Expressions are evaluated by the Python interpreter. If you type in an expression into the Python interpreter, it evaluates it and displays the result.

The first line is an expression that has a value of 5. When 1 + 1 is added, we get 2 so that is also an expression.

x = 5 is the assignment statement in action. x = x + 1 will take the current value in x on the right-hand side and add 1 to it. Then assign that value back to x.

The print statement displays the value in the x variable which is 6.

Categories

## Variables and Data Types

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.

Eric Hodges

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.

1. A way to keep track
2. A way to make decisions
3. 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:

1. Numbers
2. String
3. List
4. Tuple
5. 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.

Categories

## 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.

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.

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

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

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

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.setposition(0, -100)
turtle.pendown()
turtle.forward(length)

The generated image 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:

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

The generated image is shown below:

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.pendown()
turtle.circle(50, 180)

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

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

The generated image 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.

The code below will draw all the vertical lines:

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

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

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

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

The generated image is shown below:

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.pendown()
turtle.forward(length)

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

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

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

The generated image is shown below:

## Complete Code

```"""
Project Name: Drawing Adinkra Symbols using Python
Developer Name: Truston Ailende
"""
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.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.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.pendown()
turtle.circle(50, 180)

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

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

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

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

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

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

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

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

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

turtle.penup()
turtle.setposition(-100, 60)
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.