A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/python/python_math_factorial_method.htm below:

Python math.factorial() Method

Python math.factorial() Method

The Python math.factorial() method is used to calculate the factorial of a non-negative integer. The factorial of a non-negative integer n, denoted as n!, is the product of all positive integers from 1 to n. Mathematically, the factorial of n is calculated as −

n! = n × (n-1) × (n-2) ×...× 2 × 1

In other words, the factorial of n is the product of all positive integers less than or equal to n. By convention, 0! is defined to be 1.

For example −

Syntax

Following is the basic syntax of the Python math.factorial() method −

math.factorial(n)
Parameters

This method accepts a non-negative integer as a parameter for which the factorial is to be calculated.

Return Value

The method returns an integer value representing the factorial of the given non-negative integer n.

Example 1

In the following example, we are calculating the factorial of 5 using the math.factorial() method −

import math
result = math.factorial(5)
print("The result obtained is:",result) 
Output

The output obtained is as follows −

The result obtained is: 120
Example 2

Here, we are calculating the factorial of 0 using the math.factorial() method −

import math
result = math.factorial(0)
print("The result obtained is:",result) 
Output

Following is the output of the above code −

The result obtained is: 1
Example 3

In this example, we use a variable "n" to store the value "7". We then calculate the factorial of n, denoted as 7! −

import math
n = 7
result = math.factorial(n)
print("The result obtained is:",result) 
Output

We get the output as shown below −

The result obtained is: 5040
Example 4

If we pass a non-negative integer to the factorial() method, it raises a ValueError because it only accepts integral values as an argument −

import math
result = math.factorial(5.5)
print("The result obtained is:",result) 
Output

The result produced is as shown below −

Traceback (most recent call last):
  File "/tmp/main.py", line 2, in <module>
    import user_code
  File "/tmp/user_code.py", line 2, in <module>
    result = math.factorial(5.5)
             ^^^^^^^^^^^^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer

python_maths.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4