简体   繁体   English

从Python中的“ if…in”语句分配布尔值

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

I am just starting on Python from a PHP background. 我只是从PHP背景开始使用Python。 I was wondering if there is a more elegant way in assigning a variable the result of an "if ... in" statement? 我想知道是否有一种更优雅的方式将变量“ if ... in”的结果分配给变量?

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? 对我来说,这似乎是不必要的代码行,或者这是绝对最小值,并且符合python样式指南?

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 . 或者只是is_holiday = now_date in holidays

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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