Python Pattern Programs - Printing Stars '*' in Right Angle Triangle Shape
ฝัง
- เผยแพร่เมื่อ 16 ก.ย. 2024
- In this Python Pattern Printing Programs video tutorial you will learn to print star '*' in right angle triangle shape.
To print star pyramid patterns in python you have to use two for loops. Program that prints pattern contains two for loops: the first loop is responsible for rows and the second for loop is responsible for columns.Here we want odd number of star in columns so we used one extra variable k.
If you are using Python 2 then if you write this code you will get error because in python 2 print is not a function and also we cant use end there so you need to use print as statement and instead of end you need to use coma (,) at the end of print statement to get the same output.
in python 3 you write print like this:
print("*',end="")
in python 2 you need to write:
print "*",
For more free tutorials on computer programming
/ amulsacademy
AmulsAcademy
n=int(input("Enter the number of rows"))
for i in range(n):
for j in range(2*i+1):
print("*",end=" ")
print()
This is easier than keeping k=1 and every time appending its value by 2
n=int(input("Enter the number of rows"))
z = int(input("Column Increment : "))
for i in range(n):
for j in range(z*i+1):
print("*",end=" ")
print()
better one , I just made a bit change
The way you are explaining the each step is the most loved thing 🎉❤
Thank you so much
for i in range(1,6):
for j in range(1,(2*i)):
print("*",end=" ")
print()
this is simple.
smart programmer
but why (1,6) for i we only want 3 rows i think its not correct for this particular figure
@@prateekrai5221 just change 6 to 4 ...... It is just a number...... Logic is important not number.....
@@prateekrai5221 this program executed correctly try it
Good👏👏👏
Very good and clear. I like how you explain it step-by-step. Thank you!
My pleasure :)
Your explanation, your voice, your english, and your knowledge of Python...everything is so sweet and perfect 👌🏻 So sweet of You and your work⭐️
Thank you so much 😀
num = int(input('enter any number '))
for row in range(1,num+1,2):
for col in range(1,row+1):
print('*',end=' ')
print()
Amazing 😍
num=int(input("Enter the numbers to print output"))
for i in range(1,num+1):
for j in range(1,2*i):
print("*",end=" ")
print()
This is simple way use this program
with due respect
n=int(input("Enter the number of row:"))
for row in range(1,n+1):
for col in range(1,row):
print("*",end="")
for col in range(1,row+1):
print("*",end="")
print()
thanks for helping me get understanding how loop works.
Welcome :)
So now the "j" value is "tree(3)".... Love the way you pronounce that !!
Haha...thank you :)
@@AmulsAcademy How to reverse this right angled triangle in same manner ? Please help
@@anoop3721 num = int(input("Please enter the number of rows: "))
k = 5
for i in range(num):
for j in range(k):
print("* ", end ="")
k = k - 2
print()
n = eval(input("Please enter a number: "))
or we can use int(input("Please enter a number: "))
for i in range(1,n+1 ,2 ):
for j in range(1,i+1 ):
print("*" , end = " ")
print(" ")
Easer and more performance
Your input number will not be the number of rows then
4 rows input will give 2 rows
I just love thiss.....💌💌💌
"Welcome to python programming tutorials by Amulyas academy"....
Specially "Python" word the way you say....💌💌💌💌💌💌💌💌
Thank you :)
i luv ur voice
Thank you :)
Yes mam, your slang is 🔥🔥🔥
Thanks Amulya's Academy!!!
Here's another way of writing it.
n = int(input("Enter the number of rows..: "))
for i in range(n):
print('* '*(2*i+1))
Happy programming !!!
this method also works----in rows loop if we use (::2)
n=int(input("enter no. of rows:"))
for row in range(1,n+1,2):
for col in range(1,row+1):
print("*",end=" ")
print()
Too much of confusion for this video.. control is here! This for loop😢... That for loop😢
Quite helpful.....And what a pleasant voice of explanation......Thanks alot for the point to point explanation.
Welcome:)
Akka sooperah theliva solli tharinga romba thankyou akka🤝👏👏👏
Superb.... understood well.. thank you ❤
n = int(input("enter the number of rows: "))
for i in range(n):
print("* "*(2*i+1))
please explain the print("* "*(2*i+1))
for x in range(1,6,2):
print('*'*x,'
')
Simple code (And a bit more optimized too!):
for i in range(1, n, 2):
print (i * '*')
Thanks
You can try dis too
for i in range(1,4):
for j in range(1,i*2):
print("*",end="")
print()
num = int(input("Enter the number of rows: "))
for row in range (0, num):
for col in range (0, 2*row+1):
print("*", end=" ")
print()
You are really amazing, the way you explain is just perfect.
I am new to programming and just could easily understand what you are doing.
Thanks a lot.
Glad to help! :)
in simplest way:
num=int(input("Enter Range"))
for i in range(0,num):
for j in range(0,2*i-1):
print("*",end=" ")
print()
Yes :)
But in the output first row is printing empty, so change the range for that.
:)
Very good and very clear explanation madam
for single line run use.
for i in range(num):
print(" "*(row +i)+"1"*(i*2+1)
seen it somewhere in clg days.
This is single line:-
print("
".join(["*"*i for i in range(1,int(input("Enter the number of rows"))+1)]))
Very nice explained step by step
Thank you so much👍🙂
num = int(input("Enter the number of rows : "))
for i in range(1,num+1):
for j in range(1,i*2):
print("*",end = " ")
print()
num = 5
for i in range(1,num+1,2):
print('*'*i)
can't we use this code??
If it is working then you can :)
@@AmulsAcademy it's working for me... thanks a lot... this is really a wonderful series for a beginner 😊☺️
n=int(input('enter a number: '))
for i in range(n):
print('* '*(2*i+1))
You are such an excellent tutor. God bless you.
Awesome explanation mam thank so much you create a good feature for us by this session 👏
n = int(input("Enter the number of rows"))
for i in range(0,n):
print((2*i+1) * "* ")
you are the best thanks
You're welcome!
Очень хороший урок. Авторам большое спасибо.
Continue this series 🙏
Aapko Bahot Sara Thank You
N=int(input(""))
For i in range(0,n,2):
For j in range(0,n):
Print(end="")
Print("s",end="")
Print()
Amulya Shetty madam thank you very much.
Welcome:)
Wow very nyc explain thanks
Pleasure :)
realy helpfull.. thanks
Welcome:)
mam i am getting 9 stars in 3rd row plz help:
num = int(input('Enter the number of rows:'))
k=1
for i in range(1,num+1):
for j in range(1,k+1):
print("*",end=" ")
k=k+2
print()
num = int(input('Enter the number of rows:'))
k=1
for i in range(1,num+1):
for j in range(1,k+1):
print("*",end=" ")
k=k+2 #this should be present outside the j th for loop
print()
very
well explained
Helps a lot, Thanks!
welcome:)
for i in range(1,6,2):
print(i)
Output:
1
3
5
Soooo:
for i in range(1,6,2):
for i in range(i):
print("*",end="")
print()
n=3
for i in range(1,n+1):
stars ="*"*(2*i-1)
print(stars)
Make videos on examples of oops concept in Python ...☺️
I will try :)
@@AmulsAcademy tq..
Thank you mam
Most welcome 😊
can u please comment the same program in equilateral triangle form instead of rightangle triangle
Pyramid shape? Already uploaded.
//simple form to form even no. Of pattern!
num=int(input("enter the number"))
i=1
for i in range(i,num,2)
Print(i*"*")
We can also use while loop to print this pattern 😊
U can do it by multipying too
num=int(input("enter num :"))
k=1
for i in range(0,num):
for j in range(0,k):
print("*",end=" ")
k=k+2
print()
try this one
why we start loop from 1 to num+1
we can also use 0 to num
yes you can :)
Hello, Could you please do some tutorials for "pyspark" ?
Now i can't:)
for i in range(1,7,2):
for j in range(1,i+1,1):
print('*',end=" ")
print()
this is even simpler
def pattern():
num = int(input("Enter your rows: "))
for i in range(1, num+1,2):
for j in range(1, i+1):
print("*", end=" ")
print()
pattern()
N=int(input())
for i in range(1,N+1,2):
print('*'*i)
Very Nice!
useful thanks
The way u r explaining is good.
But the above explained program, when I'm trying to execute I'm not getting the required output.
Give me the program i will check :)
hey im also not getting the reqd output
For my better understanding if i take (k+2) inside "j" loop 9 star is coming of 5 star . How will loop work in this case plz explain??
Plz reply? I am still w8ng
can you give me the program, i will explain:)
num_of_rows = int(input('Enter the number of rows: '))
print()
for i in range(1, num_of_rows * 2 + 1):
for j in range(i, i+1):
if j % 2 == 1:
print('* ' * j)
print()
N = int(input())
for i in range (1,n+1):
print(i * "* ")
This will print above pattern
Is very Simple if u use step-size
n=int(input("Enter Num : "))
k=1
for i in range(n):
print('1'*(k))
k=k+2
Mam...why we are using k variable mam..I couldn't get that point..plz explain
In every row we need odd number of stars that's why we are sung k variable.
You can write this program like this also :)
n = int(input("rows:"))
for i in range(n):
for j in range(0,(2*i)+1):
print("*",end=" ")
print()
@@AmulsAcademy thank you mam
n=int(input())
i=1
if i==1:
print('*')
for i in range(1,n):
print('*' * (2*i+1))
for i in range(1,int(input('> '))+1,2):print('*'*i)
is this logic ok ?
num=int(input(enter the num of rows))
for i in range(0,num)
for j in range (0,2i+1)
print("*" end=" ")
print()
n=int(input())
for i in range(n):
for j in range((2*i)+1):
print("*",end=" ")
print()
rows=int(input("Enter The number of rows: "))
for i in range (1, rows +1, 2):
for j in range ( 1, i+1):
print("*",end="")
print()
please find my way also
Shouldn't the input statement ask for the number of columns ?
Number of column is dependent on number of rows here .
num = int(input("enter the number of rows:"))
for i in range(1, num + 1):
for j in range(1, i + 1):
if i % 2 != 0:
(if i % 2 == 0:
) /// for even numbers
print('*', end="")
print()
simple=>?
no=int(input('enter no of rows'))
for i in range(1,no+1):
for j in range(1,i+1):
if i%2==1:
print('*',end=" ")
print()
for i in range (5):
print('*'+ '*'*2*i)
Invalid code
How to reverse this right angled triangle in same manner (odd number wise) ?
Please help
n=3
for i in range(n+3):
if i==0 or i%2!=0 and i!=1:
for j in range(i+1):
print('*',end='')
print()
Can we write like this?
n = int(input("Enter the number of rows"))
print("
".join(["*"*i for i in range(1,n+1)]))
Thanks
max = int(input( "Enter max no of stars : "))
for i in range (1 , max+1 ):
if (i % 2) != 0:
for j in range (1 , i+1):
print (" * " , end=" ")
print("")
n=500000000000000000000000000000000000000000000000000000000000000000000
for i in range(n):
print("teach very nice")
If we put j in ( 1,2i) I think that it will work too
num = int(input("Enter the number of rows: "))
for i in range(num):
print(i * " * ")
Output:
*
**
***
****
Where I get loop program
for i in range(1, n+1, 2): print('* '*i)
Why K = K+2 outside of inner for loop. Regards
J in range (1,i+i)
This will be working...
for i in range (n):
Print('* '*(i+i+1))
you made a mistake here but i dont know how you got the right output after print statement k=j+2 then you'll get the right answer!!!!
What is the mistake ? :)
@@AmulsAcademy at 2:41 you have written k= k+2 in program but when i tried in pycharm iam getting output only by keeping k=j+2
Tq
rows = int(input('Enter the number of rows: '))
k = 1
for i in range(0, rows):
for j in range(0, k):
print("!",end="")
k = k+2
print()
Code should be readable, please don't make it complicated.
You use third line as rows, but range always take as n+1 you want put there as rows+1
A little tricky for Mee this time 🤨🤨🤨
You can write it in simple way also:)
What is the simplest way mam tell me 🤨🤨
List = [2.5,9,6,'7.2',3,8,5] #can any one sort.
Can I do this
print("* * * * *")
print ("* * * *" )
print("* * *")
print("* *")
print("*")
That's the best things about python that you can these write these kind of pattern in many ways and reduce time and space complexity.
rows = int(input("Enter number of rows: "))
k=1
for i in range(1,rows+1):
print("*" * k)
k=k+2
You can even remove the variable 'k'. print("
".join(["*"*i if i==1 else "*" * (i*2-1) for i in range(1, int(input("Enter number of rows: "))+1)]))
I like her voice
👍👍
A
B A
C B A
D C B A
E D C B A
Ma'am plz print the about pattern in python
We can write like this
n=7
for i in range(1,n+1,2):
for j in range(i):
print("*",end=' ')
print()