Write a Python function that prints out the first n rows of Pascal's triangle


Introduction

Write a Python function that prints out the first n rows of Pascal's triangle. I have used python 3.7 compiler for debugging purpose.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. def pascal_triangle(num):
  2. trow = [1]
  3. y = [0]
  4. for x in range(max(num,0)):
  5. print(trow)
  6. trow=[l+r for l,r in zip(trow+y, y+trow)]
  7. return num>=1
  8. pascal_triangle(5)

Result

python language techstudy.org

     





Related Topics




Popular Posts