2012年5月23日水曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)のまとめ演習1(基本的な操作)を解いてみる。

1.

入出力結果(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.
>>> 2**16
65536
>>> 2/5,2/5.0
(0.4, 0.4)
>>> ("spam" + "eggs" == "spameggs")
True
>>> S="ham"
>>> S*5
'hamhamhamhamham'
>>> S[:0]==""
True
>>> "green %s and %s" %("eggs",S)
'green eggs and ham'
>>> ('x',)[0] = 'x'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> ('x',)[0] == 'x'
True
>>> ('x','y')[1] == 'y'
True
>>> L=[1,2,3]+[4,5,6]
>>> L==L[:],L[-2]==5,L[-2:]==[5,6]
(True, True, True)
>>> ([1,2,3]+[4,5,6])[2:4]==[3,4]
True
>>> [L[2],L[3]]==[3,4]
True
>>> L.reverse()
>>> L==[6,5,4,3,2,1]
True
>>> L.sort() == [1,2,3,4,5,6]
False
>>> L
[1, 2, 3, 4, 5, 6]
>>> L==[1,2,3,4,5,6]
True
>>> L.reverse()
>>> L.sort()
>>> L
[1, 2, 3, 4, 5, 6]
>>> L.index(4)==5
False
>>> L.index(4)
3
>>> L
[1, 2, 3, 4, 5, 6]
>>> {'a':1,'b':2}['b']==2
True
>>> D={'x':1,'y':2,'z':3}
>>> D['w']=0
>>> (D['x']+D['w'])==1
True
>>> D[(1,2,3)]=4
>>> D.keys()
dict_keys(['y', 'x', 'z', 'w', (1, 2, 3)])
>>> D.values()
dict_values([2, 1, 3, 0, 4])
>>> print(D.values())
dict_values([2, 1, 3, 0, 4])
>>> D.has_key((1,2,3)]
  File "<stdin>", line 1
    D.has_key((1,2,3)]
                     ^
SyntaxError: invalid syntax
>>> D.has_key((1,2,3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'has_key'
>>> dir(D)
['__class__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>> D.fromkeys()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fromkeys expected at least 1 arguments, got 0
>>> D.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
>>> D
{'y': 2, 'x': 1, 'z': 3, 'w': 0, (1, 2, 3): 4}
>>> D.get()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: get expected at least 1 arguments, got 0
>>> D.get((1,2,3))
4
>>> D.items()
dict_items([('y', 2), ('x', 1), ('z', 3), ('w', 0), ((1, 2, 3), 4)])
>>> [[]],["",[],(),{},None]
([[]], ['', [], (), {}, None])
>>> [""]
['']
>>> quit()
$

0 コメント:

コメントを投稿