Programming language, Python 06 – PATTERN PROGRAMS IN PYTHON PROGRAMMING
So today pattern is so. This is our pattern today, so we just want to write the logic for this pattern, so stars right. So this is how we can we have to print, and obviously there are five rows so first, the outer for loop is for representing the row so for i, in a range of one to six simply it gives the spheros. That means one. I i value will be ranging from one two three, four and five, for example. Consider this is one. This is two. This is three, this is four, and this is five, so you can see in the first row how many spaces should be given. So, starting from here right starting from here so one two, three and four see first line. Four spaces should be there and at the fifth position we have to give the start so start from five for j in range. J in range start the fight 5. 2. I i means, if i is equal to 1 so 5 to 1. That means range of 5 comma 1 and give the negative updation minus one. So five, four three two: it will iterate four times three later four times so five, four, three two! Then we have to print at the fifth position. We have to print the star so for that purpose, just write the print spaces just give the space, because we have to print the spaces for all the iterations right. Next, after that, we have to print the star.
So again write the purple for k in range of so how many uh stars we have to print only one so first of all one star thats. Why just go with the one to i plus one? If, if i is equal to one then range of one two, that means only one iteration. If i is equal to two range of one comma three, that means one comma, two iterations will be there, so here we need to print star print star and, as usual, use the end parameter in order to print the next output in the same line and after The after completion of one iteration outer iteration just use the mp print so that the next iteration will be sprinting in the new line, so in the next iteration, whenever j becomes, whenever i becomes 2 j becomes 5 comma, 2, comma minus 1. So the j value will be iterating from 5, 4 3, because 2 is the stop index. We should not include that so power. 5, 5, 4, 3, 3 spaces will be given here in the second line and then k in range one comma, i plus two. That means two that is three, so it will give one to two iterations star will be printed in two two iterations right so like that will get the required pattern right next. The next pattern is the reverse one so give the star. So reverse reverse pattern. So if you want to print the same thing, the same logic will be used same logic will be used.
So here we are printing five in the first row, four in the second row, three in the third row, two in the fourth row, one in the first row so simply change the positive updation to negative updation, so use for i in range of 5, comma 0. Minus 1, so i becomes 5 in the first iteration four in the second iteration three in the third third iteration right and uh, two in the fourth iteration one in the fifth iteration. So it will stop at one. So whenever i becomes five so see, j is equal to 5, so 5, 5 minus 1, 0, 0 times 5, 5 minus 1. Okay, the range of 5, 5 minus 1, so automatically it ends. The i mean the end index is fine, so it will not iterate and even one time right so zero times it will be iterating so zero times the star will be printed and k in range. One two, six k in range, one, two: six. That means, if you literally one two three four and five so total five iterations, it will be iterating, so five iterations star will be plated so field in the first line. Five stars will be printed in the next line, so five comma, four minus 1, 5, comma, 4, minus 1. So only one time it will be iterated only one type. That means one space will be given. One space will be given and then k will be iterated with 1 comma 5.
So that means one two three four four times four iterations so four times a star will be printed. So, in order to get this pattern, just instead of giving the positive operation give the negative operation – five comma, zero, comma, minus 1 thats all so automatically. Well get this reverse spectrum right, so just observe how many spaces are there, so that that many spaces first print? The spaces and then print the pattern right so hope you understood this one. I will execute the same code in the system and i will show you the executation execution part so lets move on to the system, hello, friends. So, just now we have seen the logic for the following patterns, so this is a one pattern we have seen just now, and this is the second pattern we have seen the logic now. We will execute the program to print these following patterns, so the first one is a rows in order to print the rows. The for loop, for i in range of uh, give the values so give the rows. So let us take some fiber, so i will go with the one to six so that i value will be ranging from one two three four and five so total five lines and then first we need to print the spaces. So if you are going this one, so you have to print the spaces. So this is a one two, three, four, five. Six and first we need to print some four spaces and the fifth place.
We have to go with the star so thats. Why print spaces? For j in a range of so go with the phase spaces – 5, comma, i minus 1 and print the spaces here, print just an empty space and give in this same column, and after that we have to print the star according to the row number. So you can observe here the first row only one star second row, who starts the third or three steps now just print here for k in range of 1 to i 1 to i print, give the star value star and print this star in the same line And after that, just use a normal print in order to print in a new lines after every iteration, the new line – okay, so you can see here so yeah here give the y plus one so that will get the stars. So this is the a simple pattern. One type of pattern and uh hope you understood this one first, one first, w outer loop is for rows and the inner loop first, we need to represent the spaces, and the third loop is to print the value right. So if it is a first line, 4 spaces will be there so because 5 comma 1, when i is equal to 1 5 comma 1, so that is 5 4 3 2 4 spaces will be there and afterwards, so star will be printed because 1 to 2. That means 1 will be printed so similarly, it will iterate, for i is equal to one two, three four and five, so total five lines and if you want to imprint the reverse order, the same thing in a reverse order.
So, instead of giving the positive uh range, give the negative right so negative vibration, so, for i, in range of so go with the 5 to 0 minus 1, 5 to 0 minus 1 and all the remaining is same first, we need to print the spaces so For j in range of 5 to i minus 1 print and is equal to this one and next to print the star right for k in a range of one one. Two, i plus one i plus one and go with the start print the star print star value and is equal to and then give the empty print for next line fitting in the next line. So simple! So if it is a decrementing go with the negative negative operation, if it is a positive, then go with the this one positive operation, thats, the only thing you need to remember right so simple thing, so there is a no but not much difference in these two Logics, yes, this is for incrementing updation, okay, positive operation – and this is a negative objection lets say so. Whenever i becomes five so j in range of five to five okay, j in range of five to five, so minus one so try it directly five times it will be treated here right. Okay, so hope you understood this one. So if you really enjoyed my session, like my session, share my session with your friends and dont forget to subscribe to our channel thanks for watching.