Number Pattern | Part 1 | Python Pattern Programs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys and welcome to python programming tutorials by amulya's academy today in this tutorial we'll write a python program to print number pattern that is will print numbers in different shape like pyramid shape triangle shape etc like string we can arrange the number in different shape that's why we'll discuss many patterns today the first pattern of today's tutorial is this we want to print numbers in the right triangle shape in the first row we want one and in the second row we want one and two in the third row we want three number one two three and in the fourth row we want one two three four and fifth row we want one two three four five like that and to print these patterns i'll use nested for loop method so without talking much let's write the program in the python file first i'll ask the user to enter how many number of row he want or she want in the output so we'll ask the user input for the number of row and the number of column is dependent on the number of rows so no need to ask for the number of columns of the output we need input for number of rows how many rows we want in the output so to take the input i'll use a variable n and i'll use input function here and also here i'll write a message can write any suitable message here that message will display on the output screen and here i'm using python 3 to write the program in python 3 by default this input function will take input as the string value but here we want number of row which is an integer value that's why we need to convert this input to int using int function like this now we are done with the input we are taking the number of row that is an integer value and we are storing that in variable n right after taking the input next we will use nested for loop as i said we'll use nested for loop method to print this patterns so here we'll take the for loop first for loop will be for row inside that we'll take the for loop for column that is because in this type of pattern we'll print the numbers or stars like this we'll begin with the row one and we'll print star or number whatever required will print in the all the column of row zero or the first row after completing the first row we'll go to the second row and we'll print star or number whatever we want in the required column all the columns after completing all the columns of second row we'll go to the row 3 that's why we need to take the outer for loop as the follow for row inside that we need to take the for loop for column so here i and j are the variables which are representing row and column respectively and in the range function here i need to mention how many times i need to execute the loop body or in other words i need to mention how many rows i want in my output because this for loop is for row here we need to mention how many rows we want in the output we want n rows right we took the input from the user that is how many rows we want in the output and we store that in the variable n the input is stored in the variable n so here i'll take the range as n when i take range as n that means i'm taking range from 0 so range of n is equal to range of 0 to n if n value is 5 i'll get i value as 0 1 2 3 4 total 5 rows but starting from 0 ending at 4 if you want you can take the range from 1 to n plus 1 also that's your wish here we will get this output when n is 5 so here we can see this is i and these are j values here you can see and here you can see i is starting from 0 ending at 4 next we need to take how many columns we want in each row here we can see at row 0 i want 1 column at row 1 i want 2 column at row 2 i want 3 column at row 3 i want 4 column that is nothing but row plus 1 column here we are using i to represent the row so that means we want i plus 1 column in each row that's why here i need to mention i plus 1 how many columns i want in each row i want i plus 1 column that's why i need to mention this here this is the for loop for column and here also i am taking the value from 0 to i plus 1 in the range in the range function if you didn't mention the start value it will take the default value that is 0. next inside this i'll take print function and what i want to print here we want to print numbers right let's see here i want to print one next here one and two here one two three here one two three four here one two three four five that is in column zero i want to print one at column one i want to print two at column two i want to print three at column three i want to print four at column four i want to print five that means i want column plus 1 value when column is 0 i want 1 every row column 0 i want 1 that is nothing but column plus 1 that is nothing but j plus 1 right so here i'll print the value of j plus 1 and also i'll use end parameter here so in the print function the default value of end parameter is slash n after printing any message it will go to the next line but here we can see when we are printing the numbers in a row after printing this i want to print the next column in the same line right that's why i need to change the end value here in the print function i'll take space after printing a number give space right and also after completing one row i need to go to the next line that's why i'm taking this print function inside the first for loop outside the in a for loop so here i'm taking print function because after completing one row like this after completing this i need to go to the next row i want to print the numbers in the next row in the next line right that's why here you need to take this print function now we are done so let's execute this enter the number of row needed so i'll enter 6 and here we can see our output number of row has 12 here you can see the output so this is about the first pattern first you need to take the input that is number of rows we want in the output next we need to use nested for loop we need to check how many rows we want how many columns we want and we need to check which value we want to print and we are done next let's move on to the next pattern so next pattern is this i want to print the numbers like this the shape is same we want to print the numbers in the right triangle shape but here you can see first we want to print one here two one three two one four three two one five four three two one this pattern and this pattern is different you can see order of number is different right here we are printing one to here to one so to get this pattern here we can see in the first row we want one and in the second row that is row 1 we want 2 and 1 x 3 2 1 4 3 2 1 5 4 3 2 1 that is we want numbers in the reverse order right that's why what i'll do is i'll reverse the index of column that is now it is in the increasing order that is starting from 0 next 0 1 0 1 2 0 1 2 3 0 1 2 3 4 right what i'll do is i'll reverse that so here only one index is present so i'll make it as 0 here i'll take this as 1 0 reverse that here we have 0 1 2 right so i'll take it as 2 1 0 here i'll change this to 3 2 1 0 and here i'll take 4 3 2 1 0 i'll reverse the index of the column if i do that then here we can see at row 1 column 1 i want 2 at row 1 column 0 i want 1 that is nothing but column plus 1 j plus 1 value here you can see here at row 2 at column 2 i want 3 that is nothing but 2 plus 1 3 column 1 i want 2 1 plus 1 2 column 0 i want 1 0 plus 1 1 j plus 1 now if we reverse the index of the column we can get this pattern but the question is how to reverse the index of column for that what you need to do is here this is the for loop for the column right here you need to reverse the index for that you need to take previously we took the index from 0 to i plus 1 here we need to reverse that so first i need to take ie till -1 and i need to take step as minus 1 little confusing right so i'll explain you if i take n as phi so initially i value will be 0 because here range n so 0 1 2 3 4 i value will be 0 that is nothing but at row 0 next inside this for loop for j in range i value 0 minus 1 minus 1 so that means it will give j value as 0 so it will print j plus 1 that is nothing but 1 so at row 0 it will print 1 after that it will come out of the loop because here range is over it will go to the next line and again control will go here and i becomes 1 when i becomes 1 here in this range becomes 1 minus 1 minus 1 that is nothing but j value will be one and zero first j value will be one so it will print two after that control will again go here j value will be zero zero plus one one it will print two and one at row one will get two and one okay it will work like that that's why here we need to reverse the index of column now if i execute this and if i take six and here we can see our pattern one two one three two one four three 2 1 like that so for second pattern we need to reverse the index of the column next let's move on to the next pattern so the third pattern is this first i want to print 1 next two two three three three four four four four five five five five so for this the shape is same we want to print the number in the right triangle shape i'll take i value row value so at row zero i want one it row 1 i want 2 every column of a row contains same value here 1 here 2 2 3 3 3 4 4 4 4 5 5 5 5 5 so that means i plus 1 value right here 0 here i want to print 1 when i is 1 we want to print 2 when i is 2 we want to print 3 that is nothing but i plus 1 so in this program if i print the value i plus 1 then we'll get the next pattern if i execute this here you can see here we can see every column of row contains same value that means we need to print here row value and row is represented by i that's why we need to print here i plus 1 here because you are printing the i value that is the row value the index of column doesn't matter if it is reversed or it is in the ascending order descending order that doesn't matter here we can see it is in the descending order reversed order if i take just i plus 1 also 0 to i plus 1 then also it works properly like this so the order of the column doesn't matter here because we are printing the i value row value right that's fine okay so this is about the third pattern so the next pattern is this first here we want 5 next 4 4 3 3 3 2 2 2 2 1 1 1 like this here also we want to print the numbers in the right triangle shape but here first we want to print 5 next 4 3 2 1 so here i'll take i value here also you can see we want the same number in every column of a row here five here four four here three three three two two two one one one that means we want to print i value but here we can see at row zero we want 5 at row 1 we want 4 at row 2 we want 3 at row 3 we want to at row 4 we want 1 and we'll get this pattern when n value is 5 so what i'll do is i'll print n minus i value so when n is phi i is 0 5 minus 0 i'll get 5 when n is 5 i is 1 5 minus 1 so i'll get 4 when i is 2 5 minus 2 will get 3 okay so here you need to print n minus i now if i execute this here you can see the pattern so now we are done with this pattern let's move on to the next pattern here this is our next pattern we want to print 5 next 5 4 5 4 3 5 4 3 2 5 4 3 2 1. so here i'll take i and j value index here i is index whatever you can see in the brown color that is the index so now at i 0 we want 5 and i 1 i want 5 and 4 here you can see the pattern 0th column of every row we want 5 first column of every row we want 4 second column of every row we want 3 third column of every row we want to and at last the fourth column of fourth row we want one so that means we need to do something with the column value right j so let's see how to get this pattern now here if i print n minus j value we'll get this pattern when n is 5 5 minus j so i'll get 5 when j is 0 that is column 0 if i do 5 minus 1 at column 1 i'll get 4 if i do 5 minus 2 when j is 2 i'll get 3 5 minus 3 i'll get 2 5 minus 4 i'll get 1 right so to print this pattern here i need to take n minus j now if i execute this and if i take 5 here you can see the pattern 5 5 4 5 4 3 5 4 3 2 5 4 3 2 1 right so here you need to print n minus j value this is about this pattern let's move on to the next pattern here you can see you need to print five four five three four five two three four five one two three four five this pattern and this pattern is different because here we can see the order of number is different it is 0 1 2 3 4 0th index 0 1 now here what i'll do is here we can see we want numbers in the ascending order right four and five three four five two three four five one two three four five so that's why i'll reverse the index of the column if i reverse the index of column that is if i do this 1 0 this is 2 1 0 3 2 1 0 4 3 2 1 0 i'll reverse the index of column like this o three two one zero if i do that now here you can see we'll get this output when n is phi if i do now n minus j phi minus 0 when column is 0 here at row 0 i'll get 5 here this is column 1 so 5 minus 1 i'll get 4 when i do 5 minus 0 i'll get 5 5 minus 2 i'll get 3 5 minus 1 i'll get 4 5 minus 0 i'll get 5 okay so if i subtract j value now after reversing the column index i'll get the pattern so for this first here we need to reverse the index so here you need to take minus 1 you may ask why i am taking n as minus 1 that is because we want value till 0 we are taking the column value till 0 that's why here you need to take minus 1 in the range function end value is exclusive that's why this is step -1 all right so now if i execute this program and if i take 7 here we can see our pattern all right so here we need to reverse the index of the column next till now we discussed the sixth pattern and in all the pattern shape was same that is we were printing the numbers in the right triangle shape this shape the number arrangement was different now next we'll change the shape we'll repeat the same number arrangement but we'll change the shape now now will print the numbers in this shape first we want to print space after that numbers and the number arrangement is same as the previous button previous six patterns so that means the only changes we need to do is now we need to include space first before printing the numbers we need to print the space to print the space we'll use the for loop and here number of row in this output is 5 that means n value is 5 so when n value is 5 in the first row in the 0th row we want 1 2 3 4 in the first row we want 4 space and here we want 3 space here 2 space here one space hand here zero space at zeroth row we want four space at row one we want three space at row two we want two space at row three we want one space and at row four we want zero space that's why i'll write the formula to print the space n minus i minus 1 i want n minus i minus 1 space in every row so when n value is 5 i value 0 5 minus 1 so i'll get 4 space when i value is 1 n value is 5 5 minus 1 4 4 minus 1 3 space i'll get this space so as i said first we want to print space after that number so here i have all the programs which we discussed till now if i execute this program here you can see this program will print this pattern now what we need to do we need to change the shape of the pattern i don't want to change the number arrangement i want same number arrangement i want 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 that is i'll show you the pattern i want this pattern here we can see number arrangement is same one one two one two three one two three four one two three four five but here first we want space so to do this in this program what i'll do is here before writing the number before writing the for loop for number i'll include the for loop for space and here i'll take i want n minus i minus 1 space and here i'll print space print space here also you need to take end as space or whatever you will take here you need to take the same value for end here now if i execute this pattern and if i take phi here we can see the next pattern so to modify the previous pattern we just need to include a for loop to print space so next let's move on to the next pattern here i have program so this program here we can see if i execute this program it will print this pattern we discuss this program few minutes before now first we want to print space here after that i want to print this pattern that is i want to get pattern like this one two one three two one four three two one five four three one before i want space here so for that in this program i'll include the for loop for space and here i'll print space and is equal to space now if i execute this let me take 5 and here you can see the pattern next i have next program this program will give output like this now we need to change this first we want space after that we want numbers that is like this we want to print this pattern now so for this also we need to do the same thing that is first here you need to take for loop for space same formula and minus i minus 1 here you need to print space and is equal to space now if i execute this and here you can see the pattern next we have next program this program will give us the output as like this six five five four four four three three three two two two one one one like this now we need to change this pattern we want this same number arrangement in the different shape first we want space that is like this five four four three three two two one one one so for this same thing you need to do you need to include the for loop for space save and run here we go so let's take the next program which we discussed few minutes ago this pattern will give like this seven seven six seven six five seven six five four seven six five four three now we need to change the shape right first we need to include for loop for space after that we want these numbers that is like this this shape so for this include follow for space save and execute and i'll give seven and here we can see the pattern you can compare these two pattern and coming to the last pattern so so here we can see this program will give you this pattern now we want to change that we want to change the shape so for that you need to include for loop for space like this and save and execute and here you can see the pattern all right so in this way you can print these patterns number patterns you can arrange this numbers in other shape also that means i'll make more videos on the number pattern so that's it for now guys thank you for watching don't forget to subscribe to my channel i will meet you next class till then take care
Info
Channel: Amulya's Academy
Views: 357,747
Rating: undefined out of 5
Keywords: python, python video tutorials, python programming tutorials, python pattern printing, python pattern programs, python patterns, python pattern tutorial, python design patterns, printing star pattern, triangle shape, pyramid shape, number pattern, number
Id: n1iyraKXykY
Channel Id: undefined
Length: 24min 7sec (1447 seconds)
Published: Mon Dec 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.