Skip to content

Commit

Permalink
Merge pull request #89 from jbking/master
Browse files Browse the repository at this point in the history
型についての説明を整理した
  • Loading branch information
terapyon committed Apr 6, 2019
2 parents e7c7d2c + 15dff0a commit 910fb73
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions source/textbook/2_intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,42 @@ FizzBuzz処理の実装の完了
FizzBuzz はいろいろな方法で実装できます。もっと短く、わかりやすく書くにはどうすればよいか、チャレンジしてみてください。
.. admonition:: コラム: 値には型がある
Pythonでは整数や文字列、リストなど多種多様な値を使うことができますが、これらを区別して扱うための仕組みがあります。この仕組みのことを ```` といいます。
型ごとにできることとできないことが定義されていて、それぞれの値は型のインスタンスになります。
数値の ``100`` は整数型(int)の値で、他の数値と演算することができます。
また、ある値の型は他の抽象度が高い型のものとみなすことができるものもあります。
たとえば文字列やリストの型は、それぞれ文字列型(str)のとリスト型(list)ですが、ほかに繰返し可能な型(シーケンス)ともいえます。
型は組み込み関数の ``type()`` 関数や ``isinstance()`` 関数を使って調べることができます。
.. code-block:: python
:caption: 文字列とリストとそれらの型
>>> n1 = 100
>>> s1 = "hello"
>>> l1 = [1, 2, 3]
>>> type(n1)
<class 'int'>
>>> type(s1)
<class 'str'>
>>> type(l1)
<class 'list'>
>>> isinstance(n1, int)
True
>>> isinstance(s1, str)
True
>>> isinstance(l1, list)
True
>>> import collections.abc
>>> isinstance(s1, collections.abc.Sequence)
True
>>> isinstance(l1, collections.abc.Sequence)
True
まとめ
=============
本節では、FizzBuzzを通じたPythonの特徴、基本を紹介しました。
Expand Down

0 comments on commit 910fb73

Please sign in to comment.