ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Easy Guide to Python Recursion

    Developer Discussion
    python python 3 recursion
    1
    2
    1.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      scottalanmiller
      last edited by scottalanmiller

      I like this page for teaching basic recursion using Python 3.

      http://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python_3/Recursion

      1 Reply Last reply Reply Quote 0
      • S
        scottalanmiller
        last edited by

        The most common sample of recursion is always doing factorials, of course. So here it is in Python.

        def main():
            num = int(input("Please enter a non-negative integer.\n"))
            fact = factorial(num)
            print("The factorial of", num, "is", fact)
        
        def factorial(num):
            if num == 0:
                return 1
            else:
                return num * factorial(num - 1)
        
        main()
        
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post