python 1200 examples - [13] Calculating factorial

Factorial is a mathematical concept represented by n! (pronounced factorial of n), which represents the product of all positive integers from 1 to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.
Insert image description here

In Python, we can use several methods to calculate factorials. Here are some of the methods:

Method 1: Use a loop

This is the most basic method, we multiply from 1 to n through a loop.

def factorial(n):  
    result = 1  
    for i in range(1, n+

Guess you like

Origin blog.csdn.net/m0_47867638/article/details/135290385