Python Program To Generate Diamond Pattern

Hey there! Trying your hands on Python programming? Here's a simple program to illustrate the concept of loops in python. The resulting diamond can be generated with slightly different versions of the program. Also you can alter the shape and size of the diamond according to your wish. You can do so by adding spaces in the #'s to make it more cleaner or increase or decrease the range of the loops to make the diamond shape bigger or smaller.
But the central idea will remain the same. Here's the code -

def run():
    j = 7
    k = 7
    p = 1
    for i in range(8):
        print " " * k," #" *  i
        k -=1
    while j > 1:
        j -= 1
        print " " * p," #" * j
        p +=1
run()



You can copy and paste this code into your code editor and run it. Python supports three kinds of loops namely for, while and if. In this program i have used for and while. Python doesn't includes periods or semicolons to indicate the end of the line. Also, there are no braces to initiate or end the loops. Python uses intends to indicate the beginning and end of the loop. So, unlike other programming languages like C, C++ or Java intends are important. You can use TAB to shift the code or use 2 spaces which is the standard for coding at Google. If you are using a code editor like Eclipse as i do, it will take care of the intends for you. You can look at the coding and output snapshots made in Python using Eclipse IDE below-

Code for Generating Diamond Pattern in Python Programming
Coding - Generate Diamond Pattern

Output of the Diamond Pattern Generating Program in Python
Output - Diamond Pattern using Python Code

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...