2014年9月5日金曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 9(Repeating Code Using Loops)、9.10(Exercises) 4-a, b, c.を解いてみる。

9.10(Exercises) 4-a, b, c.

コード(BBEdit)

sample4.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

print('a.')
alkaline_earth_metals = [[4, 9.012], [12, 24.305], [20, 40.078], [38, 87.62],
                         [56, 137.327], [88, 226]]
print(alkaline_earth_metals)

print('b.')
for number, weight in alkaline_earth_metals:
    print(number)
    print(weight)

print('c.')
number_and_weight = []
for number, weight in alkaline_earth_metals:
    number_and_weight.append(number)
    number_and_weight.append(weight)

print(number_and_weight)

入出力結果(Terminal, IPython)

$ ./sample4.py
a.
[[4, 9.012], [12, 24.305], [20, 40.078], [38, 87.62], [56, 137.327], [88, 226]]
b.
4
9.012
12
24.305
20
40.078
38
87.62
56
137.327
88
226
c.
[4, 9.012, 12, 24.305, 20, 40.078, 38, 87.62, 56, 137.327, 88, 226]
$

0 コメント:

コメントを投稿