Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,695 questions

57,003 answers

573 users

How to calculate and print the Pascal triangle in Python

1 Answer

0 votes
rows = 6
array = []

for i in range(rows):
    array.append([])
    array[i].append(1)
    for j in range(1, i):
        array[i].append(array[i-1][j-1]+array[i-1][j])
    if rows != 0:
        array[i].append(1)
for i in range(rows):
    print("   "*(rows-i), end=" ", sep=" ")
    for j in range(0, i+1):
        print('{0:5}'.format(array[i][j]), end=" ", sep=" ")
    print()


'''
run:

                       1
                    1     1
                 1     2     1
              1     3     3     1
           1     4     6     4     1
        1     5    10    10     5     1


'''

 



answered Dec 17, 2017 by avibootz

Related questions

1 answer 201 views
2 answers 256 views
1 answer 211 views
1 answer 213 views
1 answer 1,052 views
1 answer 287 views
3 answers 462 views
...