Pascal's Triangle in Python (power of 11)
pascal to newton
I wanted to write a Python program that prints out the Pascal Triangle for the inputted number of rows. My code is as follows:
inc = int(input('Input number of rows: ')) n = 0 row =  while n <= inc: m = 11 ** n row.append(m) n += 1 for i in range(0, len(row)): row[i] = str(row[i]) result = '\n'.join(row) print(result)
My Output is:
1 11 121
I wanted to separate each symbol in each line so it would look like a Pascal's triangle. How do I implement that?
You are pretty close, you can just
str.center each line:
inc = int(input('Input number of rows: ')) n = 0 row =  while n < inc: m = 11 ** n row.append(m) n += 1 for i in range(0, len(row)): row[i] = ' '.join(list(str(row[i]))) for i in range(0, len(row)): row[i] = row[i].center(len(row[-1]),' ') result = '\n'.join(row) print(result)
Input number of rows: 4 1 1 1 1 2 1 1 3 3 1
A shorter version would be:
inc = int(input('Input number of rows: ')) max_len = 2 * len(str(11**inc)) - 1 row = (' '.join([*str(11**p)]).center(max_len,' ') for p in range(inc)) print(*row, sep='\n')
Input number of rows: 4 1 1 1 1 2 1 1 3 3 1
Pascal (unit), Pascal, Pascal's or PASCAL may refer to: Contents. 1 People and fictional characters; 2 Places; 3 Science and technology; 4 Other uses; 5 See also. People and� Pascal’s life to the Port-Royal years. Pascal’s father, Étienne Pascal, was presiding judge of the tax court at Clermont-Ferrand.His mother died in 1626, and in 1631 the family moved to Paris. Étienne, who was respected as a mathematician, devoted himself henceforth to the education of his children.
You can indent each line by
len(row) - i and then space each character so that each line is twice as long to balance out the otherwise right alignment.
inc = int(input('Input number of rows: ')) n = 0 row =  while n <= inc: m = 11 ** n row.append(m) n += 1 for i in range(0, len(row)): indent = " " * (len(row) - i) spaced_row = " ".join(list(str(row[i]))) row[i] = indent + spaced_row result = '\n'.join(row) print(result)
Input number of rows: 3 1 1 1 1 2 1 1 3 3 1
Pascal, Article Contents. Blaise Pascal, (born June 19, 1623, Clermont-Ferrand, France—died August 19, 1662, Paris), French mathematician, physicist, religious philosopher, and master of prose. Pascal definition is - a unit of pressure in the meter-kilogram-second system equivalent to one newton per square meter.
This is much simpler:
ROWS = 5 def row( a, b ) : return ' ' * (b-a-1) + ' '.join([i for i in str(11**a)]) for i in range(ROWS) : print row( i, ROWS )
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
and if you want this to work in python3, add parentheses in
print( .. ) call.
Blaise Pascal, Pascal's father was an accomplished mathematician, and he provided the only formal education that his son enjoyed. As Carraud (1992: Chapter� Pascal definition, the standard unit of pressure or stress in the International System of Units (SI), equal to one newton per square meter. Abbreviation: Pa See more.
Blaise Pascal | Biography, Facts, & Inventions, Blaise Pascal was a French mathematician, physicist and religious philosopher who laid the foundation for the modern theory of probabilities. Particularly of interest to Pascal was a work of Desargues on conic sections.Following Desargues' thinking, the 16-year-old Pascal produced, as a means of proof, a short treatise on what was called the "Mystic Hexagram", Essai pour les coniques ("Essay on Conics") and sent it—his first serious work of mathematics—to Père Mersenne in Paris; it is known still today as Pascal's theorem.
Blaise Pascal (Stanford Encyclopedia of Philosophy), Free Pascal is a mature, versatile, open source Pascal compiler. It can target many processor architectures: Intel x86 (16 and 32 bit), AMD64/x86-64, PowerPC ,� The beginning of Pascal’s education was geared toward languages, especially Latin and Greek. Even so, Etienne's plan backfired: The fact that mathematics was a forbidden topic made the subject
Blaise Pascal - Education, Pens�es & Religion, Blaise Pascal was a French philosopher, mathematician, scientist, inventor, and theologian. In mathematics, he was an early pioneer in the fields of game theory � Pascal is Rapunzel's pet chameleon and a major character in Disney's 2010 animated feature film, Tangled. As an infant, a small and defenseless Pascal would remain by his mother's loving side at all times. On a dark and stormy night, the two chameleons were attacked by a venomous snake. Though Pascal's mother was killed while fending off the snake, the sacrifice allowed Pascal time to reach
- Okay, this method works only for n <= 4. Thank you all for your answers, they helped me out!
- why do you print 4 rows, when input is 3 ??
- I just tried to follow OP's algo,
while n <= inc:, but you are right, I should change it.