简体   繁体   中英

Assigning a boolean value from an “if … in” statement in Python

I am just starting on Python from a PHP background. I was wondering if there is a more elegant way in assigning a variable the result of an "if ... in" statement?

I currently do

is_holiday = False
if now_date in holidays:
    is_holiday = True

To me it looks like an unnecessary amount of code line or is this the absolute minimum and meets python style guides?

is_holiday = now_date in holidays

Use Conditional expressions :

is_holiday = True if now_date in holidays else False

or just is_holiday = now_date in holidays .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM