"fibonacci-journal" is a transcript of an interactive session of Python.
In a new shell window enter
$ python
Recall that our convention is that the $ sign reminds you that
this is to happen at the shell prompt, and that you have to
press the (enter) or (return) key. Putting the name of key in
parentheses is another convention meaning that you press the key
rather than typing it.
Python replies with some information and the invitation to enter code.
>>> 1+1
2
You entered 1+1 at the >>> prompt and Python replies with the answer,
which is 2, as you may have guessed.
The next conversation introduces some structure
>>>for i in range(10):
The : at the end of the line tells Python to continue asking for more,
... print i
...
After the ... you must indent at least one space for subsequent lines.
An empty line ends the conversation. Python replies with the numbers 0
through 9. "Aha" you say, "where is 10" ?
Python follows the C convention that a range begins with 0 and therefore
ends with one short of the range.
I made made many mistakes writing this example, which I erased from
the journal. But I did leave the one for
>>> print hello
In the beginning, don't take Python's quetching too seriously, examine
what mistake you probably made by yourself. The print command is designed
to print string output of processes. Feeding print the string "hello"
dutifully prints it out as you'd expect. You can play with the print
command, to learn more about it, on your own ticket.
Suppose we want to do something more serious. Like compute the first 10
Fibonacci numbers: 1,1,2,3,5,8,13,21, ... aren't you getting tired of
doing it in your head? What if you want the first 10,000 Fibonacci numbers,
or more prudently, the 10,000-th Fibonacci number only?
We can assign variables a and b to 1 for a start and then keep adding the
previous two to get the next Fibonacci numbers. We test it out by doing
the first step in the loop to get 1,1,2. Then we write a loop of 10 times
adding the previous two and printing the result. And we've made a mistake,
what does 17711 have to do with this? The problem is that the variable a
and b have by now acquired a different value. So we restart at 1, 1 and
behold we get to 144. Bet you didn't know that was the 12th Fibonacci number.
We end today's lesson by defining a function, fib(x,y), but it's not correct.
Before I tell you, can you see why is it incorrect?
Because I'm confusing the working variables x,y,z and the initial variables.
The second definition of fib(x,y) is OK. I've made fib compute "the next 20"
Fibonacci numbers.
As an exercise, build fib(xin, yin, times) where times is the number of
iterations you want. Finally, build fibth(x,y,n) that prints out only
the n-th Fibonacci starting from x and y, including x, and y.
This exercise will have two effects:
1. You're getting used to the interactive mode of Python
2. You're sick and tired of the interactive mode of Python.
Next Lesson concerns the non-interactive, or 'script based' mode of
working with Python.