如何在Python中使用布尔值'and' [英] How to use boolean 'and' in Python

查看:154
本文介绍了如何在Python中使用布尔值'and'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我们可以像这样使用&&(布尔值和):

In C# we can use && (boolean and) like this:

int i = 5;
int ii = 10;
if(i == 5 && ii == 10) {
    Console.WriteLine("i is 5, and ii is 10");
}
Console.ReadKey(true);

但是用python尝试一下:

But try that with python:

i = 5
ii = 10
if i == 5 && ii == 10:
    print "i is 5 and ii is 10";      

我收到一个错误:SyntaxError: invalid syntax

如果我使用单个&,则至少没有语法错误.如何在Python中执行布尔&&?

If I use a single &, at least I get no syntax error. How do I do a boolean && in Python?

推荐答案

尝试一下:

i = 5
ii = 10
if i == 5 and ii == 10:
      print "i is 5 and ii is 10"

编辑:哦,您不需要在最后一行使用分号(编辑以将其从我的代码中删除).

Edit: Oh, and you dont need that semicolon on the last line (edit to remove it from my code).

这篇关于如何在Python中使用布尔值'and'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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