Python One Line Code To Find Factorial (3 Methods)

In this article, we present you three different methods to find factorial of a given number in one line. First approach use Lambda Expression, second approach use Reduce Function with Lambda Expression and third approach use Reduce Function with Lambda like in second approach but differently.

Method 1: Using Lambda Expression


x = lambda num : 1 if num <= 1 else num*x(num-1)

number = int(input('Enter number: '))

print('%d != %d' %(number, x(number)))

Output

Enter number: 23
23 != 25852016738884976640000

Method 2: Using Reduce Function & Lambda Expression


from functools import reduce

number = int(input('Enter number: '))


factorial = reduce(lambda x, y: x * y, range(1, n+1))

print('%d != %d' %(number, factorial))

Output

Enter number: 13
13 != 6227020800

Method 3: Using Lambda Expression & Reduce


from functools import reduce

number = int(input('Enter number: '))

Factorial = lambda number: reduce(lambda x, y: x * y, range(1, number+1))

print('%d != %d' %(number, Factorial(number)))

Output

Enter number: 17
17 != 355687428096000