GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python Program to Print Diamond Pattern
Python Program to Print Diamond PatternThis article is created to cover one of the main program of pattern printing in Python, that is diamond pattern in different-different ways. Here are the list of programs covered in this article:
Print Diamond Pattern of Stars (*)The question is, write a Python program to print diamond pattern using * (stars). The program given below is answer to this one of the famous question: rowNum = 5 space = rowNum-1 for i in range(1, rowNum+1): for j in range(1, space+1): print(end=" ") space = space-1 for j in range(2*i-1): print(end="*") print() space = 1 for i in range(1, rowNum): for j in range(1, space+1): print(end=" ") space = space+1 for j in range(1, 2*(rowNum-i)): print(end="*") print() Here is its sample output: Print Diamond Pattern of * upto n RowsThis is the modified version of previous program. This program allows user to enter the number of rows or lines to print diamond pattern that expands upto given lines like shown in the program and its sample run given here: print(end="Enter Row Size (Total Lines): ") rowSize = int(input()) if rowSize%2==0: halfDiamRow = int(rowSize/2) else: halfDiamRow = int(rowSize/2)+1 space = halfDiamRow-1 for i in range(1, halfDiamRow+1): for j in range(1, space+1): print(end=" ") space = space-1 for j in range(2*i-1): print(end="*") print() space = 1 for i in range(1, halfDiamRow): for j in range(1, space+1): print(end=" ") space = space+1 for j in range(1, 2*(halfDiamRow-i)): print(end="*") print() Here is its sample run with user input, 9. Since 9 is the number entered by user. Then the output will be a diamond pattern of 9 lines or rows like shown in the snapshot of sample output given below: Print Diamond Pattern of NumbersThis program is similar to previous program. The only difference is, this program uses natural numbers to print diamond pattern upto given rows: print(end="Enter Row Size (Total Lines): ") rowSize = int(input()) if rowSize%2==0: halfDiamRow = int(rowSize/2) else: halfDiamRow = int(rowSize/2)+1 space = halfDiamRow-1 for i in range(1, halfDiamRow+1): for j in range(1, space+1): print(end=" ") space = space-1 num = 1 for j in range(2*i-1): print(end=str(num)) num = num+1 print() space = 1 for i in range(1, halfDiamRow): for j in range(1, space+1): print(end=" ") space = space+1 num = 1 for j in range(1, 2*(halfDiamRow-i)): print(end=str(num)) num = num+1 print() Here is its sample run with same user input (9) as of previous program: Print Diamond Pattern of Alphabet CharactersThis program also prints the diamond pattern, but using alphabet characters that are A, B, C, D, etc. We've used ASCII value of first alphabet character. That is the ASCII value of A is 65. And later we've used chr() that converts ASCII value to its respective character. Let's have a look at the program and its sample run for clear understanding about all the things going on: print(end="Enter Row Size (Total Lines): ") rowSize = int(input()) if rowSize%2==0: halfDiamRow = int(rowSize/2) else: halfDiamRow = int(rowSize/2)+1 space = halfDiamRow-1 for i in range(1, halfDiamRow+1): for j in range(1, space+1): print(end=" ") space = space-1 ascVal = 65 for j in range(2*i-1): ch = chr(ascVal) print(end=ch) ascVal = ascVal+1 print() space = 1 for i in range(1, halfDiamRow): for j in range(1, space+1): print(end=" ") space = space+1 ascVal = 65 for j in range(1, 2*(halfDiamRow-i)): ch = chr(ascVal) print(end=ch) ascVal = ascVal+1 print() Here is its sample run with again same user input, that is 9 as of previous two programs: Print Diamond Pattern of Given CharacterThis is the last program of diamond pattern printing program. This program prints diamond pattern using any particular character, entered by user at run-time: print(end="Enter Row Size (Total Lines): ") rowSize = int(input()) if rowSize%2==0: halfDiamRow = int(rowSize/2) else: halfDiamRow = int(rowSize/2)+1 print(end="Enter a Character: ") ch = input() chLen = len(ch) if chLen==1: space = halfDiamRow-1 for i in range(1, halfDiamRow+1): for j in range(1, space+1): print(end=" ") space = space-1 for j in range(2*i-1): print(end=ch) print() space = 1 for i in range(1, halfDiamRow): for j in range(1, space+1): print(end=" ") space = space+1 for j in range(1, 2*(halfDiamRow-i)): print(end=ch) print() else: print("\nOnly single character is allowed!") Here is its sample run with user input, 9 as row size and + as character: « Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |