“Creating Patterns in Python” — #LearnPythonThroughProjects: Series 7

Aminah Mardiyyah Rufai
8 min readAug 26, 2020

We have come so far these last few weeks. Buckle up! more exciting projects coming your way!

This project is all about creating some fun shapes in Python using nested for loops and conditional if-else statements. In this series, we will be creating several patterns, then I will share links to resources and source codes for further exploration.

The first pattern is a “Multiplication Table”. Let’s dive in!

As always, create a new python file, save as “multiplication.py”

PSEUDOCODE FOR MULTIPLICATION TABLE

  1. Define a variable ‘num’. Use the input function and the int function.
  2. Create an iterative condition using a “for loop”. Use the range function for a start and end at 1 and 13.
  3. Print the value of ‘i’ alongside iterative.

Save and run the program.

CODE EXPLANATION

A ‘num’ variable is defined to accept input from a user. The ‘int’ function is used to enclose the ‘input’ function here such that whatever input is entered by the user, is converted to an integer so that arithmetic operations(in this case multiplication) can be carried out effectively.

The “for loop” is used here for a finite iterative process, not an infinite program i. the program starts and ends within the limit set by the range function. Also, take note that the ‘X’ here does not represent multiplication, it is used as a string. You will understand better when you see the code working. The single ‘=’ sign there is also a string because we enclosed it in quotes. You remember that’s how Python recognizes strings right? Finally, num*i, says whatever number is entered, keep multiplying by ‘i’ until, ‘i’ gets to 12, Then stop.

So the range function works in such a way that it generates a set of output or numbers, starting at a defined start point(usually zero by default unless otherwise changed) and ends at a step behind the endpoint. So in this example, the start point was 1, and the endpoint although set at 13, ended at 12(a step behind). More explanation as we create more patterns.

Great! Now Let’s create more patterns. Pyramids,and a heartshape.

PYRAMID PATTERN PRINTING

Source: Here

PSEUDOCODE FOR LEFT-SIDED PYRAMID:

  1. Create a ‘for loop’.

i. Use the range() function.

ii. Set a start and endpoint for the shape as desired.

2. Create a nested for loop

i. Use range for the variable defined in the first loop.

ii. print(*). Use ‘end’ argument in print.

iii.Print an empty string

code input
Code output

CODE EXPLANATION

So the logic here is as follows:

  1. The first ‘for’ loop defines a variable(pattern)(you can call it whatever you choose to, i, k, j,p, etc). Then using the range function, a start, and an endpoint for the number of (*) to print was defined(i.e 1 -9).
  2. The nested ‘for’ loop defines a variable(‘stars’), again it can be called whatever, I used patterns and stars here just for ease of understanding.i.e the first loop is where the pattern and the length of the pattern were defined, the second loop is for the stars, i.e what is to be printed in the patterns. You can simply see these as rows and columns and the loops define what is to be in each row and columns. So while the first loop was used to initiate the rows and how many should be printed, the second loop represents the columns, what is to be printed in the columns. Did you notice here that the range function only had one value? Automatically, this is assumed to be the endpoint(the value for pattern), and the default start point is used here(0).
  3. The print statement was defined, stating what is to printed in the shapes, and a parameter(end= “”) was included. What this parameter simply does is that, although the stars are meant to be printed in separate lines and not all in the same line, we want it printed in such a way that a pattern is created. This was used to indicate the presence of an empty string. By default, in the print statement, this parameter returns a new line. However, we clearly defined that for every iteration, the output should all be in the same line. To understand the function of this better, go back to the program and eliminate this parameter, run, and see the difference in output.
code input
Code Output

Did you see that? Definitely not what we wanted right.

UPSIDE DOWN PYRAMID

The Pseudocode is the same as above. The only difference here is adding a reverse step to the range function such that the printed shape is the reverse of what was printed previously. Let’s see how it works:

Code input
code output

Pretty cool!

CODE EXPLANATION

So the code is the same as what was used for the left pyramid above, the difference here is changing the start point(which indicates the top/base of the same), the endpoint(which is always a step behind, hence it was set as 0, meaning stop at 1 here), finally adding a reverse point(-1)- which indicates the subtraction of 1, each time the loop runs, from the start point till it gets to the endpoint. Just as can be seen in the same(10,9,8,7,6……1).

