The document provides Python code to print various patterns using numbers, letters, and asterisks. There are 13 code examples that print patterns in shapes like increasing order, right triangles, pyramids, diamonds, hollow squares, and staircases. The patterns are printed by manipulating variables that control spacing, repetition, and character selection in print statements using loops and string formatting.
The document provides Python code to print various patterns using numbers, letters, and asterisks. There are 13 code examples that print patterns in shapes like increasing order, right triangles, pyramids, diamonds, hollow squares, and staircases. The patterns are printed by manipulating variables that control spacing, repetition, and character selection in print statements using loops and string formatting.
The document provides Python code to print various patterns using numbers, letters, and asterisks. There are 13 code examples that print patterns in shapes like increasing order, right triangles, pyramids, diamonds, hollow squares, and staircases. The patterns are printed by manipulating variables that control spacing, repetition, and character selection in print statements using loops and string formatting.
1. Print a pattern of asterisks in increasing order.
n=5 for i in range(1, n+1): print('*' * i) output: 2. Print a pattern of numbers in a right triangle n=5 for i in range(1, n+1): print(str(i) * i) output: 3. Print a pattern of numbers in a pyramid shape. n=5 for i in range(1, n+1): print(' ' * (n-i) + str(i) * (2*i-1)) output: 4.Print a pattern of alphabets in increasing order. n=5 start_char = ord('A') for i in range(n): print((chr(start_char + i) + ' ') * (i+1)) output: 5. Pattern: Print a pattern of alphabets in a reverse right triangle. n=5 start_char = ord('A') for i in range(n, 0, -1): print((chr(start_char) + ' ') * i) start_char += 1 output: 6. Print a pattern of alphabets in a diamond shape. n=5 start_char = ord('A') for i in range(1, n+1): print(' ' * (n-i) + (chr(start_char) + ' ') * i) start_char += 1 for i in range(n-1, 0, -1): print(' ' * (n-i) + (chr(start_char) + ' ') * i) start_char += 1 output : 7.Print a pattern of numbers in a staircase shape. n=5 for i in range(1, n+1): print(' ' * (n-i) + str(i) + ' ' * (i-1)) output: 8. Print a pattern of numbers in a diamond shape. n=5 for i in range(1, n+1): print(' ' * (n-i) + (str(i) + ' ') * i) for i in range(n-1, 0, -1): print(' ' * (n-i) + (str(i) + ' ') * i) output: 9. Print a pattern of a square with alternating characters. n=5 for i in range(n): if i == 0 or i == n-1: print('*' * n) else: print('*' + ' ' * (n-2) + '*') output: 10. Print a pattern of a hollow square. n=5 for i in range(n): if i == 0 or i == n-1: print('*' * n) else: print('*' + ' ' * (n-2) + '*') output: 11. Print a pattern of numbers in a diamond shape: n=5 for i in range(1, n+1): print(' ' * (n-i) + ' '.join(str(j) for j in range(i, 2*i))) for i in range(n-1, 0, -1): print(' ' * (n-i) + ' '.join(str(j) for j in range(i, 2*i))) output : 12. print a pattern of alphabets in a hollow square: n=5 for i in range(n): if i == 0 or i == n-1: print(' '.join(chr(ord('a')+j) for j in range(n))) else: print(chr(ord('a')+i) + ' ' * (2*(n-2)) + chr(ord('a')+i)) output: 13. print a pattern of alphabets in a diamond shape n=5 start_char = ord('a') for i in range(1, n+1): print(' ' * (n-i) + ' '.join(chr(start_char + j) for j in range(i))) start_char += i for i in range(n-1, 0, -1): start_char -= i print(' ' * (n-i) + ' '.join(chr(start_char + j) for j in range(i))) output: