ver2事始め

とりあえず下の所からインストール
404 Not Found
現在のver2の最新2.6.2のmsiを落としてインストール。

とりあえずコマンドラインを起動して

print "test"

とか打ってみる。
次はループとか?
無限ループ

#!/usr/bin/python

count = 0
while 1:
	print count
	count = count + 1

ループの終端にマークが無いけどループ処理部分はタブで字下げしてるところになる。
だから次の例だと

#!/usr/bin/python

count = 5 
while count:
	print "loop"
	count = count - 1
	print count
print "loop end" 

これの実行結果はしたになる。

loop
4
loop
3
loop
2
loop
1
loop
0
loop end

わかりやすいのかわかりにくいのかわからない。
次は条件分岐?

#!/usr/bin/python

print "guilty or not guilty"
guilty = raw_input('not or guilty: ')

if guilty == "not":
	print "not guilty"
elif guilty == "guilty":
	print "guilty"
else :
	print "?"

これもC言語の{}はタブ下げ
else ifはエラーなので注意。

最後にsinテーブルを自動生成するスクリプトでも作って見た。
調べながら1時間くらい?

# coding: shift_jis
#!/usr/bin/python

#おまじない
import sys
from math import *

#振幅設定
Amp = raw_input('amplitude: ')
if Amp.isdigit() != True:
	 print "amplitude value error\n"
	 sys.exit()

#offset設定
offset = raw_input('offset: ')
if offset.isdigit() != True:
	 print "offset value error\n"
	 sys.exit()

#要素数設定
index = raw_input('index: ')
if index.isdigit() != True or index < 5:
	 print "index value error\n"
	 sys.exit()

#ファイルネ-ム取得
filename = raw_input('filename: ')
filename = filename + ".c"
print filename

#ファイル作成
file=open(filename, 'w')

#先頭書き出し
file.seek(0)
file.write('const float sin_table[] = {\n')


index_int = int(index)
offset_int = int(offset)
Amp_int = int(Amp)
angle = 360/float(index_int)
print angle
cnt = 0
while index_int:
	#ループ
	rad = pi * (angle * float(cnt))/180.0
	file.write(str(Amp_int * sin(rad) + offset_int)+',\n')
	cnt += 1
	index_int -= 1

file.write('0\n};\n')
#ファイルクローズ
file.close()

相変わらずの汚さ。とりあえず動く?