Home > Backend Development > Python Tutorial > How to implement star arrangement in python

How to implement star arrangement in python

爱喝马黛茶的安东尼
Release: 2019-06-25 14:01:55
Original
7162 people have browsed it

How to implement star arrangement in python

#How to implement star arrangement in python? Here are several formations for you.

Star formation 1

def stars1(n):
for i in range(1,n+1):
str = "*"*i
print str
if __name__ ==  "__main__":
stars1(5)
Copy after login

How to implement star arrangement in python

Star formation 2

def stars2(n):
for i in range(1,n+1):
str = '*'*(n+1-i)
print str
if __name__ ==  "__main__":
stars2(5)
Copy after login

How to implement star arrangement in python

Related recommendations: "Python Video Tutorial"

Star Formation 3

def stars3(n):
for i in range(1,n+1):
str = ' ' * (n-i) + '*' * i
print str
if __name__ ==  "__main__":
stars3(5)
Copy after login

How to implement star arrangement in python

Star Formation 4

def stars4(n):
for i in range(1,1+n):#total n line(s)
str = ' '*(i-1) + '*'*(n+1-i)
print str
if __name__ ==  "__main__":
stars4(5)
Copy after login

How to implement star arrangement in python

5. Diamond formation

def stars5(n):
    RANGE1 = [2*i+1 for i in range(n)]
    RANGE2 = [2*i+1 for i in range(n)[::-1]][1:]
    RANGE = RANGE1 + RANGE2
    RANGE_1 = [i for i in range(n)[::-1]]
    RANGE_2 = [i for i in range(n)[1:]]
    RANGE_12 = RANGE_1 + RANGE_2
    for i in range(len(RANGE)):
        print ' '*RANGE_12[i] + '*'*RANGE[i]
    
if __name__ ==  "__main__":
stars5(5)
Copy after login

How to implement star arrangement in python

6. Hourglass formation

def star6(n):
    a = [i*2+1 for i in range(n)[::-1]]
    b = [i*2+1 for i in range(n)][1:]
    aa = a + b
    
    d = [i for i in range(n)]
    e = [i for i in range(n)[::-1]][1:]
    dd = d + e
    
    for j in range(len(dd)):
        print ' '*dd[j] + '*'*aa[j]   
         
if __name__ == "__main__":
    star6(5)
Copy after login

How to implement star arrangement in python

The above is the detailed content of How to implement star arrangement in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template