PATTERNS-II

So now we have done enough patterns with the stars which were quite easy as we had to just understand the logic behind how many stars to be printed and just print them. But with numbers it is not so as we have to carefully take care of i,j… values which we are using in the loop to print our number pattern. In the last pattern blog, I left the 4th pattern for you, if you are done with it, its great, otherwise at the end of this blog I will share the solution with you. So now lets look at some of the number patterns in python.

So our first pattern has to print 1 one time, 2 two times and so on. So depending upon our input n, our pattern should print from 1 to n in the same fashion. We know the same code for doing stars, just we have to change our print statement within the loop and our job is done.

Inside the loop statement if we print the value of i instead of star, our star pattern is converted now into the above problem.

Here we can see, we are using our j loop in a different way which is not available in C/C++. By default the starting value is always 0, so we can just give for j in range(i), means loop will run from 0 to i-1.
output for above pattern

Now if we want to print the pattern which is shown to our left hand side, we just have to change one or two things, so you can make changes in your previous code itself.

Just run the inner loop from 1 to i+1 and print the value of j everytime, job is done. Your code will look like this:

def k():
 n=int(input(“Enter a number: “))
 for i in range(1,n+1):
 for j in range(1,i+1):
 print(j,end=””)
 print(“\r”)

output for the above code.

The last pattern for today is a bit tricky, in this we have to print the numbers in continuity, so no need of doing anything new here, just we have to use a third variable k and increment it after every iteration. The pattern which we have to print is:

So this the output if input given is 4, so if we give 5 as input, list will include 11 to 15 in the next row. In this fashion our pattern should go, so as I told u we have to just use a third variable as shown in code below:

def k():
 k=1
 n=int(input(“Enter a number: “))
 for i in range(1,n+1):
 for j in range(1,i+1):
 print(k,end=””)
 k=k+1
 print(“\r”)

OUTPUT for last pattern.

So these are some basic number patterns, I think this is enough for starters, we will do some complex and tricky patterns in the next blog. Now we can see the solution of pattern which was left in the previous blog, the pattern was:

So we had to write the code for the pattern given on the left. We want three loops in this pattern:

1.Overall loop will run n times

2. Loop for printing stars in a single line will run (n-i) times.

3. Loop for space will run depending on i value.

IDLE CODE
OUTPUT

So that’s all for today, hope you are learning something out of these patterns and now you are not afraid of patterns anymore. We saw just by simply printing i instead of j creates a lot of changes, hence we have to take care of these things. But if you understand the logic behind it, 70% of work is done, rest is just writing the code. So as I said, we will do some more number patterns in next blog, till then keep coding…