[Fsf Education] IT@School, Writing Text

Arun M arun@freedevelopers.net
25 Oct 2002 09:00:00 +0530


Hey, 

   I've started writing a text book for school :)
Here is the first draft of a chapter on Python(one more section will be added 
introducing 'turtle'). 

Text below is mostly adapted from the LiveWires Python intro.

BTW it is my first attempt.

Do we have better writers ;)

Regards
Arun.
-------------

 Raju is still in the class room. Others have left for the play ground. 
What happened ?

"Raju why dont to go the play ?". "Teacher asked me to find out sum of 
first 100 natural number and only then go to play" said Raju  disappointment.
" I am using calculator still taking time" he added.  
"You can get computers to do these tedious work, i will teach you how to write computer programs. Lets go to lab".

In the lab:

"Here is the program, Raju. You can use it"

sum = 0
for i in range(1,101):
	sum = sum + i
print sum

5050

Raju: "Can you explain how this works ?" 

Python is simple and powerful programming language. You can write the program 
and run it with Python interpreter. You can use python interactive mode where 
it will carryout our instruction as we gave them. Lets use Python prompt. 

Python 2.0 (#4, Dec 12 2000, 19:19:57)
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>>

The >>> is Python way of telling you that you are in interactive mode.

You can type things into that window, and the computer will obey them.
Unfortunately, the computer doesn't understand English. If you type in
>>> Tell me the sum of twelve and thirteen.
it won't understand at all. Instead, because the computer is very stupid, 
you have to talk to it in a special language, designed 
to be easy for the computer to understand. In fact, there are lots of languages designed for computers to understand; the
one we're going to look at is called "Python". One of the good things about Python is that it's pretty easy for humans to understand, too.

Here's how you ask the computer to tell you the sum of twelve and thirteen. Try it yourself. (You don't need to type in the

>>>, but you do need to hit the key marked Enter after typing the line.)

>>> 12+13

>>> 1+2+3+4
10

>>> 1+2*3-4    Use * for multiplication, not x.
3    If you expected 5, think again!

>>> 200*300
60000

>>> 12/4    Use / for division.
3

Now, here's a bit of a surprise.
>>> 7/3

2

You might have expected it to say 2.3333333 or 2 1 , but in fact the remainder just gets thrown away. There are ways of
3
getting a more accurate answer; we'll find out about them later.
Try experimenting some more with using Python as a calculator.


Giving names to things
--------------------

Suppose you know that you're going to need to do a lot of calculations involving the number 123456. (Maybe it's your height in mm, or something.) 
You could just type the number in every time:
>>> 123456*3
370368

>>> 123456/6
20576

>>> 123456-1000
122456

This might get very boring after a while. And if anyone else wanted to read 
what you were doing, they might be confused by the mysterious number 123456 
and wonder why it appeared so often. We can solve either of these problems 
by giving the number a name. To save typing, give it a short name, 
like n (short for "number", maybe). To make it more obvious what it means, 
give it a longer name, like salary. Here's how we do that.
>>> height=123456
>>> print height*4
493824

>>> print height/12
10288

>>> print height
123456

The idea is that, after you've said salary=123456, you can always type height
instead of 123456. What we've called "names", most people call "variables". 

Doing something over and over again
So far, we've done very little that your pocket calculator couldn't do equally well. Here's something your calculator probably isn't so good at. The extra 
spaces on the second line are important, by the way! 
>>> for x in 1,2,3,4,5:
...    print x,x*x    //The prompt changes, to tell you Python is expecting more.
...    Just press Enter .

Can you guess what this will do? . . . If you guessed that it prints out the numbers from 1 to 5 along with their squares, well done. 

Usually computer takes instructions line by line and execute it. 'for' tells 
it to repeat some job. Let see oh it works. 
' for x in 1,2,3,4,5:' means 'variable' x will get value 1,2,3,4,5 one at a 
time in the order. Now see, we have put space in front of 'print' in next
like and it comes under line with 'for'. 'for' will repeat all the instruction 
which are blocked below it.


Ok, now lets come back to our program to add numbers. 

sum = 0
for i in range(1,101):
        sum = sum + i
print sum

First line create name 'sum' and value 0 is put in it.

hmm, there is something new second like !!! What is it. Lets ask computer.

>>> range(1,101)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,100]

oh, it generate numbers from 1 to 100 good that you dont have to type all of
them. So in the second  name 'i' will get value from 1 to 100 and line below 
will be repeated 100 times. 

See 3rd line will add 'i' to name 'sum', ie sum will get increased. finally 
sum will be printed. Which will have sum of all numbers from 1 to 100.

Raju:"Ha this is good. i got the result. Lets go to teacher." 

At Staff Room

Raju:"Teacher i've done the problem given. answer in 5050 can I go now."
Teacher:"Finished this fast ? How did you do it!!"
Raju explains his programs. Teacher:" 

Haha, thats good, you learned 
python programming. But dont think that computers are solution to 
all problems. It cant think like you. It can help you do boring repetitive 
tasks. Let me tell you story of about the young Johann Carl Friedrich Gauss 
(1777-1855) , later a famous mathematician. The class was misbehaving and 
the teacher assigned everyone to find the sum of all the numbers from 
1 to 100 as busy work.

The young Gauss, already brilliant, wrote two lines and added them, 
like this (notice we skip actually writing and adding most of the numbers -
- so did he):

      1 +   2 +   3 + ... + 100
    100 +  99 +  98 + ... +   1 <-- same 100 terms in reverse 
    --------------------------- 
    101 + 101 + 101 + ... + 101 

 Clearly, he reasoned, I'm getting this number 101 in the bottom row 100 
times. But that's from adding all the numbers I need twice (I added the series 
to itself). So the number I really want is 1/2 of 100 x 101 = 10100/2 = 5050.
"

"Raju now you can go and play. You should learn more 'Python'".