Python — Practical Programs 100+ Part2

Key computer Education
7 min readMar 23, 2023

--

Pyramids and Patterns Programs

Program to Print Square Star Pattern using For Loop

n = int(input('Enter the number of rows: '))

# nested For Loop to print star
for i in range(n):
for j in range(n):
# printing stars
print("* ",end="")
print("\r")
Enter the number of rows: 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Program to create Left Half Pyramid Pattern Using Asterisk (*)

r=int(input('Enter number of rows: '))
for i in range(r):
for j in range(i+1):
print('* ', end='')
print(' ')
Enter number of rows: 5
*
* *
* * *
* * * *
* * * * *

Program to Create Half Pyramid Pattern Using Numeric Values

r=int(input('Enter number of rows: '))
for i in range(r):
for j in range(i+1):
print(j+1, end=' ')
print(' ')
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Program to Create Half Pyramid Pattern Using Alphabets

r=int(input('Enter number of rows: '))
asciiValue = 65
for i in range(r):
for j in range(i + 1):
alpha = chr(asciiValue)
print(alpha, end=' ')
asciiValue += 1
print(' ')
Enter number of rows: 5
A
B B
C C C
D D D D
E E E E E

Program to Create Inverted Half Pyramid Pattern Using Asterisk

r=int(input('Enter number of rows: '))

for i in range(r, 0, -1):
for j in range(0, i):
print('*', end=' ')

print(' ')
Enter number of rows: 5
* * * * *
* * * *
* * *
* *
*

Program to Right Half Pyramid Star Pattern in Python

n = int(input('Enter the number of rows: '))
for i in range(n):
for j in range(n-i-1):
# printing spaces
print(" ", end=" ")

for j in range(i+1):
# printing stars
print("* ",end="")
print()
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *

Program to Create Full Pyramid Pattern Using Asterisk

r=int(input('Enter number of rows: '))
x = 0
for i in range(1, r + 1):
for space in range(1, (r - i) + 1):
print(end=' ')
while x != (2 * i - 1):
print('*', end='')
x += 1
x = 0
print()
Enter number of rows: 5
*
***
*****
*******
*********
n = int(input('Enter the number of rows: '))
a = n - 1
for i in range(n):
for j in range(a):
print(end=" ")

# decrementing a after each loop
a = a - 1
for j in range(i+1):
# printing stars
print("* ",end="")
print("\r")
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *

Full Inverted Pyramid Pattern

rows =int(input('Enter number of rows: '))  
for i in range(rows+1, -1, -1):
for space in range(0, rows-i+1):
print(' ', end='')
for j in range(i, 2*i-1):
print('* ', end='')
for j in range(1, i-1):
print('* ', end='')
print()
Enter number of rows: 5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

Reverse Right Half Pyramid Star Pattern in Python

n = int(input('Enter the number of rows: '))
for i in range(n):
for j in range(i+1):
# printing spaces
print(" ",end=" ")

for k in range(n-i):
# printing stars
print("* ",end="")
print()
Enter the number of rows: 5
* * * * *
* * * *
* * *
* *
*

Scalene Triangle Star Pattern

n = int(input('Enter the number of rows: '))
a = n - 1
for i in range(n):
for j in range(a):
print(end=" ")

# decrementing a after each loop
a = a - 1
for j in range(i+1):
# printing stars
print("* ",end=" ")
print("\r")
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *

Diamond Star Pattern in Python

n = int(input('Enter the number of rows: '))
for i in range(n):
for j in range(n-i-1):
print(" ", end="")
for j in range(2*i+1):
# printing stars
print("*", end="")
print()

# print downward pyramid
for i in range(n-1):
for j in range(i+1):
print(" ", end="")
for j in range(2*(n-i-1)-1):
# printing stars
print("*", end="")
print()
Enter the number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*

Hourglass Star Pattern

n = int(input('Enter the number of rows: '))
for i in range(n-1):
for j in range(i):
print(" ", end=" ")
for j in range(2*(n-i)-1):
# printing stars
print("*", end=" ")
print()

# print upper pyramid
for i in range(n):
for j in range(n-i-1):
print(" ", end=" ")
for j in range(2*i+1):
# printing stars
print("*", end=" ")
print()
Enter the number of rows: 5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

Heart Star Pattern

