KEMBAR78
BK Patternpgm1 | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
34 views17 pages

BK Patternpgm1

The document discusses various ways to print star patterns in Python using for loops and while loops. It explores printing stars in rows, columns, and diagonal patterns. Key techniques include using range() to control the number of iterations, printing stars with end="" to print on the same line, and nesting loops to print multi-line patterns where the inner loop controls the number of stars printed in each row. Various approaches are tested to build up star shapes line by line through incremental changes to the loop logic and print parameters.

Uploaded by

531Binod Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views17 pages

BK Patternpgm1

The document discusses various ways to print star patterns in Python using for loops and while loops. It explores printing stars in rows, columns, and diagonal patterns. Key techniques include using range() to control the number of iterations, printing stars with end="" to print on the same line, and nesting loops to print multi-line patterns where the inner loop controls the number of stars printed in each row. Various approaches are tested to build up star shapes line by line through incremental changes to the loop logic and print parameters.

Uploaded by

531Binod Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

To print 5 star in one column

In [1]:

i=0
while i<5:
print("*")
i=i+1
print()

*
*
*
*
*

In [2]:

i=0
while i<5:
print("*")
i+=1

*
*
*
*
*

In [4]:

i=0
while i<5:
print("*",end=" ")
i+=1

* * * * *

In [7]:
for i in range(5):
print(*)

File "<ipython-input-7-f974fa1313b4>" , line 2


print(*)
^
SyntaxError: invalid syntax

In [8]:
for i in range(5):
print(*) #Missing inverted comma
i=i+1
print()

File "<ipython-input-8-5875c8b7ca3e>" , line 2


print(*)
^
SyntaxError: invalid syntax

In [10]:
for i in range(5):
print("*")
i=i+1
print()
*

In [5]:
for i in range(5):
print("*")
i=i+1

*
*
*
*
*

In [1]:
for i in range(5):
print("*")

*
*
*
*
*

In [11]:
for i in range(5):
print("*")
i=i+1
print()

*
*
*
*
*

In [14]:
for i in range(5):
print("*")
i=i+1
print(" ") #Space btw double quotes but no effect
*
*
*
*
*

In [13]:
for i in range(5):
print("*")
i=i+1
print("*") #Here one more * will be print
*
*
*
*
*
*
*

In [9]:
for i in range(5):
print("*")
i=i+1
print("*") #Here previous print statement will again executed bcz it is inside
for loop ,so this line
#follow same increment opperator ,10 stars here.image w.r.t x-axis
.
*
*
*
*
*
*
*
*
*
*

In [4]:
for i in range(5):
print("*")
i=i+1
print("*") #Here previous print statement will again executed bcz it is inside
for loop ,so this line
print("*") #follow same increment opperator ,15(3 times 5 star in ho
rizontal) stars here.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*

In [1]:
for i in range(5):
print("*",end="")
i=i+1
print("*") #Here previous print statement will again executed bcz it is inside
for loop ,so this line
#follow same increment opperator ,10 stars here.
**
**
**
**
**

In [2]:
for i in range(5):
print("*",end="")
i=i+1
print() #Here previous print statement will again executed bcz it is inside fo
r loop ,so this line
#follow same increment opperator ,5 stars here.
*
*
*
*
*

In [2]:
for i in range(5):
print("*",end="")
i=i+1
print("*") #Here previous print statement will again executed bcz it is inside
for loop ,so this line
print() #follow same increment opperator ,10 stars here.
**
**
**
**
**

To print 5 star in one row


In [2]:
i=0
while i<5:
print("*",end="")
i=i+1
print()

*****

In [4]:
i=0
while i<5:
print("*",end=" ")# Space btw double quotes so that it print some space
i=i+1
print()

* * * * *

In [15]:
i=0
while i<5:
print("*",end=" ")# Space btw double quotes so that it print some space
i=i+1
print(" ") #No effect of space here
* * * * *

In [6]:
i=0
while i<5:
print("*",end=" ")# Space btw double quotes
i=i+1
print() #Here print inside the block of loop ,so it will print in column
*
*
*
*
*
*

In [5]:
i=0
while i<5:
print("*",end=" ")# Space btw double quotes
i=i+1
print(*) #Missing double quotes and one extra * will be print
File "<ipython-input-5-0adf566d01ee>" , line 5
print(*)
^
SyntaxError: invalid syntax

