Python Pattern Program - Printing Stars in Heart Shape

  Python. Compiler App link


Download



 Write a program printing stars " * " in Printing Stars in Heart Shape program




Explanation :


Hello Guys and welcome to Python programming tutorials by kriisheditz.

So today in this tutorial we are discussing about a pattern program that is we want to

print stars in this shape that is heart shape ok so there are different ways to write a

program so today here we will discuss one way to write the program to print stars in

the heart shape.

if you have any other ideas or way to write this program then don't forget to comment

below.

Alright so let's get started with the today's program, so we want to print stars in the

heart shape the first thing we need to do is, we need to write down the output with

rows and column.

We can see these are the rows here 0th row 1 2 3 4 and 5 ok.

and these are the columns 0 1 2 3 4 5 and 6.

So in my output there are 6 rows and 7 columns.

So in our program first we'll take two for loops because we want to print stars in different

row and column so for that we need two for loop so we'll use nested for loop here, for

loop within the for loop.

Like previous pattern programs, first we will write the for loop for row, inside that we

will write the for loop for column.

ok so in the Python file first i need to write the for loop for row,

So here first i will write for loop for row, here i'll take my variable name as ‘row’

if you want you can change the variable name “in” and i'll use range function here

and inside the range i need to mention how many rows i want in my output, we want 6 rows

right we saw in the output we want 6 rows.

Here it will start from zero so 0 1 2 3 4 5 ok so total 6 rows.

so here i'll mention my range as 0 to 6 ok so that means it will give output as 0 1 2

3 4 5 that's what i want in my output right.

Next inside this i'll take another for loop that is for column alright here “col”

is my variable name and here also i am using range function and here inside the range function

i need to mention how many columns i want in my output, i want 7 columns right, here

we can see 0 1 2 3 4 5 6 that means total 7 columns.

so here i took range as 7, so it will give output as 0 1 2 3 4 5 and 6.

Inside this for loop body i will take if else conditional statement that is because here

i want to print stars in the heart shape so for that we need condition that's why we use

if else conditional statement and here i'll mention “if” keyword after that i need

to mention in which row and column i want to print star ok.

So here i need to mention that , for that we need to see the output so first what i'll

do is, i will divide my output into different parts so that it will be easy to write the

condition ok so this is my first part okay that is row 0, next this row 1 ok.

This is my second part , so here this is my third part and this is my 4th part ok.

so i will divide my output in four parts ok.

So here first we'll see this stars ok we want to print star at row zero and column 1, column

2, column 4, column 5.

row and column, at row 0 i want start at column 1, column 2, column 4 and column 5 right.

i want star in these columns so i don't want star at 0, 3 and 6.

Here observe this if i divide this column value by 3 we will get the reminder as zero.

That’s nothing but if i divide “zero modulus 3”, ok if i check the reminder of this,

it will 0.

“3 modulus 3”,so it is 0.

“6 modulus 3”, it is 0 right.

And here we can see, this 0 3 and 6 are the column values right so here first i will write

my condition as when “row is equal to zero and column modulus 3 is not equal to zero”,

i want to print star.

so ok so this is my first condition that is when “row is equal to equal to zero and

column modulus 3 is not equal to zero”, that is nothing but ok so it will check for

the first condition that is this.

Row 0 column 0, ok so row is equal to zero, so it will go to the “and” condition and

it will check this condition, Whether “column modulus 3” is equal to

0 or not.

Here column value is 0, so so it will check, it is equal to zero right.

so this condition will become false, so it won't print star here.

In the next position here we can see row 0 column 1 right so here it will check,

whether “row is equal to zero”,yes and it will come to the next condition,

so here column value is 1, so “1 modulus 3” whether it is equal to zero, no it is

not right so it is equal to one, so it will print this star at the output okay .

Here we can see at row 0, we don't want star at column 0, column 3 and column 6.

That's why here we mention the condition like this ok, so this is my first condition.

Here i will check when “row is equal to equal to zero and column modulus 3 is not

equal to zero” columns i want star ok this is my first condition.

Here we used logical “and”, because to print star both these condition need to be

true here OK so the first condition is when “row is equal to zero”, i want star at

column 1, 2 and 4, 5 i don't want star at column zero, column 3 and columns 6.

That's why here we wrote the condition like this ok so we are done with the first part.

