Python

15 Python Pattern Programs for CBSE Class 11 and 12, Star, Number, Alphabet

15 Python pattern programs with code and output for CBSE Class 11 and 12. Star, number, and alphabet patterns for board exams and practicals.

Pattern programs are a favourite in CBSE board exams and practical assessments. They test your understanding of nested loops, which is a core topic in both Class 11 and Class 12 Computer Science.

This post covers 15 patterns, stars, numbers, and alphabets, with complete code and output. Practice all of them before your exam.


Pattern 1: Right Triangle (Stars)

n = 5
for i in range(1, n + 1):
    print('* ' * i)
*
* *
* * *
* * * *
* * * * *

Pattern 2: Inverted Right Triangle (Stars)

n = 5
for i in range(n, 0, -1):
    print('* ' * i)
* * * * *
* * * *
* * *
* *
*

Pattern 3: Pyramid (Centered Stars)

n = 5
for i in range(1, n + 1):
    print(' ' * (n - i) + '* ' * i)
    *
   * *
  * * *
 * * * *
* * * * *

Pattern 4: Inverted Pyramid

n = 5
for i in range(n, 0, -1):
    print(' ' * (n - i) + '* ' * i)
* * * * *
 * * * *
  * * *
   * *
    *

Pattern 5: Diamond

The diamond is a combination of a pyramid and an inverted pyramid. This is a common 5-mark question in board exams.

n = 5
# Upper half
for i in range(1, n + 1):
    print(' ' * (n - i) + '* ' * i)
# Lower half
for i in range(n - 1, 0, -1):
    print(' ' * (n - i) + '* ' * i)
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *

Pattern 6: Number Right Triangle

n = 5
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end=' ')
    print()
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pattern 7: Repeated Number Triangle

n = 5
for i in range(1, n + 1):
    print((str(i) + ' ') * i)
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Pattern 8: Floyd's Triangle

Floyd's Triangle prints consecutive natural numbers in a triangular form. This is frequently asked in CBSE practicals.

n = 5
num = 1
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(num, end=' ')
        num += 1
    print()
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Pattern 9: Pascal's Triangle

Pascal's Triangle is important for both maths and computer science. Each number is the sum of the two numbers above it.

n = 6
for i in range(n):
    val = 1
    row = ''
    for j in range(i + 1):
        row += str(val) + ' '
        val = val * (i - j) // (j + 1)
    print(' ' * (n - i) + row)
      1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1
 1 5 10 10 5 1

Pattern 10: Alphabet Right Triangle

n = 5
for i in range(1, n + 1):
    for j in range(i):
        print(chr(65 + j), end=' ')
    print()
A
A B
A B C
A B C D
A B C D E

Note: chr(65) gives 'A', chr(66) gives 'B', and so on. The chr() and ord() functions are important for the board exam.


Pattern 11: Same Letter Repeated Triangle

n = 5
for i in range(n):
    ch = chr(65 + i)
    print((ch + ' ') * (i + 1))
A
B B
C C C
D D D D
E E E E E

Pattern 12: Hollow Square

n = 5
for i in range(n):
    for j in range(n):
        if i == 0 or i == n - 1 or j == 0 or j == n - 1:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()
* * * * *
*       *
*       *
*       *
* * * * *

Pattern 13: Checkerboard Pattern

n = 5
for i in range(n):
    for j in range(n):
        if (i + j) % 2 == 0:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()
*   *   *
  *   *
*   *   *
  *   *
*   *   *

Pattern 14: Right-Aligned Number Triangle

n = 5
for i in range(1, n + 1):
    print(' ' * (n - i) * 2, end='')
    for j in range(1, i + 1):
        print(j, end=' ')
    print()
        1
      1 2
    1 2 3
  1 2 3 4
1 2 3 4 5

Pattern 15: Butterfly Pattern

This is a challenging pattern that combines spaces and stars. It is good for 5-mark questions.

n = 5
# Upper half
for i in range(1, n + 1):
    print('* ' * i + '  ' * (n - i) * 2 + '* ' * i)
# Lower half
for i in range(n, 0, -1):
    print('* ' * i + '  ' * (n - i) * 2 + '* ' * i)
*                 *
* *             * *
* * *         * * *
* * * *     * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * *     * * * *
* * *         * * *
* *             * *
*                 *

Board Exam Tips for Pattern Programs

  1. Always use nested loops. The outer loop controls the row, the inner loop controls the column. This is the standard approach examiners expect.

  2. Identify the pattern first. Before writing code, note how many characters appear in each row and how spaces change. Write it out on paper.

  3. Use end=' ' in print. This keeps output on the same line. Use a bare print() to move to the next line. This is a must-know syntax.

  4. Know chr() and ord(). For alphabet patterns, chr(65) gives 'A' and ord('A') gives 65. These functions are part of the syllabus.

  5. Comment your code. In board exams and practicals, writing a brief comment for each loop helps earn full marks.

  6. Test with small values. Run your code with n = 3 first to check correctness, then scale up.

  7. Common mistakes to avoid:

    • Forgetting print() for a new line after the inner loop
    • Off-by-one errors in range(), remember range(1, n+1) gives 1 to n
    • Incorrect number of spaces in centred patterns

Practice these 15 patterns and you will be ready for any pattern question in your CBSE board exam or practical assessment.

Want to learn more?

Explore free chapter-wise notes with quizzes and code playground

Prefer watching over reading?

Subscribe for free.

Subscribe on YouTube