Solve any Star Pattern program in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this video we will learn how to  solve any star pattern program in python   when you search the internet you might have  come across many videos playlists or lengthy   blogs explaining different pattern programs so  do you need to learn each of them answer is no   what you need is to understand the basics and  learn the tricks so that you can answer any random   pattern question which is given to you now if you  go through this video you will be able to make any   of these patterns shown and i will explain to you  how now you might have played with blocks when you   were a kid you could create any kind of shapes and  design from a simple block we are going to use the   same method here what we are going to learn is two  basic blocks how to make an increasing triangle   and a decreasing triangle using just these two we  will teach you how to join them together to make   any kind of pattern that is asked so a complex  pattern is nothing but these two basic triangles   either filled with star hash or dollar an  important point you need to know before you start   is that when we print we do it row by row you  always start printing from left side of the screen   if your star is printed away from the left side  of the screen or there is space in between the   stars like in these two hills then you do not jump  and print this is actually done by printing spaces   so in this line there are four spaces printed  first then one star and then again seven spaces   and then one star and so on for all  remaining rows we cannot go back to   previous row you have to print this entire row  and then move to next row so let's get started   the first thing in pattern is the size size means  how big or how many rows you want in the triangle   now either you could be asked to take size from  the user and you can use input function to take in   the size or if you are asked to write a function  this n could be passed to you as a parameter if   you are given size in the question itself in that  case you can just directly assign it to a variable   n for our examples we will consider that we have  size in variable n which has value 5. also in the   remaining video we will just focus on code to make  the pattern depending upon your question you can   either write a function or take input from user  and just plug this code in first let's start by   basics pattern program requires you to use loops  if you do not know basics of loops in python   please watch my video on iterations first let's  start with a simple square as an example first   now if you are asked to print one star you will  write a print statement which will print a star   for you now if you are asked to print n stars  what will we do there are two ways you can do it   one is just use multiplication in print statement  and you can get n stars or we can use a loop   in this method we use the range function along  with for loop if we give range n then the loop   will run from 0 to less than n means i will have  values from 0 to 4 but if we run this code we see   it prints each star in a new line but we want it  in one line so we will add end is equal to blank   and it will override the new line and print stars  in the same line this loop will run five times and   print the star five times so we have written  this code with which we can print one row but   what we want is to print this row five times  to get our square pattern so what i will do is   insert this complete set of code inside another  loop which will print this row five times   if you run this you will see that all  stars are getting printed on the same row   how do we fix this we know our print statement  with end is equal to blank is making it print   in the same row so can we just remove it  from the print star statement to fix it   if we do that then what happens is  now each star is going to next line   what we want is 5 stars in the same row and then  it should go to next line means this loop will   continue to have print statement with end is equal  to blank and only after this whole row is printed   we go to next line so we will insert a print  statement as the last statement of the outer loop   so let's just understand how the loop will work we  start with the outer loop it holds our row numbers   and starts with 0 then we enter the inner loop  it holds our column numbers and starts with 0   and runs till less than n which is 4 to print  all the columns once inner loop is finished   the control comes back to the outer loop and  outer loop increments by 1 so we go to next row   and again in a loop prints 5 columns as  it will again start from 0 to less than   n this repeats for all rows till i is less  than n and then we exit from the outer loop   a key takeaway from this is do not forget to put  print statement before exiting the outer loop   this is a very important point and if you miss  writing print statement as the last statement   your whole pattern can go for a toss now if i run  this you will see our output has stars to close   and it is giving an effect of rectangle instead  of square you can fix this by just adding an extra   space using the end parameter what we will do is  instead of blank we will put a single space in   the end parameter which will ensure that there is  a space after every start so this is a final code   to print a square let's go back to the print  statement we are printing star here this could   actually be anything if you print hash it will  be a pattern of hash a dollar then it will be   a pattern of dollar and so on now let's learn  our first basic pattern of increasing triangle   let's take a previous program which printed a  square we will just edit this to print triangle   we know now that the outer loop determines the  rows and inner loop determines the columns we   see that in this pattern the rows are 5 or  n only so our outer loop is correct already   and requires no change and it is already printing  n rows coming to columns instead of printing   5 stars or printing from 0 to 4 it should print  1 star for 0 through 2 star for first row 3 star   for second row and so on point to note here is  that for each row i need row number plus 1 stars   row number is stored in variable i which  is starting from 0 and incrementing to 1 2   3 till all rows are printed so what we will just  change here is set inner loop to run to i plus 1.   this will ensure inner loop runs for only  one time in the first row two time in the   second row then three then four and then n this  completes our program for increasing triangle   important point to remember in this is that for  making an increasing triangle outer loop is set to   n and nested loop is set to i plus 1  remaining program is exactly the same   now let's learn second basic pattern  decreasing triangle we will again take   our square program we will just edit this to  print decreasing triangle if you see this code   outer loop is correct already and requires no  change as it is already printing n rows from   0 to less than n but now we need the columns to  print decreasing number of stars 5 4 3 2 and 1.   how do we change our nested for loop to reduce  by 1 every time this time we will add a start   condition we will use i as the start condition  so for first row the loop will run from 0 to 5   printing 5 stars second row it will run from 1  to 5 that means 4 times and then 3 2 and 1 star   this completes our program for decreasing  triangle so important point to remember in   making decreasing triangle pattern is that outer  loop is still n and nested loop is from i to n   now you have the basic ammunition in place if  you do not understand these two basic triangles   go back rewind and understand it again let's take  a look at some of the patterns we saw earlier   all of the patterns you see here are made from  combination of these two type of triangles as   you know we always start printing from left side  of the screen so where we see blank is actually   the same triangle pattern made with spaces  so this pattern is made up of two triangles   one increasing pattern of space and one  decreasing pattern of star this pattern also   has two triangles it has one decreasing pattern of  space followed by one increasing pattern of star   this pattern has three triangles first  it has one decreasing pattern of space   followed by one increasing pattern of star and  one more increasing pattern of star also do   note in all of these here the rows are n only but  columns have different printing options space or   star so let's try to solve it let's pick up this  pattern first now this pattern has two triangles   first is decreasing triangle printing space and  second is increasing triangle printing stars   do note that on same row we have to now print  space and star let's write the program for it   since our number of rows are same as that of  square pattern we will first copy the outer   loop from our previous program and it runs till  n to note our empty print line which takes us to   new row every time sticks to the end of the outer  loop you should not forget it or try to link it   with any nested loop ever first we have decreasing  triangle of space so we recall our code of nested   loop for decreasing triangle and we will write  our nested for loop for decreasing triangle from   i to n here the print statement will print space  only now the next triangle is increasing triangle   pattern printing stars in the same row so  we will just add another nested for loop   for increasing triangle pattern and we know this  will run till i plus one this will print star   let's take a look at how this will work for the  first row when i is zero first the upper nested   loop will execute and print 5 spaces then in the  same row it will enter the second nested loop and   will print 1 star then it will go to next row and  i will become one then it will print four space   from the first nested loop and then two star from  the second nested loop so these two nested loops   are working one after the other to print all  columns we need in a row this will go on till   the entire pattern is printed now this is the  program for this pattern you need to remember two   important things here first empty print statement  is linked to rows or outer loop we do not repeat   it with every inner loop second each print has  got either a space or a character followed by   a space which is part of the end that means two  characters are printed in each print statement   all of your print statement need to have the same  number of characters even if it is printing space   now let's see another pattern what if the pattern  was like this it is increasing triangle of space   and then decreasing triangle of star since our  number of rows are same as that of square pattern   we will first copy the same outer loop from the  previous program and it runs till n since first   loop is increasing triangle we will write our  nested for loop for increasing triangle till i   plus 1. here the print statement will print space  only now the next triangle is decreasing triangle   so we will just add another nested for loop for  decreasing triangle pattern which will run from   i to n this will print start this  is the final code for this pattern   now what if pattern was like this a hill  pattern now we already have written code for   first two triangle of this pattern here the third  triangle is also an increasing triangle pattern   so we will write one more nested loop now there  is a small catch here which you need to remember   let's run this and see here when you see the  output you see it does not have a peak why   because our loop knows how to make 5 columns each  in such pattern actually there is one less column   printed we need to make one of the nested loop  print one less column so to make this nested loop   run one less we will just change the for condition  of first increasing triangle from range i plus 1   to range i this will make the loop print one less  column so this is a final code for this pattern   now what if it was reverse mountain pattern  here there is one decreasing triangle and two   increasing triangle now we already have written  code for first two triangle of this pattern   like in previous question we will just add a  third nested loop for increasing triangle and to   make sure we have our valley correct we will have  nested loop print 1 less column so we will change   n to n minus 1 in range function this  is our code for reverse mountain pattern   let's take a look at patterns which double up  on rows as well let's take this diamond pattern   here you have to print pattern below another  pattern now we have the code for hill pattern   and inverted hill pattern 2. here note that the  rows are also doubling up so what you need to   do is just place two code one below the other  along with the outer loop so you run one outer   loop along with its nested loop for rows of upper  set of triangles and run another outer loop along   with its nested loop for rows for lower set of  triangles let's run this to see by just doing this   do we get the desired pattern this is close  but this too has one issue that it does not   have pointed corners looking closely we  see actually our rows two don't double up   there is one less row to give us pointed edges  the solution is similar to what we did for columns   we need to print one less row how do we do that  we just reduce the condition of outer for loop   to print one less row effectively deleting its  last row so we will just change our outer loop   condition from range n to range n minus 1.  now this is our required program for diamond   now you can try any different pattern like  this shown using the same logic break it into   increasing and decreasing triangles and then just  bundle up the code hope you have understood how to   solve the pattern problems in our next video  we will cover how to solve any of the number   patterns if you have any doubts do reach out  to us at simplycoding.in thank you and goodbye
Info
Channel: Simply Coding
Views: 513,082
Rating: undefined out of 5
Keywords: Simply Coding, simplycoding, CBSE, ICSE, SSC, Computer Applications, Computer Science, Information technology, NCERT
Id: fX64q6sYom0
Channel Id: undefined
Length: 18min 44sec (1124 seconds)
Published: Mon May 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.