2012年5月22日火曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)の9章(タプル、ファイルオブジェクト、その他)の練習問題を解いてみる。

1.

タプルの大きさを知るにはlen関数を使えばいい。

2.

タプルは不変性オブジェクトなので直接変更はできないので、新しいタプルを作成する必要がある。(元のタプルを利用するにはスライシング等を使えばいい。)

3.

open関数のデフォルト処理モードは読み込み(r)。

4.

pickleモジュールを使えばいい。

5.

copyモジュールのdeepcopy関数を使えばいい。

6.

  • 数値は0以外の場合
  • その他のオブジェクトは空ではない場合

7.

III部ではステートメントや構文について学ぶ。

以上のことを対話型コマンドプロンプトで確認。

入出力結果(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.
>>> t=(1,2,3,4,5)
>>> len(t)
5
>>> t=(4,5,6)
>>> t
(4, 5, 6)
>>> t=(1,)+t[1:]
>>> t
(1, 5, 6)
>>> f=open('sample.py')
>>> f.read()
'#!/usr/bin/env python\n#encoding: utf-8\n\nprint("Hello module world!")'
>>> f.write('hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable
>>> f.close()
{'a': {'b': 100}, 'd': 2}
>>> d={'a':1,'b':{'c':2}}
>>> d1=d.copy()
>>> import copy
>>> d2=copy.deepcopy(d)
>>> d['b']['c']=100
>>> d
{'a': 1, 'b': {'c': 100}}
>>> d1
{'a': 1, 'b': {'c': 100}}
>>> d2
{'a': 1, 'b': {'c': 2}}
>>> bool(1)
True
>>> bool(10)
True
>>> bool(0)
False
>>> bool([1,2])
True
>>> bool([])
False
>>> bool({1:0,2:0})
True
>>> bool({})
False
>>> bool(())
False
>>> bool((1,2))
True
>>> quit()
$

0 コメント:

コメントを投稿