開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅱ.(Types and Operations)、Test Your Knowledge: Part II Exercises 6.(Dictionary indexing)を解いてみる。
その他参考書籍
- Pythonチュートリアル 第2版
- Python クックブック 第2版 (原書(最新版))
- Programming Python
- Python Pocket Reference (Pocket Reference (O'Reilly))
6.(Dictionary indexing)
コード(BBEdit)
sample6.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
D = dict(a=1, b=2, c=3)
try:
# KeyError
print(D['d'])
except Exception as err:
print(type(err), err)
D['d'] = 'spam'
# {'a':1, 'b':2, 'c':3, 'd':'spam'} (順序は分からない)
print(D)
入出力結果(Terminal, IPython)
$ ./sample6.py
<class 'KeyError'> 'd'
{'a': 1, 'b': 2, 'c': 3, 'd': 'spam'}
$
0 コメント:
コメントを投稿