One Liner Factorial Python Code

In this tutorial, we are going to show How to write one liner factorial python code. We have added the video tutorial and the source code of the program.

Video Tutorial: One Liner Factorial Python Code

What is Factorial?

A factorial is the product of all the integers from 1 to a given number. For example, the factorial of 5 is 120 (1 x 2 x 3 x 4 x 5).

Source Code

# One Liner Factorial Python Code 

factorial=lambda x:x and x*factorial(x-1) or 1 

print('Factorial of 5 is',factorial(5))

Explanation

The x & x*factorial(x-1) part returns a value as long as x is not zero, but when x is zero, the function returns 1, ending the recursion.

Output

Also Read: Python program to create bmi calculator

Join Now : Learn Python Programming Masterclass

Leave a Reply

Your email address will not be published.