開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のII部(ビルトインオブジェクト)のまとめ演習1.(基本的な操作)を解いてみる。
その他参考書籍
1.(基本的な操作)
入出力結果(Terminal)
$ python Python 3.3.1 (default, Apr 6 2013, 12:29:18) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 2 ** 16 65536 >>> 2 //5 , 2 / 5 # 0, 0.4 (0, 0.4) >>> "spamm" + "eggs" # spameggs 'spammeggs' >>> S="ham" >>> "eggs " + S # "eggs ham" 'eggs ham' >>> S * 5 # 'hamhamhamhamham' 'hamhamhamhamham' >>> S[:0] # '' '' >>> "green {0} and {1}".format("eggs", S) 'green eggs and ham' >>> ('x',)[0] # 'x' 'x' >>> ('x', 'y')[1] # 'y' 'y' >>> L = [1,2,3]+[4,5,6] >>> L, L[:], L[:0], L[-2], L[-2:] # [1,2,3,4,5,6], [1,2,3,4,5,6], [], 5, [5,6] ([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [], 5, [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) # 3 3 >>> {'a':1,'b':2}['b'] # 2 2 >>> D={'x':1,'y':2,'z':3} >>> D['w']=0 >>> D['x']+D['w'] # 1 1 >>> D[(1,2,3)] = 4 >>> D.keys() dict_keys(['w', (1, 2, 3), 'z', 'y', 'x']) >>> D.values() dict_values([0, 4, 3, 2, 1]) >>> 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'] >>> help(D.get) >>> (1,2,3) in D True >>> D.get((1,2,3)) 4 >>> [[]] #[[]] [[]] >>> ["", [], (), {}, None] ['', [], (), {}, None] >>> quit() $
0 コメント:
コメントを投稿