始めよう

Pythonの長所 Pythonの用途 Pythonと類似ツール

Python プログラムの実行

パスの設定

他のアプリケーションでも一緒だが,コマンドがインストールしてあっても パスが通っていないと使えない.
% which python
no python in /usr/bin /bin /usr/openwin/bin
% set path = (/pub/solaris/python/bin $path)
% which python
/pub/solaris/python/bin/python
% python
[GCC 2.8.1] on sunos5
Type "copyright", "credits" or "license" for more information.
>>> 

対話的に(インタラクティブ)

% python
>>> print 'Hello world!'
Hello world!
>>> 35 + 65
100
>>> x = 35
>>> y = 65
>>> print x + y
100
>>> (Ctrl+D) or (Ctrl+Z) 終了

モジュールファイルの実行

% cat > spam.py
import sys
print sys.argv
(Ctrl+D)
または,テキストエディタやviなどで作成してもよい.
% python spam.py -i eggs -o bacon
['spam.py', '-i', 'eggs', '-o', 'bacon']

スクリプトの作成と実行

% cat > brian
#!/pub/solaris/python/bin/python
print 'The Bright Side of Life ...'
(Ctrl+D)
または,テキストエディタやviなどで作成してもよい.
% chmod +x brian
% brian
The Bright Side of Life ...
Python は 行の中に # が現れると,そこより後をコメントとみなすので, python brian でも同じように実行できる.
また,最初の行は #!/usr/bin/env python でもよい.

モジュールファイルについて

モジュールは import できる.
% cat > myfile.py
title = "The Meaning of Life"
(Ctrl+D)
または,テキストエディタやviなどで作成してもよい.
% python
>>> import myfile
>>> print myfile.title
The Meaning of Life
あるいは,
% python
>>> from myfile import title
>>> print title
The Meaning of Life

名前空間

dir 関数

Python の環境設定

Cシェルの path 変数を設定.(または 環境変数 PATH を設定)
~/.cshrc に path を設定しているところがあるので,/pub/solaris/python/bin を 付け加えておこう.
システムによっては教科書のような setenv が必要かもしれない.

GUIテスト

% python
>>> from Tkinter import *
>>> w = Button(text='Hello', command='exit')
>>> w.pack()
>>> w.mainloop()

練習問題