Python 2-line-coded pascal’s triangle generator (without using exec/eval)

4 versions
Normal(, 2 liner(, NuclearPasta0’s version(, and triangle version (


Please don’t fall in pain

You need to fork it to change the values, in default it uses the column and line size of terminal to stop pascal generation (depending if the column or lines length of the console is shorter)


If any of you have idea of optimising any of the first 2 version I mentioned at the beginning, please feel free to tell me, it sure helps.

ternary operators FTW!!! btw you don’t need to put import statements on a separate line, you can use __import__("os"), etc

Also try using the values (terminal size limit etc) directly in the code)


Can you give me some example of how to use __import__()?

Also in my code editing thing I can resize the console, and using shutil’s get_terminal_size() helps with that

Awesome, lots of math and formatting and generators, but you can easily make the program a single line in various ways:

  1. Use semicolons and replace the for loop with map and lambda
  2. Use walrus operator, then combine lines into an expression such as a list display, and replace for loop with map and lambda (more readable and no “cheating” semicolons)
  3. there’s probably a lot more

instead of import math, wherever you use the module “math”, import the object with __import__("math").

Here, I’ll provide my version of the nice one liner… give me a sec

No semicolons:

(INFO:=(0, 23, True, False)), (n:=INFO[0]), (end:=INFO[1]), (terminal_size_limit:=INFO[2]), (lines:=INFO[3]), (math:=__import__('math')), (shutil:=__import__('shutil')), (itertools:=__import__('itertools')), list(map(lambda n: exit() if (len(' '.join(list(map(str, list((math.factorial(n) // (math.factorial(r) * math.factorial(n - r))) for r in range(n + 1))))).center(shutil.get_terminal_size().columns)) > shutil.get_terminal_size().columns and terminal_size_limit) or n==(end if lines else shutil.get_terminal_size().lines) else print(' '.join(list(map(str, list((math.factorial(n) // (math.factorial(r) * math.factorial(n - r))) for r in range(n + 1))))).center(shutil.get_terminal_size().columns)), itertools.count(n)))

There’s lots of redundancies and ways to improve this actually, but this is enough for me.

Oh my god it actually works

Yeah, but your code did all the heavy lifting, I just consolidated the rest of the lines with some random python features.


Your code actually taught me something

I could’ve replaced the variable names with its value but it would be a pain to change the value, your one liner actually solved the thing I was worrying about.

Good job also


Guys I have taken advice from the comment in the repl.

I changed some of the code from @NuclearPasta0 and made it into a triangle

Now, as @QwertyQwerty88 wanted, we have a triangle that generated pascal triangle

1 Like

I don’t see any changes yet.

Ah, we meant make the output a triangle, not the code! Currently it’s very hard to see the diagonals and look at what numbers add up to the next row.
You’d need to pad each number to the biggest number’s length. (this will make the triangle very wide though.)
Use this link for a bigger console.

1 Like

Ha! Fun!

I was anticipating something a little different – I expected it to be the whole thing written in one line (which does seem to exist here) but all the options and methods are fun lol

1 Like

One line code can be found in provided by NuclearPasta0

Based on my code structure I may need to rewrite the entire thing tho, I will try that out when I have time!