Title: Lesson 4: Conditionals, recursion and iterations
Course: Basic scripting and programming > Section: Course Content

This lesson introduces the concept of recursion. Furthermore, multiple assignment, the while-statement and the break-statement are introduced.

Objectives:

- learn about recursion
- introduce a number of Python functions and statements

Entrance requirements:

- Lesson 1-3

Required materials:

- Computer with Python
- The course book “Think Python,” chapters 5, 6 and 7

Learning outcomes:

At the end of this lesson the student:

- knows what recursion is
- knows how to get input from the keyboard
- can apply the debugger
- knows about multiple assignment
- knows about the while-statement and the break-statement
- can calculate a square root using Newton's method
- knows what an algorithm is
- and can apply all of the above

Learning activities:

- Read section 5.8 and onwards (recursion)
- Read section 6.5 and onwards (more recursion)
- Read chapter 7 (multiple assignment, while, break, algorithms)

The beginning of chapters 5 and 6 was dealt with in lesson 3!

Tasks:

Recursion is a difficult concept to grasp, and sometimes it truly requires a leap of faith to believe that recursion will work. Don't worry if you don't get it at first. Even though recursion is a very powerful tool, it is only used for a few very specific problems. Most problems can be solved without recursion.

Do the following exercises:

- Exercise 6.2 the Ackermann function A(m, n)
- Exercises 7.1 and 7.2, square roots
- Use the Fibonacci function on page 58 of the book:
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)

                Try to find the maximum Fibonacci number that can be calculated using this function:      fibonacci(20), fibonacci(21), fibonacci(22) and so on...

          - What is the largest number you can give as an argument?
          - What is the reason for this limitation?

SUBMIT all your answers in a PDF document!

Planned time frame for this lesson:

6 hours