2013年4月3日水曜日

開発環境

初めてのコンピュータサイエンス(Jennifer CampbellPaul GriesJason MontojoGreg Wilson(著)長尾 高弘(翻訳))の3章(文字列)の3.8の練習問題を解いてみる。

3.8 練習問題

入出力結果(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.
>>> 'Comp' 'Sci' # 'CompSci'
'CompSci'
>>> 'Computer' + ' Science' # 'Computer Science'
'Computer Science'
>>> 'H2O' * 3 # 'H2OH2OH2O'
'H2OH2OH2O'
>>> 'CO2' * 0 # ''
''
>>> print("They'll hibernate during the winter.")
They'll hibernate during the winter.
>>> print("Absolutely not,\" he said.")
Absolutely not," he said.
>>> print("He said, 'Absolutely not,'\" recalled Mel.")
He said, 'Absolutely not,'" recalled Mel.
>>> print("\"He said, 'Absolutely not,'\" recalled Mel.")
"He said, 'Absolutely not,'" recalled Mel.
>>> print("hydrogen sulfide")
hydrogen sulfide
>>> print(left\\right")
  File "<stdin>", line 1
    print(left\\right")
                      ^
SyntaxError: unexpected character after line continuation character
>>> print("left\\right")
left\right
>>> print("""'''A
... B
... C'''""")
'''A
B
C'''
>>> print("'''A\nB\nC'''")
'''A
B
C'''
>>> len('') # 0
0
>>> x=3
>>> y=12.5
>>> print("The rabbit is {0}".format(x))
The rabbit is 3
>>> print("The rabbit is {0} years old.".format(x))
The rabbit is 3 years old.
>>> print("{0} is average.".format(y))
12.5 is average.
>>> print("{0} * {1}".format(y, x))
12.5 * 3
>>> print("{0} * {1} is {2}".format(y, x, y * x))
12.5 * 3 is 37.5
>>> "%.2f" % 34.5
'34.50'
>>> "{0:0>4d}".format(8)
'0008'
>>> "{0:<2d}".format(8)
'8 '
>>> num = float(input())
1.2
>>> num
1.2
>>> type(num)
<class 'float'>
>>> "{0:e}".format(34.5)
'3.450000e+01'
>>> "{0:.2e}".format(34.5)
'3.45e+01'
>>> "a" * -1
''
>>> quit()
$

0 コメント:

コメントを投稿