開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)のまとめ演習1.(基本的な操作)を解いてみる。
その他参考書籍
1.(基本的な操作)
入出力結果(Terminal)
$ python Python 3.3.0 (default, Sep 29 2012, 08:16:08) [GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 2 ** 16 # 65536 >>> pow(2, 16) 65536 >>> 2 / 5 # 0.4 0.4 >>> 2/5.0 # 0.4 0.4 >>> 2 // 5 # 0 python2から3での変更点 0 >>> "spam" + "eggs" # spameggs 'spameggs' >>> S = "ham" >>> "eggs " + S # eggs ham 'eggs ham' >>> S * 5 # hamhamhamhamham 'hamhamhamhamham' >>> s[:0] # "" 空文字 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 's' is not defined >>> S[:0] # "" 空文字 '' >>> "green %s and %s" % ("eggs", S) # green eggs and ham 'green eggs and ham' >>> "green {0:s} and {1:s}".format("eggs", S) 'green eggs and ham' >>> ('x', )[0] # x 'x' >>> ('x', 'y')[1] # y 'y' >>> L = [1,2,3] + [4,5,6] >>> L # [1,2,3] [1, 2, 3, 4, 5, 6] >>> L # [1,2,3, 4, 5, 6] [1, 2, 3, 4, 5, 6] >>> L[:] # [1,2,3,4,5,6] [1, 2, 3, 4, 5, 6] >>> L[:0] # [] [] >>> L[-2] # 5 5 >>> L[-2:] # [5,6] [5, 6] >>> ([1,2,3] + [4,5,6])[2:4] # [3,4] [3, 4] >>> [L[2], L[3]] # [3,4] [3, 4] >>> L.reverse(); L # [6,5,4,3,2,1] [6, 5, 4, 3, 2, 1] >>> L.sort(); L # [1,2,3,4,5,6] [1, 2, 3, 4, 5, 6] >>> L.index(4) # 5 3 >>> L.index(4) # 3 3 >>> {'a':1, 'b':2}['b'] # 2 2 >>> D = ['x':1,'y':2,'z':3} File "<stdin>", line 1 D = ['x':1,'y':2,'z':3} ^ SyntaxError: invalid syntax >>> D = {'x':1,'y':2,'z':3} >>> D['w'] = 0 >>> D['x'] + D['w'] # 1 1 >>> D[(1,2,3)] = 4 >>> D.keys() # ['x', 'y', 'z', 'w', (1,2,3)] dict_keys(['w', (1, 2, 3), 'z', 'x', 'y']) >>> type(D.keys()) <class 'dict_keys'> >>> D.keys() # dict_keys(['x','y','z','w',(1,2,3)]) (順番はわからない) dict_keys(['w', (1, 2, 3), 'z', 'x', 'y']) >>> type(D.values()) <class 'dict_values'> >>> D.values() # dict_values([1,2,3,0,4]) (順番は分からない) dict_values([0, 4, 3, 1, 2]) >>> D.has_key((1,2,3)) # True 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__', '__dir__', '__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'] >>> # dict.has_keyはpython3.3では無くなったみたい ... # 代わりにinで調べてみる ... (1,2,3) in D.keys() True >>> [[]] # [[]] [[]] >>> ["",[],(),{},None] ['', [], (), {}, None] >>> quit() $
0 コメント:
コメントを投稿