LoginSignup
0
0

More than 3 years have passed since last update.

【Udemy Python3入門+応用】 37. 値が入っていない判定をするテクニック

Posted at

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

■値が入っているか?入っていないか?

◆使うもの
if is_ok:
    print('OK!')
else:
    print('No!')

を用意し、is_okに色々入れてみて試してみる。

■数字

◆1の場合
is_ok
is_ok = 1

if is_ok:
    print('OK!')
else:
    print('No!')
result
OK!
◆0の場合
is_ok
is_ok = 0

if is_ok:
    print('OK!')
else:
    print('No!')
result
No!

変数に数字が代入されている場合、
0 ならばif文ではFalseとなり、
0以外の数字(正でも負でも) ならばif文ではTrueとなる。
これを利用して、変数に0以外の値が代入されているかどうかを調べることができる。

Falseと判定されるもの

0 (int型)
0.0 (float型)
'' (str型)
[] (list型)
() (tuple型)
{} (dict型)
set() (set型)

0
0
0

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
0
0