1
1

More than 3 years have passed since last update.

【Udemy Python3入門+応用】 38. Noneを判定する場合

Posted at

※この記事はUdemyの
現役シリコンバレーエンジニアが教えるPython3入門+応用+アメリカのシリコンバレー流コードスタイル
の講座を受講した上での、自分用の授業ノートです。
講師の酒井潤さんから許可をいただいた上で公開しています。

■Noneを判定する場合

◆None

Pythonでは、何も入っていないオブジェクトをNoneと表現する。

◆Noneかどうかの判定
None
is_empty = None

if is_empty == None:
    print('None!!!')
result
None!!!

今までやってきた通り、==を使って判定することももちろんできるが、
Noneの判定で==を使うことは少ない。

◆isで判定
None_is
is_empty = None

if is_empty is None:
    print('None!!!')
result
None!!!

Noneの判定にはisを使うことが多いので、覚えておくこと。

==isの違い
=_is
print(1 == True)
print(1 is True)
result
True
False

厳密な違いは今の段階ではわからなくてよいが、
isは左辺と右辺が完璧に同じもののときにTrueとなる。
isを使うのは、Noneの判定が一番多いので覚えておくこと。

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1