47 of 151 menu

The factorial method of the math module

The factorial method of the math module returns the factorial of a number, i.e. the product of all natural numbers from 1 to the given number. Only a positive number can be passed in the method parameter, otherwise the method will return an error.

Syntax

import math math.factorial(number)

Example

Let's find out the factorial of the number 5:

import math print(math.factorial(5))

Result of code execution:

120

Example

Now let's apply the factorial method to the number -4:

import math print(math.factorial(-4))

Result of code execution:

ValueError: factorial() not defined for negative values

See also

  • method fsum of module math,
    which returns the sum of all the members of a sequence
  • function min,
    which returns the minimum number
  • function max,
    which returns the maximum number
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline