In [1]:
# Install fortranmagic with 'pip3 install fortranmagic'
%load_ext fortranmagic
/usr/local/lib/python3.5/dist-packages/fortranmagic.py:147: UserWarning: get_ipython_cache_dir has moved to the IPython.paths module since IPython 4.0.
  self._lib_dir = os.path.join(get_ipython_cache_dir(), 'fortran')
In [2]:
%%fortran
subroutine fib(n, a)
    integer, intent(in) :: n
    real, intent(out) :: a

    integer :: i
    real :: b, tmp

    a = 0
    b = 1
    do i = 1, n
        tmp = b
        b = a
        a = a + tmp
    end do
end subroutine
In [3]:
for i in range(10):
    print(fib(i))
0.0
1.0
1.0
2.0
3.0
5.0
8.0
13.0
21.0
34.0
In [4]:
%timeit fib(100)
195 ns ± 1.27 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)