Learning to program - The Basics

Simple Sequences

In the most recent versions of Python you can change this behavior to always produce real numbers from a division by adding this line to the top of your program:

>>> from __future__ import division

なんかimportとか使い始めるとはじまったなって感じがする。
整数がうんたらとか式がうんたらってのはPython チュートリアル - Guido van Rossumを既に読んでいて、ある程度までやっているのでクリアできている。結構めんどうくさいんだけどね。

>>>import sys

Now this is a strange one. If you've tried it you'll see that it apparently does nothing. But that's not really true. To understand what happened we need to look at the architecture of Python (for non Python programmers, bear with me there will be a similar mechanism available to you too!)

When you start Python there are a bunch of commands available to you called built-ins, because they are built in to the Python core. However Python can extend the list of commands available by incorporating extension modules. - It's a bit like buying a new tool in your favourite DIY shop and adding it to your toolbox. The tool is the sys part and the import operation puts it into the toolbox.

In fact what this command does is makes available a whole bunch of new 'tools' in the shape of Python commands which are defined in a file called 'sys.py'. This is how Python is extended to do all sorts of clever things that are not built in to the basic system. You can even create your own modules and import and use them, just like the modules provided with Python when you installed it.

So how do we use these new tools?
>>>sys.exit()

Whoops! What happened there? Simply that we executed the exit command defined in the sys module. That command causes Python to exit.
(Note 1: Normally you exit Python by typing the End Of File(EOF) character at the >>> prompt - CTRL-Z on DOS or CTRL-D on Unix. In a development tool you exit using the File->Exit menu etc as usual.)
(Note 2: If you try this inside a development tool like IDLE the tool will probably catch the attempt to exit and display a message saying something about SystemExit. Don't worry that means the program is working and the tool is just saving you the bother of restarting from scratch.)

Notice that exit had 2 brackets after it. That's because exit is a function defined in sys and when we call a Python function we need to supply the parentheses even if there's nothing inside them!

Try typing sys.exit without the brackets. Python responds by telling you that exit is a function rather than by executing it!

One final thing to notice is that the last two commands are actually only useful in combination. That is, to exit from Python other than by typing EOF you need to type:

import sys
sys.exit()

This is a sequence of two commands! Now we're getting closer to real programming....

この辺は結構面白かった。
import sys
sys.exit()

sysっていうmoduleの読み出し(import)をして、そのsysっていうmoduleの中で定義(define)されているexitっていうコマンドを実行しているのねえ。こういうのをmodule自分で作って呼び出せば使えるようになるよってのは凄い。
後exitはsysのなかでfunctionとして定義されているから()が必要なんだってのはわかった。

IDLEのことが書いてあるからIDLEちょっとインストールしてみようかな。

端末からaptitudeってやってSearchしてidle探した。んで、それを入れてみた。idleとの依存関係?はないけど、idleでインストールされるやつに依存関係があるやつが5つぐらいあったみたいだったけどそれはデフォルトではインストールになってなかったんで従うことにした。python debugがどうたらってのは後で入れてみたら面白いのかなと思ったけど、まぁ今は使いこなせまいので。
ちゃんとインストールされてるか不安だったので、aptitude -s install idleとした。変更なしなのでこれでよかった。
端末からidleと打つ。
おk、起動した。
import sys
sys.exit()とやるもなんか失敗。まぁいいか。んまぁ、Note2に書いてあるようにSystem Exitと書いてあるからいいんだろう。一からやりなおして困らせることのないようにこういう表示にしているんだっていうことか。よしよしよし。

おっしゃー、とりあえずJavascriptVBも完璧やったぜ。辞書を見ながらなのですげえ時間掛かった。とくにJavascriptのASCIIコード出すやつなんかjavastriptみたいに書いててなにもエラーでなかったので一回コピペしたりして大変だったぜ。