開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)の5章(数値)の練習問題を解いてみる。
1.
14
2.
10
3.
14.
4.
ある数の平方根を求めるにはmathモジュールのsqrt()関数、平方を求めるにはビルトイン関数のpow()関数を使えばいい。
5.
問題の演算の結果はfloat型。
6.
浮動小数点数の切り捨てを行うにはint関数、丸めを行うにはround関数を使えばいい。
7.
整数を浮動小数点数に変換するにはfloat関数を使えばいい。
8.
整数を8進数で表示するにはoct()関数、16進数で表示するにはhex()関数を使用すればいい。
9.
8進数、16進数を通常の整数に変換するにはint()関数を使用すればいい。
入出力結果(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*(3+4) 14 >>> 2*3+4 10 >>> 2+3*4 14 >>> n=4 >>> import math >>> math.sqrt(n) 2.0 >>> pow(n,2) 16 >>> 1+2.0+3 6.0 >>> a=1.2 >>> int(a) 1 >>> int(1.7) 1 >>> round(1.2) 1 >>> round(1.5) 2 >>> float(10) 10.0 >>> n=1 >>> n=16 >>> oct(n) '0o20' >>> hex(n) '0x10' >>> int(020) File "<stdin>", line 1 int(020) ^ SyntaxError: invalid token >>> int(020,8) File "<stdin>", line 1 int(020,8) ^ SyntaxError: invalid token >>> int('020',8) 16 >>> int('x1',16) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 16: 'x1' >>> int('0x10',16) 16 >>> quit() $
0 コメント:
コメントを投稿