Skip to Content
『Learning Python 第6版』
book

『Learning Python 第6版』

by Mark Lutz
March 2025
Intermediate to advanced
1172 pages
24h 57m
Japanese
O'Reilly Media, Inc.
Content preview from 『Learning Python 第6版』

第12章 ifとマッチセレクション

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

この章では、Pythonがテスト結果に基づいて代替案から行動を選択するために使用する2つの文を紹介する:

if/elif/else
ほとんどのプログラムの主要な選択ツールで、任意のロジックをプログラミングすることができる。
match/case
高度なマッチング演算子を備えた、より狭い複数選択のためのツール

この章は複合文(他の文を埋め込む文)についての最初の詳細なであるため、Python文の構文モデルの背後にある一般的な概念についても、第10章の導入部で行ったよりも詳しく説明する。if ステートメントがテストの概念を導入しているため、この章ではブール式も扱い、"ternary"if 式をカバーし、一般的な真理値テストについて詳しく説明する。

if文

簡単に言うと、Pythonのif 文は実行するアクションを選択する。if 式 と並んで、Pythonの主要な選択ツールであり、Pythonプログラムが持つロジックの多くを表している。また、最初の複合文でもある。他の文と同様、if 文は他の文(他のifを含む)を含むことができる。実際、Pythonではプログラム内の文を順次実行することも、任意に入れ子になった文(選択やループなど特定の条件下でのみ実行される文)を組み合わせることもできる。

一般形式

Pythonのif は、ほとんどの手続き型言語のif ステートメントの典型である。if 、1つ以上のオプションのelif ("else if "の場合)、そして最後のオプションのelse 。テストとelse の部分はそれぞれ、ヘッダ行の下にインデントされた、入れ子になった文の関連ブロックを持つ。if 文が実行されると、Python は真と評価された最初のテストに関連するコードのブロックを実行し、すべてのテストが偽と評価された場合はelse ブロックを実行する。if ステートメントの一般化は次のようになる:

if test1:                 # Main if test
    statements1           #     Associated block
elif test2:               # Optional elif test(s)
    statements2           #     Associated block
else:                     # Optional else default
    statements3           #     Associated block

基本的な例

例として、いつものようにREPLでif 。if の初期化テストとそれに関連する文以外は、すべて省略可能である。したがって、最も単純なケースでは、他の部分は省略される:

$ python3
>>> if 1:
...     print('true')
...
true

前に見たように、多くのREPLでは継続行を入力するとプロンプトが... 。IDLEでは、代わりにインデントされた行にドロップダウンするだけだ(必要なときはBackspaceをタップしてバックアップする)。空白行は、Enterを2回押すことで入力され、ほとんどの対話型インタフェースでは文全体を終了して実行する。

1 (後で復習するが、True という単語が等価性である)はブール真なので、この文のテストは常に成功する。ここで偽の結果を処理するには、if のテストが真でないときに実行されるように、else をコード化する:

>>> if not 1: ... ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python Polars ―高速なデータ処理を実現する次世代ライブラリ

Python Polars ―高速なデータ処理を実現する次世代ライブラリ

Jeroen Janssens, Thijs Nieuwdorp, 中田 秀基
入門 Python 3 第2版

入門 Python 3 第2版

Bill Lubanovic, 鈴木 駿, 長尾 高弘

Publisher Resources

ISBN: 9798341634367