• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Factorial Recursion – Using Recursive Function to Find Factorials

Python Factorial Recursion – Using Recursive Function to Find Factorials

February 4, 2022 Leave a Comment

Using Python, we can calculate factorials using recursion. Defining a recursive function to find the factorial of a nonnegative integer in Python can be done in the following code.

def factorial_with_recursion(n):
    if isinstance(n,int) and n >= 0: 
        if n == 0 or n == 1: 
            return 1
        else:
            return n * factorial_with_recursion(n-1)
    else: 
        return "Not valid input"

When working with numbers, one common calculation which is easy to perform in Python is finding the factorial of a number.

We can find the factorial of a number in Python in a number of ways.

One such way is to use recursion to calculate the factorial of a number.

To use recursion, we need to define a base case for our recursive function, and define the recursive step where we will call the recursive function again.

Using Recursion to Calculate Factorial of Number in Python

Finding the factorial of a number using recursion is easy. To calculate the factorial of a number in Python using recursion, we need to define the base case, and then define the recursive step.

The base case for the factorial function is when n is 0 or 1. In that case, we want to return 1. If n is greater than 1, then we will call the function again with n – 1 as the input.

Below is a recursive function for calculating the factorial of a number. I’ve also included some input validation to make sure that the input is a nonnegative integer.

def factorial_with_recursion(n):
    if isinstance(n,int) and n >= 0: 
        if n == 0 or n == 1: 
            return 1
        else:
            return n * factorial_with_recursion(n-1)
    else: 
        return "Not valid input"

print(factorial_with_recursion(3))
print(factorial_with_recursion(5))
print(factorial_with_recursion(8))
print(factorial_with_recursion(12))

#Output:
6
120
40320
479001600

As you can see above, calculating the factorial of a number in Python using a recursive function is pretty straightforward.

Hopefully this article has been useful for you to learn how to use Python to calculate the factorial of a number using recursion.

Other Articles You'll Also Like:

  • 1.  Using Python to Get Queue Size
  • 2.  Replace Values in Dictionary in Python
  • 3.  Creating a List of Zeros in Python
  • 4.  Python Check if Float is a Whole Number
  • 5.  Find Magnitude of Complex Number in Python
  • 6.  Using Python to Add Items to Set
  • 7.  Change Column Name in pandas DataFrame
  • 8.  Python tan – Find Tangent of Number in Radians Using math.tan()
  • 9.  Using Python to Remove Commas from String
  • 10.  Convert String to Integer with int() in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy