Python Pattern Programs - Printing Stars '*' in Right Angle Triangle Shape | Star Pattern - YouTube

Channel: Amulya's Academy

[0]
Hello guys and welcome to Python programming tutorials by Amuls Academy.
[4]
Today in this tutorial we will discuss about a pattern program, that is we need our output
[11]
in this pattern that is a single star in first line, two star in the second line, 3 star
[18]
in the third line, 4 star in 4th line etc.
[23]
So we'll see how to write a program to get output like this.
[27]
Before going to the program we will see what are the requirements of the program, that
[33]
is first input that is we will ask the user to enter the input that is how many rows he
[39]
want.
[40]
We can see this pattern contains 5 rows but in our program we will ask the user to enter
[46]
the number of rows.
[48]
Second one is for loop, for this program we need two for loops that is we can see here
[55]
rows and columns.
[57]
So for row we need one for loop, for columns we need another for loop, so we need two for
[65]
loop.
[66]
And third is range function in our program we are using range function we already discussed
[73]
about the range function in our tutorial and again I'll explain you.
[79]
And in the range function we will use key as ‘range’ and it takes two arguments
[87]
one is start and one is end and range function is used to print the continuous number that
[94]
is if I want zero to 10 numbers, then I can mention here that is zero and i need to mention
[103]
11 here it's because here zero is included here, and 11 is excluded.
[110]
That is if I want integer from zero to 10, then i need to mention zero and 11 here.
[116]
Ok if you want to see this i will use for loop int range 0 to 11.
[136]
And here we can see if I want 0 to 10 numbers then I need to mention 0 and 11 here and to
[144]
print continuous numbers we will use range function.
[149]
And next last is print function that is, if you type help and print and this is the syntax
[162]
of print function and here we can see end is ‘\n’.
[167]
That is if i use print function here, and I'll write ‘hello’ and I'll separate this,
[178]
next i will write next line that is print “hi” ok.
[186]
Now i will enter.
[187]
We can see “hello” and “hi” in different lines.
[192]
Once it will write this “hello” message to the screen as we see in the print syntax
[197]
end is equal to “\n” and we didn't mention Anything here so by default it will go to
[203]
the next line and it will print the next message and here print “hello”, and I'll change
[216]
this end to space i will separate this and I will use print “hi” ok.
[229]
Now I will enter this, we can see “hello” and “hi” in the same line and it is separated
[235]
by a space.
[236]
it's because we use end as space here ok.
[241]
Now we'll go to our pattern program and first, we need to ask the user to enter the input.
[247]
So I'll use one variable called “num” , and I will ask the user to enter the number
[253]
of rows and it is an integer value, so I'll use int, input function “enter the number
[264]
of rows” ok, now I will use for loop.
[273]
It is first for loop for rows, so for i in range function and i will take the value from
[283]
1, so 1 to num+1.
[287]
As i said in the range function, it is included and this values excluded.
[295]
if the user entered the value 5, then we want value 1 to 5, so i mention num+ 1, so it is
[303]
6 now so it will give the value 1 to 5.
[308]
And here within the for function, I'll use another for and j in range here also I'll
[318]
use 1 and i+1 ok.
[320]
Now i will print, i want to print star”*” right, so i will print star and here, I will
[335]
mention end is equal to space ok.
[344]
Now I will go out of this for loop, and in the first for loop I'll write a print function
[353]
and now I will save this and run this, now enter the number of rows i will enter 5, and
[361]
here we can see 1 2 3 4 and 5 right ok.
[368]
First here we will ask the user to enter the number of rows, so we entered 5 here, so value
[373]
of num is 5 ok, and coming to the for loop range 1 to num+1 so range value is 1 and 6
[383]
ok.
[384]
So it will give 1 2 3 4 and 5 ok.
[392]
Now first i value is 1 here.
[395]
Next it will go to the j th loop and here it's range is 1 to i+1.
[401]
So now here range is 1 to i+1 that is, 2 so it will give 1.
[410]
j value is 1 here.
[412]
So it will go to the print statement and it will print the star and it will take end as
[419]
space and again it will go to the for loop.
[423]
But here in the range function, it will give only one value that is 1 so it is already
[429]
executed so it will come out of this loop, and it will execute this print statement that
[435]
is it will give new line.
[437]
So we got first star and the control goes to the next line now.
[441]
Next here again it will go to the for loop and now i value is 2.
[448]
so i value is 2 now.
[451]
And again it will go to the for loop, because of the i value is 2, here it is 1 to i+1.
[459]
That is 1 to 3 , 1 and 3 . so it will give value 1 and 2.
[467]
now again j value is 1, and it will print this star that is this star, and next it will
[479]
print this space.
[481]
Again it will go to this loop and now j value will be incremented to 2 and it will go to
[489]
this print statement and it will print this star okay.
[496]
After this here range function gives only two value so, after this it will come out
[503]
of this loop and it will print this, so it will go to the next line.
[508]
Like this it will continue the execution and it will print this Pattern.
[514]
ok that's it for now thank you for watching don't forget to subscribe my channel I'll
[517]
meet you next class till then take care .