開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)のまとめ演習6.(ディクショナリのインデクシング)を解いてみる。
その他参考書籍
6.(ディクショナリのインデクシング)
入出力結果(Terminal)
$ python Python 3.3.0 (default, Sep 29 2012, 08:16:08) [GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> d={'a':1,'b':2,'c':3} >>> d {'a': 1, 'c': 3, 'b': 2} >>> d['d'] # 存在しないキーはエラー Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'd' >>> l = [1,2,3,4,5] >>> l[6] # エラー Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> l [1, 2, 3, 4, 5] >>> d['d'] = 4 # 値が代入される >>> d {'a': 1, 'c': 3, 'b': 2, 'd': 4} >>> l[5] = 10 # リストの場合はエラー Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list assignment index out of range >>> quit() $
ちなみにJavaScriptの場合。
コード(BBEdit)
// Pythonの場合と違ってJavaScriptでは範囲外のインデックスを指定すると // undefinedが返される var d = {'a':1,'b':2,'c':3}, a = [1,2,3,4,5], result = d['d'] + "\n" + a[6] + "\n" + a + "\n" + a.length + "\n"; result += a + "\n" + a.length + "\n"; d['d'] = 4; // 範囲外のインデックスを指定して代入すると、その間の要素はundefinedで埋められていく a[10] = 100; result += d['d'] + "\n" + a[10] + "\n" + a + "\n" + (typeof(a[6]) === "undefined"); $('#pre0').text(result);Programming, Python,
0 コメント:
コメントを投稿