Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 10: Advanced Patterns

0 Replies, 689 Views

[Image: 1*RJMxLdTHqVBSijKmOO5MAg.jpeg]

Advanced Patterns:
          Coding an advanced pattern is nothing just combining the basic patterns. If you know how to print an increasing triangle and a decreasing triangle you can do anything with patterns.

Increasing triangle:
*
*  *
*  *  *
*  *  *  *

Decreasing Triangle:
*  *  *  *
*  *  *
*  *
*

You can combine them to make a lot of patterns. So keep in mind that every pattern has a logic so just try to understand the logic behind the code. Printing a pattern always starts from left to right. So let’s jump into coding and code some patterns. So the first pattern we will print is the right sided triangle.

    *
  **
  ***
****

Program:

Code:
# Program to print right sided triangles.
n = int(input("Enter the size of Triangle: "))

for i in range(n):
    for j in range(i,n):
        print(' ', end="")
    for j in range(i + 1):
        print('*', end='')


    print()


If you look at the code. Here we are printing two triangle one is decreasing which is printing spaces and the second is increasing which is printing stars. So that's how we can code our patterns.

Note: in second loop we used condition (i + 1) because default value of i is 0 but in first row we want to print 1 ‘*’ so we use this condition. Now let’s Print the Pyramid Pattern

Program:

Code:
# Program to print Pyramid pattern
n = int(input("Enter the size of Triangle: "))

for i in range(n):
    for j in range(i,n):
        print(' ', end="")
    for j in range(i + 1):
        print('* ', end='')


    print()


Printing hill pattern or pyramid pattern is so easy just print a space after each star just like we did in code. Now let’s code reverse the right sided Triangle..
****
  ***
  **
    *



Code:
# Program to print reverse right sided Triangle
n = int(input("Enter the size of Triangle: "))

for i in range(n):
    for j in range(i + 1):
        print(' ', end="")
    for j in range(i,n):
        print('*', end='')


    print()


And if you want to print code a reverse pyramid so just print a space ‘ ’ after ‘*’ sign

Code:
# Program to print the reverse pyramid
n = int(input("Enter the size of Triangle: "))

for i in range(n):
    for j in range(i + 1):
        print(' ', end="")
    for j in range(i,n):
        print('* ', end='')


    print()


Now let’s understand the hill pattern. In hill patterns we need three basic patterns. One decreasing triangle to print spaces and two increasing triangles to print stars in the second triangle. We set the range to ‘i’ because we don’t want the second loop of stars to print in the first row. Let’s directly jump into code:

Note: try to run the code and set ‘j’ range (i + 1)  in second loop and see what happens

Code:
# Program to print Hill pattern

n = int(input("Enter the size of Triangle: "))

for i in range(n):
    for j in range(i,n):
        print(' ', end="")
    for j in range(i + 1):
        print('*', end='')
    for j in range(i):
        print('*', end='')


    print()


Now let’s print a reverse hill pattern it is the reverse of above code here we use increasing triangle for spaces and two decreasing triangle for ‘*’

Code:
n = int(input("Enter the size of Triangle: "))

for i in range(n):
    for j in range(i+1):
        print(' ', end="")
    for j in range(i,n):
        print('*', end='')
    for j in range(i+1,n):
        print('*', end='')
    print()


Now we will see how we can print a diamond pattern. Diamond pattern is just a combination of hill pattern and reverse hill pattern. But we will still have a issue if we just copy and paste the code of both patterns and run it the issue that we will be having is of the corners we will not get the rounded corners first let’s do a copy paste method and you have to run this code th see the result

Code:
# Program to print Diamond pattern
# Note: This code has a Problem of corners
n = int(input("Enter the size of Triangle: "))

for i in range(n):
    for j in range(i,n):
        print(' ', end="")
    for j in range(i + 1):
        print('*', end='')
    for j in range(i):
        print('*', end='')
    print()
for i in range(n):
    for j in range(i+1):
        print(' ', end="")
    for j in range(i,n):
        print('*', end='')
    for j in range(i+1,n):
        print('*', end='')
    print()


But as I mentioned the problem with this code so now let jump into solution and see why this happens it is because we are saying our first outer loop to print exact same numbers that of second outer loop so we want to decrease the first outer loop in order to get our perfect pattern

Code:
# Program to print Diamond pattern
n = int(input("Enter the size of Triangle: "))

for i in range(n-1):
    for j in range(i,n):
        print(' ', end="")
    for j in range(i + 1):
        print('*', end='')
    for j in range(i):
        print('*', end='')
    print()
for i in range(n):
    for j in range(i+1):
        print(' ', end="")
    for j in range(i,n):
        print('*', end='')
    for j in range(i+1,n):
        print('*', end='')
    print()


    So that’s it for now. Programming needs practice. There are a lot of patterns you will see but once you know the basics you can do anything with these patterns. If you did not run the code and see the result of that code provided in thread you will not understand it so make sure to copy and run the code and try to understand.

Exclamation To do task: Try to print the Butterfly pattern
"Keep Good care of your Health

 Allah Hafiz" and "Good Bye" Till the next post"
"Keep Learning and Keep exploring"
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,227 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,607 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,646 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,826 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,591 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,825 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,775 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,803 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,806 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,800 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)