Python

Python2.5の文字列リテラル
#シングルもダブルも意味は同じとなるprint 'シングルクウォーテーション' print "ダブルクウォーテーション"print '''トリプルもあるぜ'''print """トリプルもあるぜ"""print '''トリプルもあるぜ''' #トリプルは改行できますprint 'ダブルクウォーテーション""を使える'print 'ダブルクウォ...

Python2.5のリスト
print print ]list = ]print listprint listprint listprint len(list)print list + listprint list * 2list = list.append(4)print list 実行結果 ]245, 0, 1, 2, 3, ], 0, 1, 2, 3, ]

Python2.5のモジュール
モジュールを作る FunctionTestというモジュールを作る。このモジュールにはprintmessageという関数があある。 FunctionTest.py def printmessage(): print "message" FunctionTestMain.py import test.FunctionTest #インポートtest.Fu...

Python2.5のファイル操作
#ファイルの書き込みw = open("./sample.txt", "w")w.write("なんで")w.write("ダイワハウスなんだ\n")list = w.writelines(list)w.close() 実行結果(出力されたsample.txt) なんでダイワハウスなんだなんでダイワハウスなんだ

Python2.5でパッケージインポート
ディレクトリにあわせてインポートする方法。 Javaのパッケージみたいなもの。フォルダで区切ればドット区切りでアクセスができる。 import test.ForTest __init__.py 「__init__.py」ファイルをパッケージのフォルダに置かなければインポートできない。これは全てのフォルダにおかないとだめ。このファイルがあるかどうかが重要なので...

Python2.5のディクショナリ
print {}print {1:1, 2:2}print {"1":"value1", "2":"value2"}print {1:1, 2:{1:1, 2:2}}dic = {1:1, 2:2, 3:3}print dicprint dic.has_key(3)print dic.keys()print dic.values()print len(dic...

Pytho2.5のタプル
タプルは不変なので変更できない。 print ()print (0, 1, 2)print (0, 1, (2.0, 2,1))t = (0, 1, 2)print tprint len(t)print t + tprint t * 3 実行結果 ()(0, 1, 2)(0, 1, (2.0, 2, 1))13(0, 1, 2, 0, 1, 2)(0, 1,...

Python2.5でクラスを作成
class Color: name = None def __init__(self, name): '''コンストラクタ''' self.name = name def getname(self): '''名前を返します。''' return self...

Python2.5のwhile文
whileをつかってみよー cnt = 0while cnt < 5: cnt = cnt + 1 print cnt if cnt == 1: continue elif cnt == 2: pass elif cnt == 3: breakelse: pr...

Python2.5のif文
if文の使い方 value = 100if value < 100: print "value < 100"elif value == 100: print "value == 100"else: print "value > 100" 実行結果 value == 100

Python2.5のFor文
forをつかってみるぜ for num in : print numfor (num1, num2) in ((1,1), (2,2)): print num1 + num2for num in range(3): print numstr = "for"for num in range(len(str)): print strkey...

Pydevのインストールと設定など
clipseで使えるプラグイン。Pythonの実行環境&開発環境を簡単に。 Pydevダウンロード インストール ダウンロードしたものを解凍してEclipseのフォルダにかぶせる。 設定 ウィンドウ>設定でインタプリタにPython.exeを設定する。 気になったこと タブを使うと日本語が化ける場合があるので、半角空白がおすすめ。

Python2.5の実行
とりあえず、実行させて見ましょ。 サンプルファイル Hello.pyファイルを作成する print "Hello Kona" 実行 python.exeに作成したサンプルファイルを引数に与えるべし。 C:\Python25>python D:\Hello.pyHello Kona ほーらできた。 インタプリタで実行させてみる スタートメニューからPyt...