Skip to Content
Raspberry Piクックブック 第4版
book

Raspberry Piクックブック 第4版

by Simon Monk, 水原 文
January 2024
Beginner to intermediate
560 pages
8h 38m
Japanese
O'Reilly Japan, Inc.
Content preview from Raspberry Piクックブック 第4版

6章 PythonのリストとディクショナリーPython Lists and Dictionaries

5章では、Python言語の基本を学んだ。この章では、リストとディクショナリーという、2つの重要なPythonのデータ構造について見ていこう。

レシピ6.1:リストを作成する

課題 1つの値だけではなく、一連の値を保持する変数を使いたい。

解決 リストを使う。Pythonでは、リストとは値の集まりに一定の順序を付けて保存したものであり、値の位置を指定することによってアクセスできる。

 リストは、[]の間に内容の初期値を指定して作成する。

>>> a = [34, 'Fred', 12, False, 72.3]
>>>

 C言語などの融通の利かない行列とは違って、Pythonではリストを宣言する際にサイズを指定する必要はない。また、いつでも好きなときにリスト中の要素の数を変更できる。

解説 このコード例が示すように、リスト中の要素は同じ型である必要はないが、そうであることも多い。リストの要素がそれ自体リストであることも、ごく普通だ。

空のリストを作成して後で要素を追加したければ、次のようにする。

>>> a = []
>>>

レシピ6.2:リストの要素にアクセスする

課題 リストの要素を個別に取り出したり、変更したりしたい。

解決 []記法を使ってリスト内の位置を指定し、リストの要素にアクセスする。例えば、リスト中の位置1の要素にアクセスするには次のようにすればよい。

>>> a = [34, 'Fred', 12, False, 72.3]
>>> a[1]
'Fred'

解説 リスト内の位置(インデックス)は最初の要素を示す0(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

SRE サイトリライアビリティエンジニアリング ―Googleの信頼性を支えるエンジニアリングチーム

SRE サイトリライアビリティエンジニアリング ―Googleの信頼性を支えるエンジニアリングチーム

Betsy Beyer, Chris Jones, Jennifer Petoff, Niall Richard Murphy, 澤田 武男, 関根 達夫, 細川 一茂, 矢吹 大輔, Sky株式会社 玉川 竜司

Publisher Resources

ISBN: 9784814400508Publisher Website