n = int(input('Enter the number of rows: '))
for i in range(n//2, n, 2):
for j in range(1, n-i ,2):
print(" ", end=" ")
for j in range(1, i+1, 1):
print("*", end=" ")
for j in range(1, n-i+1, 1):
print(" ", end=" ")
for j in range(1, i+1, 1):
print("*", end=" ")
print()

# print upper side of heart
for i in range(n,0,-1):
for j in range(i, n, 1):
print(" ", end=" ")
for j in range(1, i*2, 1):
print("*", end=" ")
print()
Enter the number of rows: 5
* * * *
* * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

Hollow Square Star Pattern

n = int(input('Enter the number of rows: '))
for i in range(n):
for j in range(n):
# printing stars
if i == 0 or i == n-1 or j == 0 or j == n-1:
print("*", end=" ")
else:
print(" ", end=" ")
print("\r")
Enter the number of rows: 4
* * * *
* *
* *
* * * *

Hollow Left Half Pyramid Star Pattern

n = int(input('Enter the number of rows: '))
for i in range(1, n+1):
for j in range(i):
# printing stars
if j == 0 or j == i-1:
print("*", end=" ")
else:
if i != n:
print(" ", end=" ")
else:
print("*", end=" ")
print()
Enter the number of rows: 5
*
* *
* *
* *
* * * * *

Hollow Reverse Left Half Pyramid Star Pattern

n = int(input('Enter the number of rows: '))
for i in range(n, 0, -1):
for j in range(i, 0, -1):
if i == 1 or i == n or j == 1 or j == i:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()
Enter the number of rows: 5
* * * * *
* *
* *
* *
*

Hollow Triangle or Pyramid Star Pattern

n = int(input('Enter the number of rows: '))
for i in range(n):
for j in range(n-i-1):
print(" ", end=" ")

for j in range(2*i+1):
# printing stars
if j == 0 or j == 2*i:
print("*", end=" ")
else:
if i == n-1:
print("*", end=" ")
else:
print(" ", end=" ")
print()
Enter the number of rows: 5
*
* *
* *
* *
* * * * * * * * *

Hollow Reverse Triangle or Pyramid Star Pattern

n = int(input('Enter the number of rows: '))
for i in range(1, n+1):
for j in range(0, i):
print(" ", end=" ")

for j in range(1, (n*2 - (2*i-1)) + 1):
if i == 1 or j == 1 or j ==(n*2 -(2*i-1)):
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()
Enter the number of rows: 5
* * * * * * * * *
* *
* *
* *
*

Hollow Diamond Star Pattern

n = int(input('Enter the number of rows: '))

for i in range(n):
for j in range(n-i-1):
print(" ", end=" ")
for j in range(2*i+1):
if j == 0 or j == 2*i:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()

for i in range(n-1):
for j in range(i+1):
print(" ", end=" ")
for j in range(2*(n-i-1)-1):
if j == 0 or j == 2*(n-i-1)-2:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()
Enter the number of rows: 5
*
* *
* *
* *
* *
* *
* *
* *
*

Hollow Hourglass Star Pattern

n = int(input('Enter the number of rows: '))
for i in range(n, 0, -1):
for j in range(n-i):
print(" ", end=" ")
for j in range(1, 2*i):
if i==1 or i==n or j==1 or j==2*i-1:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()

for i in range(2, n+1):
for j in range(n-i):
print(" ", end=" ")
for j in range(1, 2*i):
if i==n or j==1 or j==2*i-1:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()
Enter the number of rows: 5
* * * * * * * * *
* *
* *
* *
*
* *
* *
* *
* * * * * * * * *

Pascal’s Triangle

rows =int(input('Enter number of rows: '))
coef = 1
for i in range(1,rows+1):
for space in range(1, rows-i+1):
print(' ',end='')
for j in range(0, i):
if j==0 or i==0:
coef = 1
else:
coef = coef * (i - j)//j
print(coef, end = ' ')
print()
Enter number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Floyd’s Triangle

r=int(input('Enter number of rows: '))  
num = 1
for i in range(1, r+1):
for j in range(1, i+1):
print(num, end=" ")
num += 1
print()
Enter number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

--

--

Key computer Education

keykoders.wordpress.com Opens up the Mind Programming for Beginner | Coding Tutorial | Free Programs | Free Projects