10 More Pattern Programs in Python — Spiral, Star & More

By Mihir Pipermitwala · 5 min read

⚡ TL;DR

10 intermediate and advanced pattern programs in Python — spiral matrix, hollow star, diamond, Fibonacci, binary, wave, and more. Code with output.

A follow-up to our earlier Python pattern collection, this post covers 10 more intermediate and advanced patterns that are commonly asked in interviews. Each includes working code and expected output.


1. Spiral Matrix Pattern

Fill an n x n matrix with numbers in spiral order.

Copy
1  2  3  4
12 13 14 5
11 16 15 6
10 9  8  7
Copy
def spiral_matrix(n):
    matrix = [[0] * n for _ in range(n)]
    num, top, bottom, left, right = 1, 0, n - 1, 0, n - 1
    while top <= bottom and left <= right:
        for i in range(left, right + 1):
            matrix[top][i] = num; num += 1
        top += 1
        for i in range(top, bottom + 1):
            matrix[i][right] = num; num += 1
        right -= 1
        for i in range(right, left - 1, -1):
            matrix[bottom][i] = num; num += 1
        bottom -= 1
        for i in range(bottom, top - 1, -1):
            matrix[i][left] = num; num += 1
        left += 1
    for row in matrix:
        print(" ".join(f"{v:2}" for v in row))

spiral_matrix(4)

Output

Copy
 1  2  3  4
12 13 14  5
11 16 15  6
10  9  8  7

2. Hollow Star Diamond

Print a diamond with only boundary stars.

Copy
  *
 * *
*   *
 * *
  *
Copy
def hollow_diamond(n):
    for i in range(1, n + 1):
        print(" " * (n - i) + "*" + " " * (2 * i - 3) + ("*" if i > 1 else ""))
    for i in range(n - 1, 0, -1):
        print(" " * (n - i) + "*" + " " * (2 * i - 3) + ("*" if i > 1 else ""))

hollow_diamond(3)

Output

Copy
  *
 * *
*   *
 * *
  *

3. Full Diamond Star Pattern

Classic diamond using stars.

Copy
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
Copy
def diamond(n):
    for i in range(1, n + 1):
        print(" " * (n - i) + "*" * (2 * i - 1))
    for i in range(n - 1, 0, -1):
        print(" " * (n - i) + "*" * (2 * i - 1))

diamond(5)

Output

Copy
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

4. Letter Pattern

Print letters in increasing alphabetical order per row.

Copy
A
AB
ABC
ABCD
ABCDE
Copy
def letter_triangle(n):
    for i in range(1, n + 1):
        print(" ".join(chr(65 + j) for j in range(i)))

letter_triangle(5)

Output

Copy
A
A B
A B C
A B C D
A B C D E

5. Number Pattern

Each row uses its row number.

Copy
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Copy
def number_triangle(n):
    for i in range(1, n + 1):
        print(" ".join([str(i)] * i))

number_triangle(5)

Output

Copy
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

6. Fibonacci Triangle

Each row continues the Fibonacci sequence.

Copy
0
1 1
2 3 5
8 13 21 34
55 89 144 233 377
Copy
def fibonacci_triangle(n):
    a, b = 0, 1
    for i in range(1, n + 1):
        row = []
        for _ in range(i):
            row.append(str(a))
            a, b = b, a + b
        print(" ".join(row))

fibonacci_triangle(5)

Output

Copy
0
1 1
2 3 5
8 13 21 34
55 89 144 233 377

7. Binary Triangle

Alternating 1s and 0s.

Copy
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Copy
def binary_triangle(n):
    for i in range(1, n + 1):
        print(" ".join(str((1 + i + j) % 2) for j in range(1, i + 1)))

binary_triangle(5)

Output

Copy
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

8. Wave Pattern

A wave using stars and spaces.

Copy
*       *
 *     *
  *   *
   * *
    *
Copy
def wave(n):
    for i in range(n):
        for j in range(2 * n - 1):
            if i == j or i + j == 2 * n - 2:
                print("*", end="")
            else:
                print(" ", end="")
        print()

wave(5)

Output

Copy
*       *
 *     *
  *   *
   * *
    *

9. Double Hill Pattern

Two mirrored hills.

Copy
*             *
**           **
***         ***
****       ****
*****     *****
******   ******
******* *******
Copy
def double_hill(n):
    for i in range(1, n + 1):
        print("*" * i + " " * (2 * (n - i) + 1) + "*" * i)

double_hill(7)

Output

Copy
*             *
**           **
***         ***
****       ****
*****     *****
******   ******
******* *******

10. Cross/Plus Pattern

A plus shape inside a grid.

Copy
  *  
  *  
*****
  *  
  *  
Copy
def cross(n):
    mid = n // 2
    for i in range(n):
        for j in range(n):
            print("*" if i == mid or j == mid else " ", end="")
        print()

cross(5)

Output

Copy
  *  
  *  
*****
  *  
  *  

Conclusion

These 10 patterns extend the earlier collection, covering matrix algorithms (spiral), Fibonacci, letter grids, and geometric shapes. Try combining two patterns into one program for extra practice.

Happy coding!

Mihir Pipermitwala

Written by Mihir Pipermitwala

Senior Full Stack Consultant & Mobile Engineering Specialist building user-focused applications in healthcare with Flutter, Dart and React Native. I write about what I actually build and test — every tutorial here comes from real project work.

