How to Create a Diamond Pattern Using Loops in Python


In this example, we will learn the method of printing out a diamond pattern of star symbol (*) with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from sys import stdout

for i in range(4):
    for j in range(2 - i + 1):
        stdout.write(' ')
    for k in range(2 * i + 1):
        stdout.write('*')
    print()

for i in range(3):
    for j in range(i + 1):
        stdout.write(' ')
    for k in range(4 - 2 * i + 1):
        stdout.write('*')
    print()

Output:

   *
  ***
 *****
*******
 *****
  ***
   *
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments