Simple ways to write Complex Patterns in Python in just 4mins.

Gowtham S R
6 min readJul 1, 2022

--

image by author

Pattern programs are the most important interview questions, but sometimes we struggle to write complex pattern programs, but after finishing this article you will feel more comfortable solving these patterns.

I will show you how to write complex pattern programs with just two basic patterns. If you know how the for-loop works, then it will be straightforward to write any complex pattern with these basic patterns.

Basics:

Print a star:

Print a star 5 times:

Print a star 5 times vertically using for loop:

Print 5 stars horizontally using for loop:

Now using a nested loop try to print a square:

Ohh no…we got all the stars in a single line, so what to do now?? after completing the inner loop move to the following line,

Now we finally got a square pattern, we can change * to any character. Note that the outer loop is written to print rows and the inner loop is written to print the columns.

Creating complex patterns:

Any complex pattern can be created by using 2 simple basic patterns known as increasing triangle and decreasing triangle as shown below.

Image by author
Image by author
Image by author

Now modify the above triangle patterns to form complex patterns given below.

1. Print a triangle as given below.

A simple way is to visualize the above pattern as decreasing triangle of spaces and an increasing triangle of stars. Combine both patterns to get the required pattern as shown in the below image.

Image by author

2. Print the below pattern

A simple way is to visualize the above pattern as an increasing triangle of spaces and decreasing triangle of stars and combine both patterns to get the required pattern as shown below.

Image by author

Hill pattern:

Image by author

Reverse Hill pattern:

Image by author

Double Hill pattern:

Image by author

Diamond pattern:

Image by author

So now you understood how easily we can form any type of complex pattern with just 2 simple patterns. Similarly, with the same logic, we can print any characters.

Number patterns:

Just replace ‘*’ with numbers to print the number patterns, and you will find out how easily you can write the below patterns.

Image by author

Increasing numbers:

Decreasing numbers:

Create the below patterns:

Character patterns:

To write the character patterns just use the ASCII values. For eg ASCII value of A is 65 and that of Z is 90. The ASCII value of lowercase a is 97 and that of lowercase z is 122.

So try to solve the below patterns.

Thank you…..

--

--