2014年2月21日金曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART II.(Types and Operations)、Test Your Knowledge: Part II Exercises、7-a, b, c, d.(Generic operations)を解いてみる。

その他参考書籍

7-a, b, c, d.(Generic operations)

入出力結果(Terminal)

$ python3
Python 3.3.4 (default, Feb 10 2014, 22:06:52) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> # a. いずれもエラー
... 'python' + [1,2]
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: Can't convert 'list' object to str implicitly
>>> [1,2] + (1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
>>> {'a':1,'b':2} + {'c':3,'d':4}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>> # append methodが使えるのはリストのみで文字列では使えない
... l = [1,2]
>>> l.append(3)
>>> l
[1, 2, 3]
>>> s = 'python'
>>> s.append('a') # エラー
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'
>>> [1,2].keys() # エラー
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'keys'
>>> # リストならリスト、文字列なら文字列が得られる
... [1,2] + [3,4]
[1, 2, 3, 4]
>>> "spam" + "egg"
'spamegg'
>>> [1,2][:]
[1, 2]
>>> 'spam'[:]
'spam'
>>> quit()
$

0 コメント:

コメントを投稿