将多个变量与"if"中的相同值进行比较. [英] Compare multiple variables to the same value in "if"?

查看:72
本文介绍了将多个变量与"if"中的相同值进行比较.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python,并且我希望有一个if语句,其中包含许多变量.

I am using Python and I would like to have an if statement with many variables in it.

例如:

if A, B, C, and D >= 2:
    print (A, B, C, and D)

我意识到这不是正确的语法,而这正是我要问的问题-这种if语句类型的正确Python语法是什么?

I realize that this is not the correct syntax and that is exactly the question I am asking — what is the correct Python syntax for this type of an if statement?

推荐答案

这是怎么回事:

if all(x >= 2 for x in (A, B, C, D)):
    print A, B, C, D

如果您要测试条件相同的 long 变量列表,这将很有帮助.

This should be helpful if you're testing a long list of variables with the same condition.

这篇关于将多个变量与"if"中的相同值进行比较.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