the meaning of python3.x syntax

SufferProgrammer

I want to make a pascal triangle from python script. and this is my syntax that I get from web.programminghub.io

size = int(input("Enter the size of the triangle: ")) 
tri=[]

#creates a pascal triangle of size n
def pascal(n):
   """Prints out n rows of Pascal's triangle.
   It returns False for failure and True for success."""
   row = [1]
   k = [0]
   for x in range(max(n,0)):
      tri.append(row)
      row=[l+r for l,r in zip(row+k,k+row)]
   return n>=1


#prints the pascal triangle with correct spacing to 
#represent a triangle
def print_pascals_triangle(triangle):
    largest_element = triangle[-1][len(triangle[-1]) // 2]
    element_width = len(str(largest_element))
    def format_row(row):
        return ' '.join([str(element).center(element_width) 
for element in row])
    triangle_width = len(format_row(triangle[-1]))
    for row in triangle:
        print(format_row(row).center(triangle_width)) 
pascal(size)

print_pascals_triangle(tri)

the problem is, no explanation for every single of line from this script code. so can someone explain every line of this syntax meaning.

zjk

The first function pascal generates a pascal's triangle stored in tri. The print_pascals_triangle prints the triangle symmetrically.

Enter the size of the triangle: 3
  1  
 1 1 
1 2 1

pascal generates [[1], [1, 1], [1, 2, 1]].

print_pascals_triangle prints each row centered at a fixed width (triangle_width).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related