Patterns Definition :
A pattern is an idea or design that solves a common problem in many different circumstances. Understanding programming patterns is the key to writing efficient, readable and maintainable code.
Patterns are a way of thinking about programming problems. They are a way to solve problems in a general way, rather than solving them each time you encounter them. Patterns provide a better understanding of loops and how to write efficient, readable and maintainable code.
How to Print Patterns?
- You have to first figure out the number of rows in the pattern.
- Figure out the number of columns.
- Focus on what to print.
- We have to use an outer loop for iterating the rows.
- Then use an inner nested loop that deals with the number of columns.
Understand with an example:
****
****
****
****
Here, we have to print that pattern as shown above. How are we going to code that pattern?
Let's follow the mentioned steps:
Approach:
- Number of rows: The given pattern has 4 rows.
- Number of columns: The given pattern has 4 rows.
- What to print: We have to print '*'.
- Use an outer loop for rows:
- Use an inner nested loop for columns:
Using While loop:
i = 0while i < 4 : #outer loop that deals with rowsj = 0while j < 4: #inner loop that is for columnsprint('*', end = (''))j = j + 1print()i = i + 1
Using for loop:
for i in range(4) : #outer loop that deals with rowsfor j in range(4): #inner loop that is for columnsprint('*', end = (''))print()
Tags:
Blogs