2014年2月13日木曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART II.(Types and Operations)、CHAPTER 8(Lists and Dictionaries)、Test Your Knowledge: Quiz 1, 2, 3, 4, 5, 6を解いてみる。

その他参考書籍

Test Your Knowledge: Quiz 1, 2, 3, 4, 5, 6

入出力結果(Terminal)

$ python3
Python 3.3.3 (default, Dec  2 2013, 01:40:21) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> [0]*5
[0, 0, 0, 0, 0]
>>> [0 for x in range(5)]
[0, 0, 0, 0, 0]
>>> {'a':0, 'b':0}
{'a': 0, 'b': 0}
>>> {x:0 for x in ['a', 'b']}
{'a': 0, 'b': 0}
>>> dict.fromkeys('ab', 0)
{'a': 0, 'b': 0}
>>> dict(zip(['a', 'b'], [0]*2))
{'a': 0, 'b': 0}
>>> l = list(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l.reverse()
>>> l
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> l[0] = 10
>>> l
[10, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> l[1:4] = [100]
>>> l
[10, 100, 5, 4, 3, 2, 1, 0]
>>> l.extend([11, 12])
>>> l
[10, 100, 5, 4, 3, 2, 1, 0, 11, 12]
>>> l.append(10000)
>>> l
[10, 100, 5, 4, 3, 2, 1, 0, 11, 12, 10000]
>>> d = {'a':1, 'b':2}
>>> d
{'a': 1, 'b': 2}
>>> d['a'] = 10
>>> d
{'a': 10, 'b': 2}
>>> d.update({'c':3,'d':4})
>>> d
{'d': 4, 'a': 10, 'b': 2, 'c': 3}
>>> del d['a']
>>> d
{'d': 4, 'b': 2, 'c': 3}
>>> d.pop('d')
4
>>> d
{'b': 2, 'c': 3}
>>> quit()
$

5.

リストは数値によるインデックスでのみ要素の値にアクセス可能なのが、ディクショナリでは、数値以外(不変性を持つオブジェクト)によって、ディクショナリの値にアクセスできることがリストとの大きな違い。

0 コメント:

コメントを投稿