Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

factorial recursion python

def fact_rec(n):
	if n < 0:
		return
	elif n <= 1:
		return 1
	else:
		return n*fact_rec(n-1)

answer = fact_rec(4)
if answer == None: 
	print("Cannot calculate factorial of a negative value")
else:
	print(answer)  # 24 = 4x3x2x1 = 4! 
Comment

python recursion factorial

def factorial(n):

    assert type(n) == int, "Invalid input type"
    assert n >= 0, "Input must be non-negative"
    
    if n <= 1:
        return n
    else:
        return n*factorial(n-1)
Comment

recursive factorial python

def recursive_factorial(n: int) -> int:
	if n<=1: return n
    
    return n * factorial(n-1)
Comment

PREVIOUS NEXT
Code Example
Python :: how to print class attributes in python 
Python :: create virtual env pyhton3 
Python :: python sort multiple keys 
Python :: matplotlib savefig cutting off graph 
Python :: highlight null/nan values in pandas table 
Python :: raw input example python 
Python :: smallest number of 3 python 
Python :: how to use %s python 
Python :: Add PostgreSQL Settings in Django 
Python :: gridsearch cv 
Python :: Python Datetime Get year, month, hour, minute, and timestamp 
Python :: cv2 opencv-python imshow while loop 
Python :: python add commas to list 
Python :: PY | websocket - client 
Python :: ploting bargraph with value_counts 
Python :: hex to string python 
Python :: Reason: Worker failed to boot 
Python :: re.match python 
Python :: Python Tkinter PanedWindow Widget 
Python :: jupyter matplotlib 
Python :: matplot lib 3d plot autoscale 
Python :: default values python 
Python :: deep learning with python 
Python :: pronic number program in python 
Python :: django url with string parameter 
Python :: python multithreading 
Python :: python string continue next line 
Python :: marshmallow default value 
Python :: how to remove an element from a list in python 
Python :: append 1 colimn in pandas df 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =