LoginSignup
9
8

More than 3 years have passed since last update.

【Python】 1行でbool値反転 

Last updated at Posted at 2020-01-23
python初学者の自分用リマインドメモ

ご指摘を受けて編集
returnするまでもなく変数にnotで反転して格納できるとのこと。
つまり下記でOKでした。。。

python.py
li = []
result = bool(li)


turned_result = not bool(li)


print('result: ',result)
print('turned_result: ',turned_result)
# result: False
# turned_result: True 


以前のもの

notでreturn値を返すと反転できる!とのことでlambdaの即時実行で応用

python.py
li = []
result = bool(li)


turned_result = (lambda x: not bool(x))(li)


print('result: ',result)
print('turned_result: ',turned_result)
# result: False
# turned_result: True 



経験1年未満の初心者です。
もっといい方法や可読性の可否等、諸先輩のご意見伺えたらありがたいです。

9
8
3

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
9
8