Solve any Star Pattern program in Python

Solve any Star Pattern program in Python


Solve any Star Pattern program in Python

In this video we will learn how to write any star pattern program in Python

Watch any number pattern in python -    • Solve any Number Pattern program in P…  

00:00 - Introduction
03:13 - Simple square star pattern in python
07:12 - Increasing triangle star pattern in python
08:51 - Decreasing triangle star pattern in python
10:15 - Different type of patterns in python
11:21 - Right sided triangle star pattern in python
14:11 - Right sided downward triangle star pattern in python
15:00 - Hill star pattern in python
16:01 - Reverse hill pattern in python
16:39 - Diamond star pattern in python

For Notes on How to solve any star patterns in python click on - https://simplycoding.in/star-pattern-


Content

0.4 -> hello in this video we will learn how to  solve any star pattern program in python  
7.2 -> when you search the internet you might have  come across many videos playlists or lengthy  
13.2 -> blogs explaining different pattern programs so  do you need to learn each of them answer is no  
20.64 -> what you need is to understand the basics and  learn the tricks so that you can answer any random  
27.04 -> pattern question which is given to you now if you  go through this video you will be able to make any  
34 -> of these patterns shown and i will explain to you  how now you might have played with blocks when you  
41.28 -> were a kid you could create any kind of shapes and  design from a simple block we are going to use the  
49.44 -> same method here what we are going to learn is two  basic blocks how to make an increasing triangle  
57.52 -> and a decreasing triangle using just these two we  will teach you how to join them together to make  
64.56 -> any kind of pattern that is asked so a complex  pattern is nothing but these two basic triangles  
72 -> either filled with star hash or dollar an  important point you need to know before you start  
79.68 -> is that when we print we do it row by row you  always start printing from left side of the screen  
88.16 -> if your star is printed away from the left side  of the screen or there is space in between the  
94.32 -> stars like in these two hills then you do not jump  and print this is actually done by printing spaces  
103.12 -> so in this line there are four spaces printed  first then one star and then again seven spaces  
111.04 -> and then one star and so on for all  remaining rows we cannot go back to  
117.52 -> previous row you have to print this entire row  and then move to next row so let's get started  
126.16 -> the first thing in pattern is the size size means  how big or how many rows you want in the triangle  
134.56 -> now either you could be asked to take size from  the user and you can use input function to take in  
141.52 -> the size or if you are asked to write a function  this n could be passed to you as a parameter if  
148.96 -> you are given size in the question itself in that  case you can just directly assign it to a variable  
156.08 -> n for our examples we will consider that we have  size in variable n which has value 5. also in the  
165.12 -> remaining video we will just focus on code to make  the pattern depending upon your question you can  
172.4 -> either write a function or take input from user  and just plug this code in first let's start by  
180 -> basics pattern program requires you to use loops  if you do not know basics of loops in python  
188 -> please watch my video on iterations first let's  start with a simple square as an example first  
195.92 -> now if you are asked to print one star you will  write a print statement which will print a star  
202.32 -> for you now if you are asked to print n stars  what will we do there are two ways you can do it  
209.76 -> one is just use multiplication in print statement  and you can get n stars or we can use a loop  
218 -> in this method we use the range function along  with for loop if we give range n then the loop  
226.08 -> 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  
235.44 -> it prints each star in a new line but we want it  in one line so we will add end is equal to blank  
243.84 -> and it will override the new line and print stars  in the same line this loop will run five times and  
251.68 -> print the star five times so we have written  this code with which we can print one row but  
259.12 -> what we want is to print this row five times  to get our square pattern so what i will do is  
266.8 -> insert this complete set of code inside another  loop which will print this row five times  
274.24 -> if you run this you will see that all  stars are getting printed on the same row  
280 -> how do we fix this we know our print statement  with end is equal to blank is making it print  
287.04 -> in the same row so can we just remove it  from the print star statement to fix it  
293.2 -> if we do that then what happens is  now each star is going to next line  
298.64 -> what we want is 5 stars in the same row and then  it should go to next line means this loop will  
306.32 -> continue to have print statement with end is equal  to blank and only after this whole row is printed  
314.32 -> we go to next line so we will insert a print  statement as the last statement of the outer loop  
322.4 -> so let's just understand how the loop will work we  start with the outer loop it holds our row numbers  
330.24 -> and starts with 0 then we enter the inner loop  it holds our column numbers and starts with 0  
338.48 -> and runs till less than n which is 4 to print  all the columns once inner loop is finished  
346.16 -> the control comes back to the outer loop and  outer loop increments by 1 so we go to next row  
354 -> and again in a loop prints 5 columns as  it will again start from 0 to less than  
360.16 -> n this repeats for all rows till i is less  than n and then we exit from the outer loop  
368.48 -> a key takeaway from this is do not forget to put  print statement before exiting the outer loop  
376.64 -> this is a very important point and if you miss  writing print statement as the last statement  
383.12 -> your whole pattern can go for a toss now if i run  this you will see our output has stars to close  
391.2 -> and it is giving an effect of rectangle instead  of square you can fix this by just adding an extra  
398.56 -> space using the end parameter what we will do is  instead of blank we will put a single space in  
406.4 -> the end parameter which will ensure that there is  a space after every start so this is a final code  
414.4 -> to print a square let's go back to the print  statement we are printing star here this could  
422.32 -> actually be anything if you print hash it will  be a pattern of hash a dollar then it will be  
429.04 -> a pattern of dollar and so on now let's learn  our first basic pattern of increasing triangle  
437.28 -> let's take a previous program which printed a  square we will just edit this to print triangle  
445.28 -> we know now that the outer loop determines the  rows and inner loop determines the columns we  
452.32 -> see that in this pattern the rows are 5 or  n only so our outer loop is correct already  
460.08 -> and requires no change and it is already printing  n rows coming to columns instead of printing  
467.84 -> 5 stars or printing from 0 to 4 it should print  1 star for 0 through 2 star for first row 3 star  
476.96 -> for second row and so on point to note here is  that for each row i need row number plus 1 stars  
486.64 -> row number is stored in variable i which  is starting from 0 and incrementing to 1 2  
493.68 -> 3 till all rows are printed so what we will just  change here is set inner loop to run to i plus 1.  
502.96 -> this will ensure inner loop runs for only  one time in the first row two time in the  
508.72 -> second row then three then four and then n this  completes our program for increasing triangle  
516.48 -> important point to remember in this is that for  making an increasing triangle outer loop is set to  
523.68 -> n and nested loop is set to i plus 1  remaining program is exactly the same  
531.2 -> now let's learn second basic pattern  decreasing triangle we will again take  
536.96 -> our square program we will just edit this to  print decreasing triangle if you see this code  
544.64 -> outer loop is correct already and requires no  change as it is already printing n rows from  
552.08 -> 0 to less than n but now we need the columns to  print decreasing number of stars 5 4 3 2 and 1.  
562.8 -> how do we change our nested for loop to reduce  by 1 every time this time we will add a start  
570.24 -> condition we will use i as the start condition  so for first row the loop will run from 0 to 5  
578.8 -> printing 5 stars second row it will run from 1  to 5 that means 4 times and then 3 2 and 1 star  
588.64 -> this completes our program for decreasing  triangle so important point to remember in  
594.96 -> making decreasing triangle pattern is that outer  loop is still n and nested loop is from i to n  
603.84 -> now you have the basic ammunition in place if  you do not understand these two basic triangles  
610.8 -> go back rewind and understand it again let's take  a look at some of the patterns we saw earlier  
618.96 -> all of the patterns you see here are made from  combination of these two type of triangles as  
626.16 -> you know we always start printing from left side  of the screen so where we see blank is actually  
633.52 -> the same triangle pattern made with spaces  so this pattern is made up of two triangles  
640.16 -> one increasing pattern of space and one  decreasing pattern of star this pattern also  
647.44 -> has two triangles it has one decreasing pattern of  space followed by one increasing pattern of star  
655.92 -> this pattern has three triangles first  it has one decreasing pattern of space  
662 -> followed by one increasing pattern of star and  one more increasing pattern of star also do  
669.68 -> note in all of these here the rows are n only but  columns have different printing options space or  
678.32 -> star so let's try to solve it let's pick up this  pattern first now this pattern has two triangles  
687.44 -> first is decreasing triangle printing space and  second is increasing triangle printing stars  
695.44 -> do note that on same row we have to now print  space and star let's write the program for it  
703.84 -> since our number of rows are same as that of  square pattern we will first copy the outer  
710.24 -> loop from our previous program and it runs till  n to note our empty print line which takes us to  
718.08 -> new row every time sticks to the end of the outer  loop you should not forget it or try to link it  
725.6 -> with any nested loop ever first we have decreasing  triangle of space so we recall our code of nested  
734.32 -> loop for decreasing triangle and we will write  our nested for loop for decreasing triangle from  
741.44 -> i to n here the print statement will print space  only now the next triangle is increasing triangle  
750 -> pattern printing stars in the same row so  we will just add another nested for loop  
756.88 -> for increasing triangle pattern and we know this  will run till i plus one this will print star  
765.2 -> let's take a look at how this will work for the  first row when i is zero first the upper nested  
772.48 -> loop will execute and print 5 spaces then in the  same row it will enter the second nested loop and  
781.44 -> will print 1 star then it will go to next row and  i will become one then it will print four space  
790.4 -> from the first nested loop and then two star from  the second nested loop so these two nested loops  
797.68 -> are working one after the other to print all  columns we need in a row this will go on till  
805.6 -> the entire pattern is printed now this is the  program for this pattern you need to remember two  
813.76 -> important things here first empty print statement  is linked to rows or outer loop we do not repeat  
822.72 -> it with every inner loop second each print has  got either a space or a character followed by  
832.08 -> a space which is part of the end that means two  characters are printed in each print statement  
839.6 -> all of your print statement need to have the same  number of characters even if it is printing space  
848.4 -> now let's see another pattern what if the pattern  was like this it is increasing triangle of space  
856.24 -> and then decreasing triangle of star since our  number of rows are same as that of square pattern  
864 -> we will first copy the same outer loop from the  previous program and it runs till n since first  
871.68 -> loop is increasing triangle we will write our  nested for loop for increasing triangle till i  
878.48 -> plus 1. here the print statement will print space  only now the next triangle is decreasing triangle  
886.8 -> so we will just add another nested for loop for  decreasing triangle pattern which will run from  
893.36 -> i to n this will print start this  is the final code for this pattern  
899.76 -> now what if pattern was like this a hill  pattern now we already have written code for  
905.92 -> first two triangle of this pattern here the third  triangle is also an increasing triangle pattern  
913.44 -> so we will write one more nested loop now there  is a small catch here which you need to remember  
920.64 -> let's run this and see here when you see the  output you see it does not have a peak why  
928.16 -> because our loop knows how to make 5 columns each  in such pattern actually there is one less column  
936.16 -> printed we need to make one of the nested loop  print one less column so to make this nested loop  
944.48 -> run one less we will just change the for condition  of first increasing triangle from range i plus 1  
952.88 -> to range i this will make the loop print one less  column so this is a final code for this pattern  
962 -> now what if it was reverse mountain pattern  here there is one decreasing triangle and two  
968.4 -> increasing triangle now we already have written  code for first two triangle of this pattern  
975.6 -> like in previous question we will just add a  third nested loop for increasing triangle and to  
983.04 -> make sure we have our valley correct we will have  nested loop print 1 less column so we will change  
990.96 -> n to n minus 1 in range function this  is our code for reverse mountain pattern  
999.28 -> let's take a look at patterns which double up  on rows as well let's take this diamond pattern  
1006.48 -> here you have to print pattern below another  pattern now we have the code for hill pattern  
1013.2 -> and inverted hill pattern 2. here note that the  rows are also doubling up so what you need to  
1020.08 -> do is just place two code one below the other  along with the outer loop so you run one outer  
1028.24 -> loop along with its nested loop for rows of upper  set of triangles and run another outer loop along  
1036.48 -> with its nested loop for rows for lower set of  triangles let's run this to see by just doing this  
1044.16 -> do we get the desired pattern this is close  but this too has one issue that it does not  
1050.88 -> have pointed corners looking closely we  see actually our rows two don't double up  
1057.28 -> there is one less row to give us pointed edges  the solution is similar to what we did for columns  
1065.04 -> we need to print one less row how do we do that  we just reduce the condition of outer for loop  
1072.88 -> to print one less row effectively deleting its  last row so we will just change our outer loop  
1080 -> condition from range n to range n minus 1.  now this is our required program for diamond  
1088.56 -> now you can try any different pattern like  this shown using the same logic break it into  
1095.36 -> increasing and decreasing triangles and then just  bundle up the code hope you have understood how to  
1102.48 -> solve the pattern problems in our next video  we will cover how to solve any of the number  
1108.96 -> patterns if you have any doubts do reach out  to us at simplycoding.in thank you and goodbye

Source: https://www.youtube.com/watch?v=fX64q6sYom0