Transcript for:
Understanding Patterns in Python Programming

Welcome back aliens my name is Devyn Reddy and let's continue with the series on Python. Now till this point we have talked about loops while loop for loop and then do those beautiful keywords and in this video we'll draw some patterns okay. Normally if you are in college or if you are learning about programming, this is important because it will improve the way you think. Programming is all about solving a problem, right?

So you have to understand the problem statement and you have to put that in a code. So mentally in your mind, you'll be having those solutions ready, but that will be in abstract format you have to implement that on your machine with the help of code. So what we'll do is we'll try to do some patterns so that it will improve the way you think about about logic.

So what I will do is initially we'll do this in fact yeah I know this is the assignment question you had in the last last video. So we want to print this pattern of course everyone have their own way of doing this and what what you can also simply do is you can print this multiple hash multiple times right so I want to print this. So what I will do is I will simply so if you want to do that It's very simple.

Just print hash, hash, hash, hash four times. And then you can do the same. Let's do that here.

OK. And then you can. So by doing this, you can see we got four hash.

Right. So at least we got the first stuff. But then what if I say you cannot use you can only use one hash in one print. Of course, you can you can draw the entire box using five for print statement.

But let's say if you want to print only one hash within one print. So what I want is I don't want to have multiple hash. I just want to have one.

one hash because if I run this code now you can see we got one hash I want to do at least so we'll first complete the first row now you can see we have four rows and four columns right so let's complete the first row now if you want to complete the first row you will print this multiple times or you will write print four times okay let's do that so I will just copy it and we'll paste it here we'll paste it here we'll paste it here so we got four prints right and if I run this code Oh we got four hash but then we want it in row format right not in vertical format. I want horizontal format. So what is happening here is after every period it is coming on new line.

So solution would be we can give a comma and say hey don't just come on new line so we can say end and double quotes. We can do the same thing for all the statement let's do it here. And now if I run this code you can see we got four hash in one line.

So this is what we got. We got the first block. Now what I want is I want four blocks right.

In fact there is one problem with this code as well. We are writing the same statement four times. Don't you think it is weird? So the best option here would be I want to do the same thing multiple times, right?

So we can use a loop and I will be using a loop here, which is a for loop. So I will say for, we'll use j in. You can use any variable name, right?

We have discussed about that, but let's use j here. So for j in. I want to have a range of let's say 4. Now when you say range of 4 it will start from 0 and it will end at 3. Right.

I will give a colon. And now if you do this you got the same output. Right.

So instead of typing 4 times we can just use a loop. Let's say so we got first row. Right.

I want the second row as well. So we can just copy this code because this will generate the second row. paste so we are writing a for loop two times so one for loop for first row and second for the for second row and if i run this code now you can see we got oh we got eight hash but then in one line the thing is we need to make sure that before second for loop i want to come on new line right let's run this oh we got it so we got we got four rows or two rows on four four columns okay this works in fact if i do the same thing for third time we will need to print print as well the new line so that's so with that so that's that will come a new line right so if I'm this code you can see we got three times if I do the same thing we got four times but then there's an issue here don't you think we are doing same thing repeatedly and when we know that if you want to do the same thing repeatedly we'll do it only once but then we will use a loop again so the entire block let me just give a tab the entire block will come under another loop which is in this case I use a for loop I in range of four so the entire block is a part of the another loop right so if I'm this code now look at this it's so simple right so this is how you learn about programming this how you learn about different logic so what we are doing is we are printing hash but multiple times in total we are printing hash 16 times you can see this loop runs 16 times let's do it so apply the breakpoint here it's a debug and let's iterate we'll say f8 you can see the value of I is 0 the value of J is 0 and then it will print hash and you can see the console we got the value one hash then the same loop will iterate so the value of j becomes one it will print hash the value of j becomes two it will print hash the value of j becomes three it will print hash and now this is the fourth time right so it will go for we got the fourth hash now it will jump outside the loop and in fact it will come on new line first you can see it has it will jump to new Right now and then the value of i becomes 1, yeah? And then the j value becomes 0. Again, it will start from 0 to 3. That's how it goes.

And then we got 4 hash, and then it will say new line, and then it will jump up. So this is how the thing works, right? It's so simple. Okay, now once we have done this, what if I want this pattern now? So in this, you can see we don't have the perfect square.

We have a triangle shape, right? So how would I do a triangle? Now if you concentrate here, look at the number of rows and columns because in the earlier version, where we have the entire box. We were having four rows and four columns. So we can say this J represents the number of column you have and I represent the number of rows you have.

Right. Because J is responsible to print the column at the end. So now here in this triangle you can see the number of columns in a particular row is depend upon the row number. Right.

So in the first row we have only one hash. In the second row we have two hash. Right. So so the number of hash is depend upon the row number. Right.

So what you can do is you can control this range. So we have to say hey don't go till 4 because if you go till 4 it will it will print 4 hash. I want to go till i. Okay so I will go till i if I run this code.

Oh we got a triangle okay because now we are not going at the end we are going till only till i. So when the value of i is 0 you can see it is not printing any hash. This is output for that. When your i value is 1 it is printing 1 hash.

When the value of i is 2 it is printing 2 hash right. But then this is something weird okay. So we wanted 4. 4 hash at the end. The problem is the value of i starts with 0. So we will go for 1. So it should be i plus 1. So when your i value is 0 it is actually first I want 1. So you can see we got 4 hash now. This is what we wanted.

So this was the earlier pattern we had and now I want to go for the next pattern which is this one. Now you can see this is also triangle but in another way. What is different here is I am printing the same triangle but in reverse order.

The number of rows, okay, so in the first row we have 4, in the second row we have 3, in the third row we have 2 and then it ends with 1. That means as your row number increases, your number of hash decreases, right? So what if you... can say 4 minus I now you'll be thinking what is this think about this when your I value is 0 in that case the J will go till 4 right it will print 4 hash when your I value is 2 the value for J will be 3 right so it will print 3 hash so depending upon your I value just printing those number of hash right so if I run this code you can say this is what you got right so this is how thing works here so this is how the another pattern we have worked with i hope you're enjoying this video let me know in the comment section and do subscribe for further videos