see more from the shoots! NSFW

Python – Recursion using Lambda, Ternary, short and pretty

I found this line of code on in the python lessons recursion comments, this does a math computation of factorial parameter you call it with. Thing about it that really stands out to me is how compact this is and it’s recursion. Recursion gets a bad rap due to being a bit more memory intensive, but I still love this.

# Amazing factorial lambda recursion found on SoloLearn object, lambda, ternary, recursion
fact = lambda x: 1 if x == 1 else x * fact(x - 1) 

# Factorial and how it works below.
#  fact(5) = 120
# 1 * 2 * 3 * 4 * 5

I think it’s sexy AF.

Leave a Comment