Printing a pyramid is a very basic exercise after learning
Loop in any programming language.And probably the most
challenging one is Pascal Pyramid.
##
## *
## * *
## * * *
## * * * *
## * * * * *
## * * * * * *
##
## 1
## 2 2
## 3 3 3
## 4 4 4 4
## 5 5 5 5 5
## 6 6 6 6 6 6
In this blog ,we will be discussing about how we can make one such using just 4 lines of code.
Python is a Low-level programming language.It has many easy or rather
user-friendly way of handling strings where as in
C-Language its not so obvious.So lets come to the
topic.
In Python one can add or multiply strings.Like,
"*"*4+"+"
## '****+'
You can also print “Nothing”(!) in Python
" *" #---Printing two space followed by a star
## ' *'
Let’s look into the pyramid again.
##
## *
## * *
## * * *
## * * * *
Now without looking at * ,try to look into the string
*,a space followed by star.Bang!!!
Print n-1 many gaps and *.
Print n-2 many gaps and two times * and
so on.
def pyramid(n):
for i in range(n,0,-1):
print(" "*(i-1)+" *"*(n-i))
pyramid(5)
##
## *
## * *
## * * *
## * * * *