Final shape for the day- ‘ Heart Shape’.

Do you want to show off your coding skills to that special one? or simply someone(a family or friend) you really care about. Let’s see how to print a heart-shaped pattern in Python. You can explore further by printing letters ‘ I’ and ‘ U’ with it if you wish.

HEART SHAPED PATTERN

So for this pattern, we will use the combination of a nested for loop and conditional(if-else) statements.

PSEUDOCODE

  1. Create a for loop to display six lines of rows for stars.
  2. Initiate a second for loop to display seven columns to be used in the heart shape.
  3. Use an “if” condition to determine where stars will be printed. Use logical ‘and’ and ‘or’.
  4. Use an ‘else’ statement to print empty strings where stars will not be printed.
  5. Print an empty line.

Save and run the program.

code input
Code Output

CODE EXPLANATION

So the ideal thing to do here is to draw out the shape in a piece of paper. Coding works with sequence and logic control, to be able to translate the desired output into proper coding language, first, picture the problem, write it out, draw out a plan, understand how the desired results should be, develop a step by step plan for it, then translate into code. This just makes it easier, and most times with fewer bugs.

Here, the first ‘for loop’ defines the length of the shape(in rows), and the second ‘for loop’ defines the length of the columns i.e the width of the shape. Take a look at the code output again, divide the shape into lines/grids. Now take a look at the first line, in reality, we have just 4 stars printed there, the number of columns is 7, so how did that happen? The ‘if’ statement was used to initiate some set of conditions for the top part of the shape(which looks like two separate mini triangles), and the bottom part(which has the shape of a larger triangle), separately. Take a close look at the ‘if’ statement and the conditions placed inside. The first condition says “ if i == 0 and j%3!=0” . Let’s break that down.

Do you remember that the logical operators are used to link two or more conditions together? Recall that the logical ‘and’ operator checks that both conditions are true i.e both conditions must return True for it to be valid. So ‘ i’ here represents the row and this code simply says, where we have the first row(index position 0), and the ‘column modulus division 3’ is not equal to 0(i.e the remainder from dividing the column number by 3 is not equal to zero. So at column index position 0, no star is printed(0%3=0), whereas at index position 1 and 2 respectively a star is printed(1%3=1, 2%3=2). Same for index positions 3 and 6(3%3=0, 6%3=0), index positions 4 and 5 respectively(4%3=1, 5%3=2). , can display stars as a horizontal line of the top two lines (four-star and three-star) of heart shape.
The second condition in the if statement (i==1 and j%3==0), deals with the bottom part of the mini triangles at the top of the shape. So at row 1, and where modulus division between ‘column position and 3 == 0’, a star is printed. Simply the opposite of the first condition.

Finally, the last two conditions are for the bigger upside down triangle which forms the bottom shape ((i-j==2) or (i+j==8)). So where column index position subtracted from the row index position equals 2, the left side of the triangle is printed, and where column index position added to the row index position equals 8, the right side of the triangle is printed. You might want to ask why the mix of the ‘and’ and ‘ or’ logical operators here. So while the ‘and operator’ matches two conditions we want to work simultaneously, the ‘or’ is used in such a way that we have a condition that defines how the shape will be printed for every line/row. The first condition for topmost part of the shape(the sharp edges), the second condition for the bottom part of the first two mini triangles at the top, also defining the sharp steep ‘v-shape’ for the heart, and the last two conditions for the bottom-most part of the shape, which looks like and upperside down triangle.

Great! This brings us to the end of another awesome project. I encourage you to explore further on how to nest loops and create more exciting patterns. Find the link to the repository for the codes used below. The programs can be identified in the repository with titles: “multiplication.py, leftpyramid.py, reversepyramid.py, heart.py”

Thank you for reading!

--

--

Aminah Mardiyyah Rufai

Machine Learning Researcher | Machine Intelligence student at African Institute for Mathematical Sciences and Machine Intelligence | PHD Candidate