2018年10月23日火曜日

開発環境

入門 Python 3 (Bill Lubanovic (著)、斎藤 康毅 (監修)、長尾 高弘 (翻訳)、オライリージャパン)の7章(プロのようにデータを操る)、7.3(復習問題)7-4、5、6.を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3
print('4.')

print(
    '''My kitty cat likes %s,
My kitty car likes %s,
My kitty cat fell on his %s
And now thinks he's a %s.''' %
    ('roast beef', 'ham', 'head', 'clam')
)

print('5.')

letters = '''Dear {salutation} {name},

Thank you for your letter. We are sorry that our {product} {verbed} in your
{room}. Please note that it should never be used in a {room}, especially
near any {animals}.

Send us your receipt and {amount} for shipping and handling. We will send
you another {product} that, in our tests, is {percent}% less likely to
have {verbed}.

Thank you for your support.

Sincerely,
{spokesman}
{job_title}'''

print('6.')

response = {'salutation': 'Salutation',
            'name': 'Name',
            'product': 'Product',
            'verbed': 'Verbed',
            'room': 'Room',
            'animals': 'Animals',
            'amount': 'Amount',
            'percent': 'Percent',
            'spokesman': 'Spokesman',
            'job_title': 'Job_title'}

print(letters.format(**response))

print('おまけ。')
print('Literal String interpolation(フォーマット済み文字列リテラル)')

salutation = 'Salutation'
name = 'Name'
salutation = 'Salutation'
name = 'Name'
product = 'Product'
verbed = 'Verbed'
room = 'Room'
animals = 'Animals'
amount = 'Amount'
percent = 'Percent'
spokesman = 'Spokesman'
job_title = 'Job_title'
letters1 = f'''Dear {salutation} {name},

Thank you for your letter. We are sorry that our {product} {verbed} in your
{room}. Please note that it should never be used in a {room}, especially
near any {animals}.

Send us your receipt and {amount} for shipping and handling. We will send
you another {product} that, in our tests, is {percent}% less likely to
have {verbed}.

Thank you for your support.

Sincerely,
{spokesman}
{job_title}'''

print(letters1)
print(letters.format(**response) == letters1)

入出力結果(Terminal, Jupyter(IPython))

$ ./sample2.py
4.
My kitty cat likes roast beef,
My kitty car likes ham,
My kitty cat fell on his head
And now thinks he's a clam.
5.
6.
Dear Salutation Name,

Thank you for your letter. We are sorry that our Product Verbed in your
Room. Please note that it should never be used in a Room, especially
near any Animals.

Send us your receipt and Amount for shipping and handling. We will send
you another Product that, in our tests, is Percent% less likely to
have Verbed.

Thank you for your support.

Sincerely,
Spokesman
Job_title
おまけ。
Literal String interpolation(フォーマット済み文字列リテラル)
Dear Salutation Name,

Thank you for your letter. We are sorry that our Product Verbed in your
Room. Please note that it should never be used in a Room, especially
near any Animals.

Send us your receipt and Amount for shipping and handling. We will send
you another Product that, in our tests, is Percent% less likely to
have Verbed.

Thank you for your support.

Sincerely,
Spokesman
Job_title
True
$

0 コメント:

コメントを投稿

関連コンテンツ