2012年5月21日月曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)の8章(リストとディクショナリ)の練習問題を解いてみる。

全問対話型コマンドラインで解答。

入出力結果(Terminal)

$ python
Python 3.2.3 (default, Apr 18 2012, 20:17:30) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> l=[0 for i in rang(5)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'rang' is not defined
>>> l=[0 for i in range(5)]
>>> l
[0, 0, 0, 0, 0]
>>> a=[0]*5
>>> 
>>> a
[0, 0, 0, 0, 0]
>>> d={'a':0,'b':0}
>>> a
[0, 0, 0, 0, 0]
>>> d
{'a': 0, 'b': 0}
>>> b['a']=0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> b={}
>>> b['a']=0
>>> b['b']=0
>>> b
{'a': 0, 'b': 0}
>>> a=[1,2,3,4,5]
>>> b=[1,2,3,4,5]
>>> a[0]=0
>>> a
[0, 2, 3, 4, 5]
>>> a.sort()
>>> a
[0, 2, 3, 4, 5]
>>> a.reverse()
>>> a
[5, 4, 3, 2, 0]
>>> a.sort()
>>> a
[0, 2, 3, 4, 5]
>>> a += b
>>> a
[0, 2, 3, 4, 5, 1, 2, 3, 4, 5]
>>> a.append(100)
>>> a
[0, 2, 3, 4, 5, 1, 2, 3, 4, 5, 100]
>>> a={'a':1,'b':2}
>>> b={'c':3,'d':4,'e':5}
>>> a['a']=0
>>> a
{'a': 0, 'b': 2}
>>> a.update(b)
>>> a
{'a': 0, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
>>> del a[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 0
>>> a['a']
0
>>> del a['a']
>>> a
{'c': 3, 'b': 2, 'e': 5, 'd': 4}
>>> a.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: pop expected at least 1 arguments, got 0
>>> a.pop('c')
3
>>> a
{'b': 2, 'e': 5, 'd': 4}
>>> a['z']=100
>>> a
{'b': 2, 'e': 5, 'd': 4, 'z': 100}
>>> quit()
$

0 コメント:

コメントを投稿