Python One Line Code To Find Factorial (3 Methods)

In python, we can calculate factorial of a given number in one line. See example below:

Method 1


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


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


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