Diamond Pattern in Python: Print Using Stars

Diamond Pattern in Python: Print Using Stars

How to Print Two Different Diamond Patterns Using Stars in Python

In this article, we’ll explore how to print two unique diamond patterns using stars (*) in Python. Patterns like these are a great way to improve your understanding of loops and control structures. Whether you’re preparing for coding interviews or just want to refine your programming skills, creating such patterns will help you become more proficient in Python.

Understanding Diamond Patterns

A diamond pattern typically consists of two halves: the top half and the bottom half. The stars are arranged in a symmetric fashion. In this article, we’ll focus on two types of diamond patterns:
  1. Full Diamond Pattern
  2. Right Half-Diamond Pattern
Let’s walk through how to print both patterns using Python.

1. Full Diamond Pattern Using Stars

Diamond Pattern in Python: Print Using Stars

Concept of Full Diamond Pattern

A full diamond pattern consists of stars that grow wider until reaching the center, then shrink symmetrically. The number of rows defines the size of the diamond, with the largest row being at the center.

Python Code for Full Diamond Pattern

python
# Python program to print full diamond pattern using stars# Step 1: Input the number of rows for the top half of the diamond rows = int(input(“Enter the number of rows: “))# Top half of the diamond k = 0 for i in range(1, rows + 1): # Print spaces before stars for j in range(1, (rows – i) + 1): print(end=” “)# Print stars while k != (2 * i – 1): print(“*”, end=“”) k = k + 1 k = 0# New line after each row print()# Bottom half of the diamond k = 2 m = 1 for i in range(1, rows): # Print spaces before stars for j in range(1, k): print(end=” “) k = k + 1# Print stars while m <= (2 * (rows – i) – 1): print(“*”, end=“”) m = m + 1 m = 1# New line after each row print()

Sample Input and Output

Input:
mathematica
Enter the number of rows: 5
Output:
markdown
* *** ***** ******* ********* ******* ***** *** *

How the Code Works:

  1. Top Half: The top half starts with one star and increases the number of stars per row, with spaces to align them in the center.
  2. Bottom Half: The bottom half mirrors the top, starting with a wide row and decreasing the number of stars.

2. Right Half-Diamond Pattern Using Stars

Concept of Right Half-Diamond Pattern

In a right half-diamond pattern, stars start from one at the top left and increase in each subsequent row. The right half is shaped similarly to a right-angled triangle, and the lower half mirrors the upper half in reverse order.

Python Code for Right Half-Diamond Pattern

python
# Python program to print right half diamond pattern using stars# Input the number of rows rows = int(input(“Enter the number of rows: “))# Top half of the diamond (right-angled triangle) for i in range(0, rows): for j in range(0, i + 1): print(“*”, end=‘ ‘) print()# Bottom half of the diamond (inverted right-angled triangle) for i in range(rows, 0, –1): for j in range(0, i – 1): print(“*”, end=‘ ‘) print()

Sample Input and Output

Input:
mathematica
Enter the number of rows: 4
Output:
markdown
* * * * * * * * * * * * * * * *

How the Code Works:

  1. Top Half: This part prints a growing right-angled triangle.
  2. Bottom Half: This part prints an inverted right-angled triangle, mirroring the top half.

Why Print Diamond Patterns in Python?

  • Algorithm Practice: Writing programs to print patterns like diamonds allows you to practice using loops, control statements, and understanding problem-solving steps.
  • Programming Interviews: Diamond and other pattern problems are commonly asked in coding interviews to test logical thinking, problem-solving skills, and mastery over basic programming concepts.

Conclusion

In this article, we discussed how to print two different diamond patterns using stars in Python: the full diamond pattern and the right half-diamond pattern. These types of patterns are not only fun to create but also reinforce your understanding of loops and control structures in Python. Practice writing these patterns to improve your coding skills and get better at problem-solving.By following the steps outlined in the article, you can easily create these patterns for any number of rows. Keep practicing, and soon you’ll be able to create even more complex shapes using loops and logic.Click Here to know more our program:Diamond Pattern in Python: Print Using Stars
c