Python Pattern Programs – Printing Stars ‘*’ in Right Angle Triangle Shape | Star Pattern

Hello guys and welcome to Python programming
tutorials by Amuls Academy. Today in this tutorial we will discuss about
a pattern program, that is we need our output in this pattern that is a single star in first
line, two star in the second line, 3 star in the third line, 4 star in 4th line etc. So we’ll see how to write a program to get
output like this. Before going to the program we will see what
are the requirements of the program, that is first input that is we will ask the user
to enter the input that is how many rows he want. We can see this pattern contains 5 rows but
in our program we will ask the user to enter the number of rows. Second one is for loop, for this program we
need two for loops that is we can see here rows and columns. So for row we need one for loop, for columns
we need another for loop, so we need two for loop. And third is range function in our program
we are using range function we already discussed about the range function in our tutorial and
again I’ll explain you. And in the range function we will use key
as ‘range’ and it takes two arguments one is start and one is end and range function
is used to print the continuous number that is if I want zero to 10 numbers, then I can
mention here that is zero and i need to mention 11 here it’s because here zero is included
here, and 11 is excluded. That is if I want integer from zero to 10,
then i need to mention zero and 11 here. Ok if you want to see this i will use for
loop int range 0 to 11. And here we can see if I want 0 to 10 numbers
then I need to mention 0 and 11 here and to print continuous numbers we will use range
function. And next last is print function that is, if
you type help and print and this is the syntax of print function and here we can see end
is ‘n’. That is if i use print function here, and
I’ll write ‘hello’ and I’ll separate this, next i will write next line that is print
“hi” ok. Now i will enter. We can see “hello” and “hi” in different
lines. Once it will write this “hello” message
to the screen as we see in the print syntax end is equal to “n” and we didn’t mention
Anything here so by default it will go to the next line and it will print the next message
and here print “hello”, and I’ll change this end to space i will separate this and
I will use print “hi” ok. Now I will enter this, we can see “hello”
and “hi” in the same line and it is separated by a space. it’s because we use end as space here ok. Now we’ll go to our pattern program and first,
we need to ask the user to enter the input. So I’ll use one variable called “num”
, and I will ask the user to enter the number of rows and it is an integer value, so I’ll
use int, input function “enter the number of rows” ok, now I will use for loop. It is first for loop for rows, so for i in
range function and i will take the value from 1, so 1 to num+1. As i said in the range function, it is included
and this values excluded. if the user entered the value 5, then we want
value 1 to 5, so i mention num+ 1, so it is 6 now so it will give the value 1 to 5. And here within the for function, I’ll use
another for and j in range here also I’ll use 1 and i+1 ok. Now i will print, i want to print star”*”
right, so i will print star and here, I will mention end is equal to space ok. Now I will go out of this for loop, and in
the first for loop I’ll write a print function and now I will save this and run this, now
enter the number of rows i will enter 5, and here we can see 1 2 3 4 and 5 right ok. First here we will ask the user to enter the
number of rows, so we entered 5 here, so value of num is 5 ok, and coming to the for loop
range 1 to num+1 so range value is 1 and 6 ok. So it will give 1 2 3 4 and 5 ok. Now first i value is 1 here. Next it will go to the j th loop and here
it’s range is 1 to i+1. So now here range is 1 to i+1 that is, 2 so
it will give 1. j value is 1 here. So it will go to the print statement and it
will print the star and it will take end as space and again it will go to the for loop. But here in the range function, it will give
only one value that is 1 so it is already executed so it will come out of this loop,
and it will execute this print statement that is it will give new line. So we got first star and the control goes
to the next line now. Next here again it will go to the for loop
and now i value is 2. so i value is 2 now. And again it will go to the for loop, because
of the i value is 2, here it is 1 to i+1. That is 1 to 3 , 1 and 3 .
so it will give value 1 and 2. now again j value is 1, and it will print
this star that is this star, and next it will print this space. Again it will go to this loop and now j value
will be incremented to 2 and it will go to this print statement and it will print this
star okay. After this here range function gives only
two value so, after this it will come out of this loop and it will print this, so it
will go to the next line. Like this it will continue the execution and
it will print this Pattern. ok that’s it for now thank you for watching
don’t forget to subscribe my channel I’ll meet you next class till then take care .


Leave a Reply

Your email address will not be published. Required fields are marked *