2012年3月19日月曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のII部(ビルトインオブジェクト) 9章のまとめ演習3(インデクシング、スライシングとdelステートメント)、4(タプルによる値の代入)、5(ディクショナリのキー)を解いてみる。

3.

下記の対話型コマンドラインにて。

4.

問題のコードを実行した場合、XとYの値が入れ替わる。

5.

ディクショナリのキーには整数、数値、タプルの不変性オブジェクトを使用できるからコードは問題なく実行できる。

確認。

入出力結果(Terminal)

$ python
Python 2.7.2 (default, Feb 12 2012, 23:50:38) 
[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,1,2,3]
>>> L[2]=[]
>>> L
[0, 1, [], 3]
>>> L[2:3]=[]
>>> L
[0, 1, 3]
>>> L=[0,1,2,3]
>>> L
[0, 1, 2, 3]
>>> del L[0]
>>> L
[1, 2, 3]
>>> L[0:]
[1, 2, 3]
>>> L
[1, 2, 3]
>>> del L[0:]
>>> L
[]
>>> L=[0,1,2,3]
>>> L[1:2]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only assign an iterable
>>> X='spam'
>>> Y='eggs'
>>> X, Y=Y, X
>>> X
'eggs'
>>> Y
'spam'
>>> quit()
$

0 コメント:

コメントを投稿