Pythonのif文で変数や値を指定したときの挙動12個!

Python Python

Pythonのif文に変数を直接指定して、判定したときの挙動を確認してみました。
載せているコードはPythonのバージョン3.10.7で確認しました。

if文に値を指定して、挙動を確認する

if文に直接、変数や値を指定して、挙動を確認してみました。

None

none = None

if none:
    print("none:true")
else:
    print("none:false")

# none:false

NoneはFalseになります。

空文字

empty = ''

if empty:
    print("empty:true")
else:
    print("empty:false")

# empty:false

空文字(”)はFalseになります。

文字列の変数

fruit = 'apple'

if fruit:
    print("fruit:true")
else:
    print("fruit:false")

# fruit:true

文字列の変数はTrueになります。

空のリスト

empty_list = []

if empty_list:
    print("empty_list:true")
else:
    print("empty_list:false")

# empty_list:false

空のリストの変数はFalseになります。

値が入っているリスト

input_list = ['apple', 'lemon', 'banana']

if input_list:
    print("input_list:true")
else:
    print("input_list:false")

# input_list:true

値が入っているリストはTrueになります。

空のタプル

empty_tuple = ()

if empty_tuple:
    print("empty_tuple:true")
else:
    print("empty_tuple:false")

# empty_tuple:false

空のタプルはFalseになります。

値が入っているタプル

input_tuple = ('cherry', 'grape')

if input_tuple:
    print("input_tuple:true")
else:
    print("input_tuple:false")

# input_tuple:true

値が入っているタプルはTrueになります。

空の辞書

empty_dictionary = {}

if empty_dictionary:
    print("empty_dictionary:true")
else:
    print("empty_dictionary:false")

# empty_dictionary:False

空の辞書はFalseになります。

値が入っている辞書

input_dictionary = {'apple': 100, 'banana': 110}

if input_dictionary:
    print("input_dictionary:true")
else:
    print("input_dictionary:false")

# input_dictionary:true

値が入っている辞書はTrueになります。

数値(正数)

if 1:
    print("1:true")
else:
    print("1:false")

# 1:true

正数はTrueになります。

0(ゼロ)

if 0:
    print("0:true")
else:
    print("0:false")

# 0:false

0はFalseになります。

数値(負数)

if -1:
    print("-1:true")
else:
    print("-1:false")

# -1:true

負数はTrueになります。

if文に値の指定まとめ(表)

今回確認した内容を表にまとめてみました。

入力値 結果
None False
空文字(”) False
‘apple’ True
[] False
[‘apple’, ‘lemon’, ‘banana’] True
() False
(‘cherry’, ‘grape’) True
{} False
{‘apple’: 100, ‘banana’: 110 } True
1 True
0 False
-1 True

基本的には思っていた通りでした。
負数(-1)はTrueになるので、数値を使うときには気をつけたいところです。

チェックしたコード全体

今回if文の挙動確認に使ったコードです。

none = None
empty = ''
fruit = 'apple'
empty_list = []
input_list = ['apple', 'lemon', 'banana']
empty_tuple = ()
input_tuple = ('cherry', 'grape')
empty_dictionary = {}
input_dictionary = {'apple': 100, 'banana': 110}

if none:
    print("none:true")
else:
    print("none:false")

if empty:
    print("empty:true")
else:
    print("empty:false")

if fruit:
    print("fruit:true")
else:
    print("fruit:false")

if empty_list:
    print("empty_list:true")
else:
    print("empty_list:false")

if input_list:
    print("input_list:true")
else:
    print("input_list:false")

if empty_tuple:
    print("empty_tuple:true")
else:
    print("empty_tuple:false")

if input_tuple:
    print("input_tuple:true")
else:
    print("input_tuple:false")

if empty_dictionary:
    print("empty_dictionary:true")
else:
    print("empty_dictionary:false")

if input_dictionary:
    print("input_dictionary:true")
else:
    print("input_dictionary:false")

if 1:
    print("1:true")
else:
    print("1:false")

if 0:
    print("0:true")
else:
    print("0:false")

if -1:
    print("-1:true")
else:
    print("-1:false")

コメント

タイトルとURLをコピーしました