開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の5章(リスト)、5.13(練習問題)、1から13を解いてみる。
5.13(練習問題)、1から13.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8
alkaline_earth_metals = [4, 12, 20, 38, 56, 88]
# 2.
print('ラジウムの原子番号: {0} {1}'.format(
alkaline_earth_metals[len(alkaline_earth_metals) -1],
alkaline_earth_metals[-1]))
# 3. リストに含まれる要素数は関数lenで知ることが出来る
print('3.')
print(len(alkaline_earth_metals))
print('4.')
print('{0}: 最も高い原子番号 {1}'.format(
alkaline_earth_metals, max(alkaline_earth_metals)))
print('5.')
# python3.xからprintはステートメントではなく関数
# 改行についてはキーワード引数endで指定すればいい(標準は'\n')
print('a')
print('a', end= '')
print()
print('6.')
half_lives = [87.74, 24110.0, 6537.0, 14.4, 376000.0]
for x in half_lives:
print(x)
print('7.')
for x in half_lives:
print(x, end=' ')
print()
print('8.')
country_populations = [1295, 23, 7, 3, 47, 21]
total = 0
for x in country_populations:
total += x
print(total)
print('9.')
temps = [25.2, 16.8, 31.4, 23.9, 28, 22.5, 19.6]
print('temps: {0}'.format(temps))
print('10.')
temps.sort()
print('昇順にソート: {0}'.format(temps))
print('11.')
cool_temps = temps[:2]
warm_temps = temps[2:]
print('cool_temps: {0}'.format(cool_temps))
print('warm_temps: {0}'.format(warm_temps))
print('12.')
temps_in_celsius = cool_temps + warm_temps
print('temps_in_celsius: {0}'.format(temps_in_celsius))
print('13.')
temps_in_fahrenheit = []
for x in temps_in_celsius:
temps_in_fahrenheit.append(9 / 5 * x + 32)
print('temps_in_fahrenheit: {0}'.format(temps_in_fahrenheit))
入出力結果(Terminal)
$ ./sample.py ラジウムの原子番号: 88 88 3. 6 4. [4, 12, 20, 38, 56, 88]: 最も高い原子番号 88 5. a a 6. 87.74 24110.0 6537.0 14.4 376000.0 7. 87.74 24110.0 6537.0 14.4 376000.0 8. 1396 9. temps: [25.2, 16.8, 31.4, 23.9, 28, 22.5, 19.6] 10. 昇順にソート: [16.8, 19.6, 22.5, 23.9, 25.2, 28, 31.4] 11. cool_temps: [16.8, 19.6] warm_temps: [22.5, 23.9, 25.2, 28, 31.4] 12. temps_in_celsius: [16.8, 19.6, 22.5, 23.9, 25.2, 28, 31.4] 13. temps_in_fahrenheit: [62.24, 67.28, 72.5, 75.02, 77.36, 82.4, 88.52] $
0 コメント:
コメントを投稿