Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

Python Basic Pattern Questions

The document discusses 13 Python code examples that print out various patterns using numbers and letters. Each example provides the code to generate a specific pattern shape (e.g. triangle, diamond, square) using loops and string formatting methods. The patterns include increasing, decreasing, hollow and alternating shapes printed to the console.

Uploaded by

kanishkaa R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Python Basic Pattern Questions

The document discusses 13 Python code examples that print out various patterns using numbers and letters. Each example provides the code to generate a specific pattern shape (e.g. triangle, diamond, square) using loops and string formatting methods. The patterns include increasing, decreasing, hollow and alternating shapes printed to the console.

Uploaded by

kanishkaa R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Basic Pattern Questions

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:

You might also like