開発環境
- 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 5.(Dictionary keys)を解いてみる。
その他参考書籍
- Pythonチュートリアル 第2版
- Python クックブック 第2版 (原書(最新版))
- Programming Python
- Python Pocket Reference (Pocket Reference (O'Reilly))
5.(Dictionary keys)
コード(BBEdit)
sample5.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
d = {}
d[1] = 'a'
d[2] = 'b'
# immutable object(不変性を持つオブジェクト)をdictionaryのキーとして使える
# number(1, 2)やtuple((1,2,3))はimmutableなので、問題なくdictionaryのキーとして使える
d[(1, 2, 3)] = 'c'
print(d)
入出力結果(Terminal, IPython)
$ ./sample5.py
{1: 'a', 2: 'b', (1, 2, 3): 'c'}
$
0 コメント:
コメントを投稿