開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)のまとめ演習6(ディクショナリのインデクシング)を解いてみる。
その他参考書籍
6.
対話型セッションでの入出力結果(Terminal)
$ python3.3
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'] # KeyError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'd'
>>> L=[1,2,3,4,5]
>>> L[10] # IndexError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> D['d']=4
>>> D
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
>>> L[10]=10 # out of range
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> quit()
$
存在しない変数、キーのディクショナリへの値の代入は上手くいったり、存在しない変数、キーを使用するとエラーになったり、存在しない変数名を使用する規則と存在しないキーを使用する場合の規則は似ている。
0 コメント:
コメントを投稿