To print two parallel line


In [17]:
i=0
while i<5:
j=0
while j<2:
print("*",end=" ") #Space btw two lines
j=j+1
print("*") # one extra line , not inside block2
i=i+1
print()

* * *
* * *
* * *
* * *
* * *

In [10]:
i=0
while i<5:
j=0
while j<2:
print("*",end=" ") #Space btw two lines
j=j+1
print("*") # one extra line
i=i+1

* * *
* * *
* * *
* * *
* * *

In [11]:
i=0
while i<5:
j=0
while j<2: # 10 row , for j=0 ,5 rows and for j=1 5 rows in column bcz 2nd print
inside of block.
print("*",end=" ") #Space btw two lines
j=j+1
print("*") # one extra image w.r.to y axis
i=i+1

* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *

In [5]:
i=0
while i<5:
j=0
while j<4: #20 row it will executed vertically
print("*",end=" ") #Space btw two lines
j=j+1
print("*") # one extra line(image w.r.t y-axis) in left bcz end="" in prev stat
ement
i=i+1

* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *

In [9]:
for i in range(5):
print("*")
i=i+1
print("*") #Here previous print statement will again executed bcz it is inside
for loop ,so this line
#follow same increment opperator ,10 stars here.
*
*
*
*
*
*
*
*
*
*

In [6]:
j=0
while j<2:
print("*",end=" ") #Space btw two lines
j=j+1
print("*") # one extra star at the end ( in row bcz prev statement in row)

* * *

In [3]:
j=0
while j<4:
print("*",end=" ") #Space btw two lines
j=j+1
print("*") # one extra vertical pattern of previous print statement.

* *
* *
* *
* *

In [8]:
j=0
while j<2:
print("*",end=" ") #Space btw two lines
j=j+1

* *

In [12]:
i=0
while i<5:
j=0
while j<2:
print("*",end=" ")
j=j+1
i=i+1
print()

* * * * * * * * * *

In [19]:
i=0
while i<5:
j=0
while j<2:
print("*",end=" ")
j=j+1
print()
i=i+1
print()

* *
* *
* *
* *
* *

In [20]:
i=0
while i<5:
j=0
while j<2:
print("*",end=" ")
j=j+1
print()
i=i+1

* *
* *
* *
* *
* *

In [21]:
i=0
while i<5:
j=0
while j<8:
print("*",end=" ")
j=j+1
print()
i=i+1

* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *

In [28]:
for i in range(5):
for j in range(8):
print("*",end=" ")
j+=1
print()
i+=1 # i.e i=i+1
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *

In [14]:
for i in range(5):
for j in range(8): #by default it take increment as +1
print("*",end=" ")
print()

* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *

To print right angled triange and image from all view (Think on
the basis of row and column let i as row index and j as
column)
In [22]:
i=0 # i j=0,1,2,3,4 i
j iRj ie j<=i
while i<5: # 0 * 0
0 j<=0 and j is inital
j=0 # 1 * * 1
0,1 j<=1 as zero
while j<=i: # 2 * * * 2
0,1,2 j<=2
print("*",end=" ") # 3 * * * * 3
0,1,2,3 j<=3
j=j+1 # 4 * * * * * 4
0,1,2,3,4 j<=4
print()
i=i+1

*
* *
* * *
* * * *
* * * * *

In [29]:
In [29]:
for i in range(5):
for j in range(i+1):
print("*",end=" ")
j+=1
print()
i+=1

*
* *
* * *
* * * *
* * * * *

In [30]:
for i in range(5):
for j in range(i+1):
print("*",end=" ")
j+=1
print(" ")
i+=1

*
* *
* * *
* * * *
* * * * *

In [15]:
i=0 # i j=0,1,2,3,4 i
j iRj ie j>=i
while i<5: # 0 * * * * * 0
0,1,2,3,4, j>=0 and j is inital
j=4 # 1 * * * * 1
0,1,2,3 j>=1 as 4
while j>=i: # 2 * * * 2
0,1,2 j>=2
print("*",end=" ") # 3 * * 3
0,1, j>=3
j=j-1 # 4 * 4
0, j>=4
print()
i=i+1

