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版』

第13章 whileループとforループ

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

この章では、Pythonの2つの主要なループ構文(アクションを何度も繰り返す構文)を紹介することで、Pythonの手続き文のツアーを締めくくる:

while/else
最も一般化されたループ文であり、あらゆる種類の反復タスクを処理できる。
for/else
イテレート可能な」オブジェクト内のオブジェクトを簡単にステップ実行するための特殊化ループである。

この2つはすでに非公式に紹介し、使ってきたが、ここではさらに使い方の詳細を説明する。ついでに、break やcontinue 、ループのelse など、ループ内で使用されるあまり目立たない文も勉強し、range 、zip 、enumerate など、ループでよく使用されるビルトインも取り上げる。

ここで取り上げたwhile とfor ステートメントは、繰り返し動作をコーディングするために提供される主要な構文であるが、Pythonには他にもループ演算子や概念がある。次の章では、Pythonの反復プロトコル(for ループで使用)とリスト内包表記(for ループのいとこ)の関連アイデアを探求する。後の章では、ジェネレータや関数型ツール(map 、filter 、reduce など)のような、さらにエキゾチックな反復ツールについて説明する。しかし今は、物事をシンプルに考えよう。

while ループ

Pythonのwhile 文は、言語の中で最も一般化された繰り返し構文である。簡単に言うと、先頭にあるテストが真と評価され続ける限り、関連する文のブロックを繰り返し実行する。ループ」と呼ばれるのは、、テストが偽になるまで制御が文の最初に戻ってループし続けるからである。テストが偽になると、while ブロックに続く文に制御がpassする。正味の効果は、先頭のテストが真である間、ループ本体が繰り返し実行されることである。テストが最初から偽の場合、本体は実行されず、while 文はスキップされる。

一般形式

最も複雑な形では、while ステートメントは、テスト式を含むヘッダ行と、1つ以上の通常インデントされたステートメントの本体、そしてbreak ステートメントに遭遇せずに制御がループを抜けた場合に実行されるオプションのelse 部分から構成される。Pythonは、テストが偽の値を返すまで、先頭でテストを評価し、ループ本体に入れ子になっている文を実行し続ける:

while test:                  # Loop test
    statements               #     Repeated loop body
else:                        # Optional else
    statements               #     Run if didn't exit loop body with break

例

説明のために、いくつかの単純なwhile ループを見てみよう。while ループの中にprint 文が入れ子になっている最初のループは、ただメッセージを永遠にprintするだけである。True は整数1 のカスタムバージョンに過ぎず、常にブール値 true を表すことを思い出してほしい。テストは常に true なので、Python はボディを永遠に、あるいは実行を止めるまで実行し続ける。このようなの振る舞いは通常無限ループと呼ばれる。実際には不死ではないが、強制的に終了させるにはCtrl+Cキーの組み合わせが必要かもしれない: ...

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

Fluent Python 第2版 ―Pythonicな思考とコーディング手法

Fluent Python 第2版 ―Pythonicな思考とコーディング手法

Luciano Ramalho, 牧野 聡
Fluent Python ―Pythonicな思考とコーディング手法

Fluent Python ―Pythonicな思考とコーディング手法

Luciano Ramalho, 豊沢 聡, 桑井 博之, 梶原 玲子

Publisher Resources

ISBN: 9798341634367