Pattern Program in Python
Star Pattern Program:
You
learn different types of pattern here, first we understand the concept of star
pattern.
Example:
N =
5
Print (‘*’ * n)
Output: *
* * * *
Note:
here ‘*’ are printing in one row in horizontal. If we run loop here than all-star
print in a row in vertically Because the loop moves to a new line every time:
Example
N =
5
For j in range (n):
Print (‘*’)
Output: *
*
*
*
*
Now using Nested loop for
pattern printing:
Example:
N =
5
For i in range (n):
For j in range (n):
Print (‘*’)
Output: *
*
*
*
*
*
*
*
*
*
Continue ………..
Here the loop prints the
pattern on a new line each time. If we want to take our accordion new line then
we do "(end=’ ‘)" statement for that
यहाँ लूप हर बार एक नई लाइन में पैटर्न
प्रिंट करवाता है यदि हम अपने अकॉर्डिंग नई लाइन लेना
चाहे तो उसके लिए हम "( end=’ ‘)एन्ड" स्टेटमेंट उसे करते हैं
Example:
N =
5
For i in range (n):
For j in range (n):
Print (‘*’, end=’ ‘)
Output: * * * * * * * * * * * * * * *
When we apply "( end=’ ‘)"
statement the loop runs horizontally in a single line. When the loop runs again
and it is printed on a new line, we will do "print()".
जब हम "( end=’ ‘)"
स्टेटेमेंट लगते हैं तो लूप एक ही लाइन में हॉरिजॉन्टल चलता है. जब लूप दुबारा चले और वो नई लाइन में प्रिंट हो तो हम " print()"
करवाएंगे.
Example:
N =
5
For i in range (n):
For j in range (n):
Print (‘*’, end=’ ‘)
Print ()
Output: * * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Print a triangle pattern
Example:
N =
5
For i in range (n):
For j in range (j+1):
Print (‘*’, end=’ ‘)
Print ()
Output: *
* *
* * *
* * * *
* * * * *
Print a reverse triangle pattern
Example:
N =
5
For i in range (n):
For j in range (i,n):
Print (‘*’, end=’ ‘)
Print ()
Output: * * * * *
* * * *
* * *
* *
*
Example
i = 1
while i <= 5:
j = 1
while j <= i:
print (‘*’, end=’ ‘))
j += 1
print
()
i += 1
Output: *
**
***
****
*****
Example
i = 1
while i <= 5:
j = 1
while j <= i:
print (i, end=’ ‘))
j += 1
print
()
i += 1
Output: 1
22
333
4444
55555
Example
i = 1
while i <= 5:
j = 1
while j <= i:
print (j, end=’ ‘))
j += 1
print
()
i += 1
Output: 1
12
123
1234
12345
Star Pattern Programs In Python
1.
Write a python program to print following star pattern:
* * *
* *
* * * *
*
* * * *
*
* * * *
*
* * * *
*
Code:
n=5
for i in range(n):
for j in range(n):
print('*', end=' ')
print()
2.
Write a python program to print following pattern:
*
* *
* * *
* * * *
* * * *
*
Code:
n
= 5
for i in range(n):
for j in range(i+1):
print('*', end=' ')
print()
3.
Write a python code to print following star pattern:
* * *
* *
* * * *
* * *
* *
*
Code:
n
= 5
for i in range(n):
for j in range(i, n):
print('*', end=' ')
print()
4.
Write a python code to print following pattern:
*
* *
*
* *
* * *
*
* * * *
*
Code:
n
= 5
for i in range(n):
for j in range(i, n):
print(' ', end=' ')
for j in range(i+1):
print('*', end=' ')
print()
5.
Write a python program to print following star pattern:
* * *
* *
* * *
*
*
* *
* *
*
Code:
n
= 5
for i in range(n):
for j in range(i+1):
print(' ', end=' ')
for j in range(i, n):
print('*', end=' ')
print()
6.
Write a python program to print following hill star pattern:
*
* * *
*
* * * *
* * *
* * *
*
* * * *
* * *
* *
Code:
n
= 5
for i in range(n):
for j in range(i, n):
print(' ', end=' ')
for j in range(i):
print('*', end=' ')
for j in range(i+1):
print('*', end=' ')
print()
7.
Write a python program to print revers hill star pattern:
* * *
* * * *
* *
* * *
* * *
*
*
* * * *
* * *
*
Code:
n
= 5
for i in range(n):
for j in range(i+1):
print(' ', end=' ')
for j in range(i, n-1):
print('*', end=' ')
for j in range(i, n):
print('*', end=' ')
print()
8.
Write a python code to print Dimond pattern with star:
*
* * *
*
* * * *
* * *
* * *
*
* * * *
* * *
* *
* * *
* * *
*
*
* * * *
* * *
*
Code:
n
= 5
for i in range(n-1):
for j in range(i, n):
print(' ', end=' ')
for j in range(i):
print('*', end=' ')
for j in range(i+1):
print('*', end=' ')
print()
n
= 5
for i in range(n):
for j in range(i+1):
print(' ', end=' ')
for j in range(i, n-1):
print('*', end=' ')
for j in range(i, n):
print('*', end=' ')
print()
Post a Comment