* * * * *
* * * *
* * *
* *
*

In [32]:
for i in range(5):
for j in range(4,-1,-1):
print("*",end=" ")
j+=1
print(" ")
i+=1

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

In [37]:
for i in range(5):
for j in range(4,i-1,-1):
print("*",end=" ")
j+=1
print(" ")
i+=1

* * * * *
* * * *
* * *
* *
*

In [14]:
for i in range(5): # it is a collection of two pattern
for j in range(i+1):
print("*",end=" ")
j+=1
print(" ")
i+=1

for i in range(5):
for j in range(4,i-1,-1):
print("*",end=" ")
j+=1
print(" ")
i+=1

*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*

In [19]:
for i in range(5):
for j in range(i+1):
print("*",end=" ")
j+=1
print(" ")
i+=1

for i in range(5):
for j in range(3,i-1,-1): # in 2nd pattern column is initialize from 3
print("*",end=" ")
j+=1
print(" ")
i+=1

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

In [38]:
for i in range(5):
for j in range(4,i+1,-1):
print("*",end=" ")
j+=1
print(" ")
i+=1
* * *
* *
*

In [35]:

for i in range(5):
for j in range(4,j>=i ,-1):
print("*",end=" ")
j+=1
print(" ")
i+=1

* * *
* * *
* * *
* * *
* * * *

In [16]:
i=0 # i j=0,1,2,3,4 i Space j
iRj Space k ie j>=4-i
while i<5: # 0 * 0 0,1,2,3 4
j>=4-0 k<4-i and j is inital
j=4 # 1 * * 1 0,1,2 3,4
j>=4-1 as 4 and k=0
while j>=4-i:
k=0 # 2 * * * 2 0,1 2,3,4
j>=4-2
while k<4-i:
print(end=" ") # 3 * * * * 3 0 1,2,3,
4 j>=4-3
k=k+1 # 4 * * * * * 4 0,1,2,
3,4 j>=4-4
print("*",end="")
j=j-1
print()
i=i+1
print()

*
*
*
* * *
* * * *
*****

In [5]:
i=0 # i j=0,1,2,3,4 i Space j
iRj Space k ie j>=4-i
while i<5: # 0 * 0 0,1,2,3 4
j>=4-0 k<4-i and j is inital
k=0 # 1 * * 1 0,1,2 3,4
j>=4-1 as 4 and k=0
while k<4-i:
print(" ",end="") # 2 * * * 2 0,1 2,3,4
j>=4-2
k=k+1
j=4
while j>=4-i:
print("*",end="")
j=j-1
i=i+1
print()

# 3 * * * * 3 0 1,2,3,4 j
>=4-3
# 4 * * * * * 4 0,1,2,3,4 j
>=4-4 j=j-
*
**
***
****
*****

In [4]:
for i in range(5):
for k in range(5-i):
print(" ",end="")
for j in range(4,3-i,-1): #4-i-1=3-i
print("*",end="")
print()

*
**
***
****
*****

In [1]:

i=0 # i j=0,1,2,3,4 i Space j


iRj Space k ie j>=4-i
while i<5: # 0 * * * * * 0 __ 0,1,2,3,4
j>=i k<i and j is inital
k=0 # 1 * * * * 1 0, 1,2,3,4
as 4 and k=0
while k<i:
print(" ",end="") # 2 * * * 2 0,1 2,3,4
k=k+1 # 3 * * 3 0,1,2 3,4
j=4 # 4 * 4 0,1,2,3 4
while j>=i:
print("*",end="")
j=j-1
i=i+1
print()

*****
****
***
**
*

In [2]:

for i in range(5): # Space pattern


for k in range(i):
print(" ",end="")
for j in range(4,i-1,-1):
print("*",end="")
print()

*****
****
***
**
*

In [18]:
for i in range(5): #Space pattern
for k in range(i):
print(" ",end="")
for j in range(4,i-1,-1):
print("*",end=" ") #Add one step space btw double quotes
print()
* * * * *
* * * *
* * *
* *
*

In [17]:

for i in range(5):
for k in range(5-i):
print(" ",end="")
for j in range(4,3-i,-1): #4-i-1=3-i
print("*",end="")
print()

