How to print pattern in python?
In any programming language, everyone wants to print pattern of different types.
To make a certain pattern we have to follow certain common steps.
- Define a function
- 1st for loop for rows
- 2nd for loop for columns
We can make patterns with numbers, asterisk, etc. We’ll see some examples here.
e.g.,
rows = 6 for n in range(rows): for i in range(n): print(n, end=" ") # print number print(" ")
e.g.,
rows = 5 for i in range(0, rows): for j in range(0, i + 1): print("*", end=' ') print("\r")
e.g.,
rows = 5 k = 2 * rows - 2 for i in range(rows, -1, -1): for j in range(k, 0, -1): print(end=" ") k = k + 1 for j in range(0, i + 1): print("*", end=" ") print("")
This is all about printing patterns in python.
Subscribe
Login
Please login to comment
0 Discussion