Intro to Python Scripting: 13 Simple Recursion

Video Duration: 
13 minutes
Zach Downey

In this tutorial we take a look at Recursion through a couple of simple examples. Recursion can be a bit confusing at times, so have a cup of coffee ready for this one. Basically, recursive functions are functions that call themselves. Generally, they keep calling themselves until some condition is met, at which point they exit. This is critical, because you can very easily create an infinite loop, which will crash Rhino. Trust me I did it a couple of times while writing the next tutorial. :)

def factorial(n):
    if n == 0:
        return 1
         return n * factorial(n-1)
print factorial(5)
#Fibonacci Sequence
def fib(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    return fib(n-1) + fib(n-2)
for i in range(0,15):
    print fib(i)
import rhinoscriptsyntax as rs
def RecursiveCircle(pt, r):
    if r == 0:
        return 1
        rs.AddCircle(pt, r)
        return RecursiveCircle(pt, r-1)
pt = rs.GetPoint("Pick starting point")
RecursiveCircle(pt, 10)


Please rate this tutorial below. Thanks.

Average: 4.8 (8 votes)


I believe the Fibonacci Sequence should be 0,1,1,2,3,5,8,13,21....

Want to Contribute?

Want to be an author? Drop us a line here we'd love to have you.

Already have a video you'd like to post? Send us a link and we'll get you going.