More about Mihir →
Scroll down to load comments...
Related Blogs
Go JSON v2: A Practical Migration Guide to encoding/json/v2

Go JSON v2: A Practical Migration Guide to encoding/json/v2

Migrate a Go REST API to encoding/json/v2 in 7 steps: struct tags, omitempty, unknown fields, error handling, and jsontext streaming, plus a v1 vs v2 comparison.

GOTUTORIALDEVELOPER TOOLS

November 12, 2026

Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlock Flutter's potential with clean code. Learn best practices for writing maintainable Dart code, with examples and essential principles.

CLEAN CODECODE BEST PRACTICESCODE MAINTAINABILITYCODE ORGANIZATIONCODE READABILITY

June 02, 2023

Top 9 Local Databases for Flutter: Full Comparison

Top 9 Local Databases for Flutter: Full Comparison

Compare the best local databases for Flutter (Isar, Hive, Sqflite, ObjectBox, Realm, Drift, Floor, CBL, Sembast). Find pros, cons, and performance comparisons.

CODINGFLUTTERFLUTTER LOCAL DATABASESLEARN TO CODENOSQL

April 06, 2023

Related Tutorials
Essential Git Skills: How to Delete Remote Branches

Essential Git Skills: How to Delete Remote Branches

Keep your repository clean and organized. This guide walks you through how to delete both local and remote branches in Git, step by step.

BITBUCKETDELETE LOCAL BRANCHDELETE REMOTE BRANCHGITGIT BRANCH CLEANUP AUTOMATION

June 12, 2024

Show an Offline Message for No Internet in Flutter

Show an Offline Message for No Internet in Flutter

You have to use the 'Connectivity Flutter Package' to achieve this feature on your App. This package helps to know whether your device is online or offline.

CONNECTIVITY PLUSDEPENDENCIESFLUTTERFLUTTER DEVELOPMENTFLUTTER PACKAGES

April 09, 2024

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Implement TabBar and TabBarView in Flutter step by step — customize tab indicators, enable scrollable tabs, and change tabs programmatically.

CODINGDEFAULT TAB CONTROLLERFLUTTERLEARN TO CODETAB CONTROLLER

July 26, 2023

File Manipulation in Flutter: Best Practices & Examples

File Manipulation in Flutter: Best Practices & Examples

Master Flutter file manipulation — permission management, directory handling, and practical read-write scenarios to elevate your app's file handling.

CODINGFLUTTERFLUTTER DEVELOPMENTFLUTTER FILE OPERATIONFLUTTER PATH PROVIDER

June 30, 2023

Related Recommended Services
Visual Studio Code for the Web

Visual Studio Code for the Web

Build with Visual Studio Code, anywhere, anytime, in your browser.

IDEVISUAL STUDIOVISUAL STUDIO CODEWEB
Renovate | Automated Dependency Updates

Renovate | Automated Dependency Updates

Renovate Bot keeps source code dependencies up-to-date using automated Pull Requests.

AUTOMATED DEPENDENCY UPDATESBUNDLERCOMPOSERGITHUBGO MODULES
Best XML Formatter and XML Beautifier

Best XML Formatter and XML Beautifier

Online XML Formatter will format xml data, helps to validate, and works as XML Converter. Save and Share XML.

XMLXML BEAUTIFIERXML CONVERTERXML FORMATXML FORMATTER
Kubecost | Kubernetes cost monitoring and management

Kubecost | Kubernetes cost monitoring and management

Kubecost started in early 2019 as an open-source tool to give developers visibility into Kubernetes spend. We maintain a deep commitment to building and supporting dedicated solutions for the open source community.

CLOUDKUBECOSTKUBERNETESOPEN SOURCESELF HOSTED
Related Recommended Stories
How GitHub reduced testing time for iOS apps with new runner features

How GitHub reduced testing time for iOS apps with new runner features

Learn how GitHub used macOS and Apple Silicon runners for GitHub Actions to build, test, and deploy our iOS app faster.

IOSGITHUBTESTINGRUNNER
5 ways to transform your workflow using GitHub Copilot and MCP

5 ways to transform your workflow using GitHub Copilot and MCP

Learn how to streamline your development workflow with five different MCP use cases.

AGENT MODECODING AGENTCOPILOTFIGMAGITHUB
One weird trick for powerful Git aliases

One weird trick for powerful Git aliases

Advanced Git Aliases

ALIASALIAS TEMPLATEATLASSIANBITBUCKETGIT
Awesome Python

Awesome Python

An opinionated list of awesome Python frameworks, libraries, software and resources

AWESOMEAWESOME PYTHONCOLLECTIONSGITHUBPYTHON
Related Recommended Tools
Find out what websites are built with - Wappalyzer

Find out what websites are built with - Wappalyzer

Find out the technology stack of any website. Create lists of websites and contacts by the technologies they use.

ADD ONSANALYTICSAPP STOREAPPLEBOOKING
Sourcetree | Free Git GUI for Mac and Windows

Sourcetree | Free Git GUI for Mac and Windows

A Git GUI that offers a visual representation of your repositories. Sourcetree is a free Git client for Windows and Mac.

GITGITHUBGITLABATLASSIANBITBUCKET
Related Recommended Videos