Python 模擬試験

PythonZen & PEP 8 模擬試験 第5回 問4・The Zen of Python(The Zen of Python の格言)

曖昧な日付形式

「In the face of ambiguity, refuse the temptation to guess.」の観点から、01/02/2026 のような日付の文字列を読み取る処理の書き方として、最も適切なものを1つ選べ。

  1. A

    日付の形式(月/日/年か、日/月/年か)を、format で明示して変換する。

  2. B

    形式を指定せず、自動の判定に任せる。

  3. C

    すべてのデータを、月/日/年だと決めつけて、確認しない。

  4. D

    日付の列は、使わずに捨てる。

正解と解説を見る

正解:A

曖昧な入力は、推測ではなく、形式を明示して扱う。datetime.strptime や pd.to_datetime(format=...) で、形式を指定する。

The Zen of Python(PEP 20)の格言と意味。
格言意味
Beautiful is better than ugly.美しい方が、醜いよりよい
Explicit is better than implicit.明示的な方が、暗黙的よりよい
Simple is better than complex.単純な方が、複雑よりよい
Complex is better than complicated.複雑でも、ごちゃごちゃ(complicated)よりはよい
Flat is better than nested.平らな方が、入れ子より(深い入れ子より)よい
Sparse is better than dense.ゆとりのある方が、詰め込みよりよい
Readability counts.読みやすさは大事
Special cases aren't special enough to break the rules.特別な場合でも、規則を破るほど特別ではない
Although practicality beats purity.ただし、実用性は純粋さに勝る
Errors should never pass silently.エラーを黙って見過ごしてはいけない
Unless explicitly silenced.明示的に黙らせた場合は別
In the face of ambiguity, refuse the temptation to guess.あいまいなときは、推測したくなる誘惑を断る
There should be one-- and preferably only one --obvious way to do it.明白なやり方が1つ、できればただ1つだけあるべき
Although that way may not be obvious at first unless you're Dutch.その方法は、オランダ人でなければ最初は分かりにくいかもしれない
Now is better than never.やらないよりは、今やる方がよい
Although never is often better than *right* now.ただし、「今すぐ」よりやらない方がよいことも多い
If the implementation is hard to explain, it's a bad idea.実装を説明しにくいなら、悪い考え
If the implementation is easy to explain, it may be a good idea.実装を説明しやすいなら、よい考えかもしれない
Namespaces are one honking great idea -- let's do more of those!名前空間は素晴らしい考え。もっと活用しよう

学習のポイント:ISO 8601(2026-02-01)は、曖昧さがない。

この問題を演習で解く →

問題一覧へ

このサイトの問題は、各試験の公式に示された出題範囲に沿って作ったオリジナルの模擬問題です。本試験の問題ではありません。一般社団法人Pythonエンジニア育成推進協会の公式サービスではありません。