for i in range(5):
for k in range(i):
print(" ",end="")
for j in range(4,i-1,-1):
print("*",end="")
print()

*
**
***
****
*****
*****
****
***
**
*

To print a rectangular shape unit enter by user


In [8]:
x=int(input("Enter length units : "))
y=int(input("Enter Breath units : "))
for i in range(x):
for j in range(y):
if (i==0 or i==x-1 or j==0 or j==y-1):
print("*",end=" ")
else:
print(end=" ")
print()

Enter length units : 6


Enter Breath units : 3
* * *
* *
* *
* *
* *
* * *

In [9]:

x=int(input("Enter length units : "))


y=int(input("Enter Breath units : "))
for i in range(x):
for j in range(y):
if (i==0 or i==x-1 or j==0 or j==y-1):
print("*",end="") #No space btw double quotes
else:
print(end=" ") # Space btw double quotes
print()

Enter length units : 8


Enter Breath units : 6
Enter Breath units : 6
******
* *
* *
* *
* *
* *
* *
******

To print a letter A
In [12]:
for i in range(6):
for j in range(4):
if ((i==0 and (j!=0 and j!=3)) or i==3 or ((j==0 or j==3) and i>0)):
print("*",end=" ") #one space
else:
print(end=" ") # one more space than 4 line to view a separate
print()

* *
* *
* *
* * * *
* *
* *

To print a letter B
In [19]:
for i in range(7):
for j in range(3):
if (((i==0 or i==3 or i==6) and j!=2) or j==0 or (j==2 and (i!=0 and i!=3 and i
!=6))):
print("*",end=" ") # and means holds all property
else:
print(end=" ")
print()

* *
* *
* *
* *
* *
* *
* *

In [20]:

for i in range(7):
for j in range(3):
if (((i==0 or i==3 or i==6) and j!=2) or j==0 or (j==2 and (i!=0 and i!=3 and i
!=6))):
print("*",end="") # No space
else:
print(end=" ") # one space
print()

**
* *
* *
**
* *
* *
**
In [1]:

for i in range(7):
for j in range(3):
if (((i==0 or i==3 or i==6) and j!=2) or j==0 or (j==2 and (i!=0 or i!=3 or i!=
6))): # or means any one cond true all T
print("*",end="")
else:
print(end=" ")
print()

***
* *
* *
***
* *
* *
***

To print a letter M
In [18]:

for i in range(7):
for j in range(5):
if ((i==1 and j!=2) or j==0 or j==4 or (i==2 and (j!=1 and j!=3))):
print("*",end=" ") # one step space
else:
print(end=" ") #two step space
print()

* *
* * * *
* * *
* *
* *
* *
* *

To print a letter Z
In [21]:

for i in range(6):
for j in range(5):
if (i==0 or i==5 or (i==1 and j==3) or (i==2 and j==2) or (i==3 and j==1) or (i
==4 and j==0)):
print("*",end=" ") # one step space
else:
print(end=" ") #two step space
print()

* * * * *
*
*
*
*
* * * * *

In [22]:

for i in range(1,6):
for j in range(1,i+1):
print("12345",end="")
print()

12345
1234512345
123451234512345
12345123451234512345
1234512345123451234512345

In [15]:

for i in range(5):
for k in range(5-i):
print(" ",end="")
for j in range(4,3-i,-1): #4-i-1=3-i
print("*",end=" ")
print()

for i in range(5):
for k in range(i):
print(" ",end="")
for j in range(4,i-1,-1):
print("*",end=" ")
print()

*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*

In [4]:

for i in range(5):
for k in range(i):
print(" ",end="")
for j in range(4,i-1,-1):
print("*",end=" ")
print()

* * * * *
* * * *
* * *
* *
*

In [13]:
for i in range(5):
for k in range(5-i):
print(" ",end="")
for j in range(4,3-i,-1): #4-i-1=3-i
print("*",end=" ")
print()

*
* *
* * *
* * * *
* * * * *

In [20]:

for i in range(5):
for k in range(5-i):
print(" ",end="")
for j in range(4,3-i,-1): #4-i-1=3-i
print("*",end=" ")
print()
for i in range(5):
for k in range(i):
print(" ",end="")
for j in range(4,i-1,-1):
print("*",end=" ")
print()

*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*

In [ ]:

You might also like