2014年2月14日金曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART II.(Types and Operations)、CHAPTER 9(Tuples, Files, and Everything Else)、Test Your Knowledge: Quiz 1, 2, 3, 4, 5, 6を解いてみる。

その他参考書籍

Test Your Knowledge: Quiz 1, 2, 3, 4, 5, 6

入出力結果(Terminal)

$ python3
Python 3.3.3 (default, Dec  2 2013, 01:40:21) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> t = (1,2,3,4,5)
>>> len(t)
5
>>> t=(4,5,6)
>>> t=(1,) + t[1:]
>>> t
(1, 5, 6)
>>> f = open('sample.py') # defaultではread mode
>>> f.read()
"#!/usr/bin/env python3\n#-*- coding: utf-8 -*-\n\n# 順序は定義した場合と同様になるとは限らない\nd = {'a':1, 'b':2, 'c':3, 'd':4}\nprint(d)\nd['a'] = 10\nprint(d)\n\n# keyが一意的になってないとどうなるか確認\nd1 = {'a':1, 'b':2, 'a':10}\nd2 = {'a':10, 'b':2, 'a':1}\nprint(d1)\nprint(d2)\n\n# 結果から推測すると、一番右のが優先されて上書きされるみたい"
>>> f.write('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable
>>> f.close()
>>> f=open('temp.dat', 'wb')
>>> pickle.dump({'a':1, 'b':2}, f)
>>> f.close()
>>> f=open('temp.dat', 'rb')
>>> pickle.load(f)
{'b': 2, 'a': 1}
>>> f.close()
>>> a=[1,[2,3],4,5]
>>> b = a
>>> a[1][0]=10
>>> a
[1, [10, 3], 4, 5]
>>> b
[1, [10, 3], 4, 5]
>>> import copy
>>> b = copy.deepcopy(a)
>>> a[1][0]=100
>>> a
[1, [100, 3], 4, 5]
>>> b
[1, [10, 3], 4, 5]
>>> # False以外はTrue。Falseになるのは空や0、None
... for x in ["", [], (), {}, set(), 0, None]:
...     print(bool(x))
... 
False
False
False
False
False
False
False
>>> quit()
$

0 コメント:

コメントを投稿