Based on CBSE Computer Science and Informatics Practices Syllabus – Solving Pattern questions using Python

Patterns questions help you improve on logic around loops and repetitive tasks having some symmetry

Almost all the languages support repetitive tasks by providing structures like loops. All the procedural languages like C, C++, Java, Python, PHP etc. provide loops to handle logic that deal with repetitive tasks. Pattern questions are one of them, taught to polish the logical thinking of students.

Languages like C, C++, Java follow a similar approach to handle the pattern questions, but Python has to be different.

For example the pattern to be created is:

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

Traditional programming Languages Approach – for example C/C++ style

Traditional approach towards solving this problem would require two loops, as shown in the example. A Python code that uses approach similar to the seasoned C, C++ or Java Programmer to handle pattern questions.

# program to print pattern
rows=int(input("Enter the number of rows:"))
for x in range(0,rows):
    for y in range(0,x+1):
        print("*",end="")
    print("")

Observe:

  • The program first asks for how total number of rows a pattern should stretch across.
  • It uses two loops one nested inside the other. Outer loop controls theĀ  rows and the inner loop controls the columns.
  • Observe the print statement:
print("*",end="")
  • The statement above prints the “*”, and end=”” ensures that the stars (“*”) are printed on the same line.
  • Next print statement outside the inner loop, takes the control to the next line (i.e. after printing all the stars on the current line).
print("")

Python’s Approach – you can do it with less

Observe the code carefully, it does the same job as above program but with less efforts and smaller code

# program to print pattern
rows=int(input("Enter the number of rows:"))
for x in range(0,rows+1):
    print("*"*x)
  • You will observe it uses only one loop. that would save a lot of execution time.
  • Loop has been employed to handle total number of rows entered by the user (line no. 2)
  • Observe the print statement, carefully, it is because of this statement we could get away with only one loop.
print("*"*x)
  • In Python when we try to multiply a string with a number n, it does not reports any error. It simply prints that string but n number of times.
  • In the above program it prints “*” x number of times (because of “*”*x inside print function).

Note: The discussion above is in context with the given pattern. It does not imply that all kind of patterns would require single loop only.

Conclusion

Try to type the above code and run using Python 3.x. Observe the outputs of both the traditional as well as python approach. Try to solve more similar pattern questions to improve your logic.

Happy Coding.

 

 

 

 

 

Pawan Arora AdministratorKeymaster
Founder , Edukers
Teaching, Coding and Sharing is his passion. A true mentor and motivator. C/C++, Python, Java, Web Technologies (html5 / CSS/ Javascript/ JQuery,Bootstrap, nodeJS ,PHP etc.) and a WordPress enthusiast with more than two decades of experience.
follow me