w3resource

Python: Factorial of a number using itertools module

Python Itertools: Exercise-20 with Solution

Write a Python program to find the factorial of a number using the itertools module.

Sample Solution:

Python Code:

import itertools as it
import operator as op

def factorials_nums(n):
    result = list(it.accumulate(it.chain([1], range(1, 1 + n)), op.mul))
    return result;
    
 
print("Factorials of 5 :", factorials_nums(5))
print("Factorials of 9 :", factorials_nums(9))

Sample Output:

Factorials of 5 : [1, 1, 2, 6, 24, 120]
Factorials of 9 : [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program which iterates the integers from 1 to a given number and print "Fizz" for multiples of three, print "Buzz" for multiples of five, print "FizzBuzz" for multiples of both three and five using itertools module.
Next: Write a Python program to find the years where 25th of December be a Sunday between 2000 and 2150.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/itertools/python-itertools-exercise-20.php