2
0

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 1 year has passed since last update.

PythonAdvent Calendar 2023

Day 1

Pythonで「どのようなオブジェクトが真(True)や偽(False)と評価されるのか」の動作を確認してみた

Posted at

概要

Pythonで「どのようなオブジェクトが真(True)や偽(False)と評価されるのか」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
print(bool(True))
print(bool(False))
print(bool(None))
#print(bool(NotImplemented))
print(bool(0))
print(bool(5))
print(bool(0.0))
print(bool(0.1))
print(bool(0j))
print(bool(""))
print(bool("Hello"))
print(bool(()))
print(bool(("Blue","Red")))
print(bool([]))
print(bool(["Apple", "Orange"]))
print(bool({}))
print(bool({"A":"Apple","O":"Orage"}))

name = ""
if name:
    print("名前は" + name + "です")
else:
    print("名前は未登録です")

name = "Yamada"
if name:
    print("名前は" + name + "です")
else:
    print("名前は未登録です")

以下のコマンドを実行しました。

$ python3 sample.py 
True
False
False
False
True
False
True
False
False
True
False
True
False
True
False
True
名前は未登録です
名前はYamadaです

まとめ

何かの役に立てばと。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?