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

第11章. 代入、式、プリント

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

さて、Python文の構文を一通り学んだところで、この章では具体的なPython文について詳しく解説していく。代入文、式文、print演算子といった基本的なものから始める。これらの文はすでに動作を見てきたが、ここではこれまで飛ばしてきた重要な詳細を説明する。これらの文は比較的単純なものだが、実際にPythonプログラミングを始めると便利なオプションがある。

代入

Pythonの代入文は、オブジェクトをin文に保持するためにしばらく使ってきた。その基本的な形は、等号の左に代入対象、右に代入されるオブジェクトを書く。左側のターゲットは名前でもオブジェクト・コンポーネントでもよく、右側のオブジェクトはオブジェクトを作成する任意の式でもよい。ほとんどの場合、代入は簡単だが、いくつか重要な特性があるので前もって覚えておこう:

  • 代入はオブジェクト参照を作成する。 第6章で説明したように、Pythonの代入はオブジェクトへの参照を名前やデータ構造のコンポーネントに格納する。オブジェクトをコピーするのではなく、常にオブジェクトへの参照を作成する。そのため、Python の変数はデータ格納領域というよりはポインタに近い。

  • 名前は最初に代入されたときに作成される。Pythonは最初に値を代入したときに変数名を作成するので(つまりオブジェクト参照)、、前もって名前を宣言しておく必要はない。一部の(すべてではないが)データ構造スロットも代入時に作成される(辞書エントリや一部のオブジェクト属性など)。一度代入された名前は、式の中で登場するたびに、それが参照する値に置き換えられる。

  • 名前は参照される前に代入されなければならない。まだ値を代入していない名前を使うのはエラーだ。Pythonは曖昧なデフォルト値を返すのではなく、例外を発生させる。もしPythonが未割り当ての名前をエラーとして扱う代わりにデフォルト値として提供していたら、コード中の名前のタイプミスを見つけるのはもっと難しくなっていただろう。

  • いくつかの演算子は暗黙的に代入を行う。この節では、= 文とその:= 式との相対を扱うが、代入は Python の多くの文脈で発生する。例えば、モジュールのインポート、関数やクラスの定義、for ループ変数、関数の引数はすべて暗黙の代入であることは後で説明する。代入はどこに出てきても同じように働くので、これらのコンテキストはすべて、実行時に名前とオブジェクトコンポーネントをオブジェクト参照にバインド(つまり代入)するだけである。

前置きはこれくらいにして、コードに移ろう。

代入構文フォーム

代入はPythonの一般的な概念ですが、この章では主に代入文()と、1つの限定式に興味を持ちます。表11-1は、Pythonで代入をコーディングするために利用可能なさまざまな構文を示しています。

表11-1. 代入文と式の形式
演算子 インタプリタ
target = 'Hack' 基本代入
code, hack = 'py', 'PY' タプルの代入
[code, hack] = ['py', 'PY'] 代入リスト
a, b, c, d = 'hack' シーケンス代入
a, *b = 'hack' 延長開梱代入
code = hack = 'python' 複数のターゲット代入
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