Printing patterns in python
Till now we learnt about different loops and control statements in Python and we can use
this concept to print different patterns , patterns of alphabet ,Numbers and symbols like # , *
We can use simply nested for loops (2) outer for loop will manage number of rows and inner for loop decide the number of columns in pattern
We will discuss here some important question that most likely ask in interviews and aptitude tests
this concept to print different patterns , patterns of alphabet ,Numbers and symbols like # , *
We can use simply nested for loops (2) outer for loop will manage number of rows and inner for loop decide the number of columns in pattern
We will discuss here some important question that most likely ask in interviews and aptitude tests
Q.1 Write a code to print following pattern in Python
# # #
# # #
# # #
Solution =
for a in range (3):
for j in range (3):
print("#",end ="")
print()
In above question there are three rows and three columns of" # "
We use range function in above code and( end ="") is use to be on same line and you can use debug function to understand how it actually code runs.
Q.2 Write a code to Print following pattern in python ?
****
***
**
*
Solution=
for a in range (4):
for j in range (4-a):
print("*",end="" )
print()
As value of a is increasing after every iteration and (4 -a) is decreasing
at first iteration value of a is zero therefore
( 4 - a =4) and it will print " * "for 4 time and in next iteration value of a is increase value of (4-a) will decrease and in this way this programme works
Q.3 Write a code to Print following pattern in python ?
- $
$$
$$$
$$$$
$$$$$
(This question is just opposite to Q.2 )
Solution =
for a in range (4):
for j in range (a+1):
print("$",end="" )
print()
Q.4 Write a code to Print following pattern in python ?
-1 2 3 4
2 3 4
3 4
4
Solution =
Please debug code in PyCharm to understand how this code actually works .
You can try a code for printing pattern
Q.5 Write a code to Print following pattern in python ?(Assignment question )
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
I hope this article on printing patterns in python is helpful for u ....
Thank u ...😊