So next we will move to the next condition, that is row 1.

At row 1 we want star at column 0, column 3 column 6.

It is the exact opposite of row 0 right?, At row 0 we don't want star at column 0, column

3, column 6 . But in row 1, we want star at only column

0 ,column 3 and column 6 that's why i will write the condition like this, or when “row

is equal to equal to one column modulus 3 is equal to equal to zero”.

Because here we want star at 0,3 and 6 right, that's why here “equal to equal to zero”

when it is true i want to print star.

At row 0 i don't want star at 0, 3, 6 that's why here i mention “not equal to” ok and

here “or” is the logical or operator and in the case of logical or operator if any

one of this condition is true it will print star ok that's why here we took or operator

here.

So next we will move on to the next condition, so next we want to print star here.

So to print star here, we will subtract row and column values.

Here we can write this as row and column ok so here we can see this star is at Row 2 and

column 0.

This is at row 3 and column 1.

This is at row 4 and column 2. and this is at row 5 and column 3 right.

So now what i'll do is, to print these 4 stars i will subtract column value from row.

if i do row minus column, here to “2-0” so we'll get 2.

“3 - 1”, 2 “4 - 2”, 2

“5 - 3”, 2 ok. so in the case of these 4 stars, if you subtract

column value from row, then we'll get the result as 2.

So i will write my condition like this when “row minus column is equal to equal to two”,

i want to print star ok so my next condition will be i will check “row minus column is

equal to equal to 2”, ok so this is my third condition so to print these four Stars.

Next ok so next to we want to print these 3 stars right.

So first we will write the position of the stars that is row and column value.

So this star is at row 2 and column 6, So i will write row 2 column 6.

Next this star is at row 3 column 5.

This star is at row 4 and column 4.

OK so next to print these stars what i'll do is, i'll add this row and column value.

So if i add this, we can see 8, “3 + 5”, 8

“4 + 5”, 8.

so if i add row plus column value, it will give same output that is 8.

so to print these three stars i will write my condition like this when “row + column

is equal to equal to 8”, i want to print star.

So it will print these three stars.

So my next condition will be or “row+column is equal to equal to 8” colon here inside

this body if any one of this condition is true i want to print star so star, end is

equal to empty string.

here “end is equal to empty string” is nothing but after printing one star i want

control to be in the same line that's why here i took “end is equal to empty string”,

by default in print function end value will be “\n” that is after printing star it

will go to the next line.

But here we don't want that that's why here we took “end is equal to empty string”,

and here this is my first condition, this is my second condition, this is my third condition

and next is the fourth condition.

Here we used logical or between this condition so if any one of this condition is true it

will print star ok.

So next we will go to the else part, if that condition is not true ok, then i want to print

space in that place ok so space “end is equal to empty string”, if that condition

is not true i want to print space and here also i want to keep the control in the same

line that that's why here i used “end is equal to empty string” ok here we can see

in this places we want space that's why we wrote the else part ok and here we can see

after printing one star i want next star immediately after that right so that's why we use “end

is equal to empty string”.

Here you can write like this ” space , end is equal to empty string” or you can write

end, both are same ok .

Next here inside the first for loop i will use print function, so this is inside the

first for loop.

This print function is for new line that is after printing stars in one row we want to

go to the next line.

Here we can see after printing row 0 we want next row in the new line right that's why

here we used this print function.

So we are done with our code so we need to save this and and run this.

and here we can see stars in the heart shape ok so here we wrote our program in the Python

3.5 but if you're using Python 2 and you want to run this code then you need to do few changes

in the code.

That is so if you are using python 2 then you need to write the code like this, till

here it is same ok condition all are same here you just need to concentrate on print

OK.

Here print is not a function so you need to write like this and we can't use end here

instead of that we can use comma at the end of this statement right.

So if i save this and run this here we can see stars in heart shape ok so that's it for

now thank you for watching don't forget to subscribe to my channel i'll meet you in next

class till then take care.


Video tutorial to this program,




Program :

for row in range(6):

for col in range(7):

if(row==0 and col%3!=0) or (row==1 and col%3==0) or (row-col==2) or (row+col==8):

print("*",end="")

else:

print(" ",end="")

print()


Output :


** **

*  *  *

*     *

 *   *

  * *

   *


[Program finished]



For more program 

click here


Post a Comment

0 Comments