3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Python】if 変数: の真偽値

Last updated at Posted at 2019-03-26

pythonでは

if 変数:
    処理

で変数のnullチェックが行える。

本記事は、None以外の値で誤動作を防ぐためのメモ。

サンプルコード

if hoge:
    print('True')
else:
    print('False')

のとき

Trueになるもの
hoge = True
hoge = 1
hoge = '0'
hoge = ' '
hoge = ['']
hoge = {''}

Falseになるもの
hoge = None
hoge = False
hoge = 0
hoge = ''
hoge = []
hoge = {}

公式ドキュメント該当箇所

組み込み型 — Python 3.7.3 ドキュメント

3
5
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
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?