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

查看:32
本文介绍了将多个变量与“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

如果您正在测试具有相同条件的变量列表,这应该会很有帮助.

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

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

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