Python

Pythonとは

オブジェクト指向のスクリプト言語である。
(Pythonの名前は「モンティパイソン」からきている)

Python の使用例

Pythonはオブジェクト指向のスクリプトなので、クラスを作成しないと本格的なプログラミングはできないが、ここでは簡単な例で雰囲気だけをつかんで欲しい。

Python の簡単な使い方 その1

コマンドライでの実行方法。

# python -c "print 'Hello World'"     <- おなじみHello World
Hello World
#

同じHello Worldのウインドウ版。
以下のプログラムをhello.pyという名前で作る。

from Tkinter import *
widget = Label(None, text='Hello World')
widget.pack()
widget.mainloop()

# python hello.py     <- Hello Worldが新しいウインドウの中に表示される
#

Python の簡単な使い方 その2

データファイル中の数値の合計を出力するスクリプト。

import sys
import string
total = 0
for line in sys.stdin.readlines():
	for str in line:
		num = string.split(str)
		for n in num:
			total = total + string.atoi(n)
print total

#  cat data    <- サンプルデータ
1 2 3
4 5 6
7 8 9
#  python total.py < data    <- 実行
45
#

Python の簡単な使い方 その3

http://www2.gol.com/users/abetmhr/index.htmlを取得するスクリプト。

import sys
import socket
import httplib
h = httplib.HTTP('www2.gol.com')
h.putrequest('GET', '/users/abetmhr/index.html')
h.putheader('Accept', 'text/html')
h.putheader('Accept', 'text/plain')
h.endheaders()
h.getreply()
f = h.getfile()
print f.read()

#  python gethtml.py    <- 実行
ここにhttp://www2.gol.com/users/abetmhr/index.htmlのデータが出力される
#

参考


Prev | Next
Home | Contents
abe@injapan.net