Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式
2013-08-28 15:45
1056 查看
简单的说就是if要判断condition是真是假,Python和C语言一样非0即真,所以如果if的condition是布尔表达式我们可以用True或者非0数(不可是浮点数)表示真,用False或者0表示假。
[python]view plaincopyprint?def if_check():
if1:
print("1")
if0:
print("0")
if -100:
print("-100")
if100:
print("100")
ifTrue:
print("true")
ifFalse:
print("false")
def main():
if_check()
main()
可以看出只有布尔表达式是0和False这两条if语句没有被执行,而布尔表达式为1 -100 100 True的if语句其下的打印语句都执行了。
[python]view plaincopyprint?def if_check():
if1:
print("1")
if0:
print("0")
if -100:
print("-100")
if100:
print("100")
ifTrue:
print("true")
ifFalse:
print("false")
def main():
if_check()
main()
def if_check(): if 1: print("1") if 0: print("0") if -100: print("-100") if 100: print("100") if True: print("true") if False: print("false") def main(): if_check() main()运行结果如下所示。
可以看出只有布尔表达式是0和False这两条if语句没有被执行,而布尔表达式为1 -100 100 True的if语句其下的打印语句都执行了。
相关文章推荐
- Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式
- Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式
- Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式
- Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式
- Python学习入门基础教程(learning Python)--1.2.1 Python输出语句print基本使用 .
- Python学习入门基础教程(learning Python)--2.1 Python下自定义函数 .
- Python学习入门基础教程(learning Python)--2.3.5Python返回多个值问题
- Python学习入门基础教程(learning Python)--8.3 字典常用的方法函数介绍
- Python学习入门基础教程(learning Python)--6 Python下的list数据类型
- Python学习入门基础教程(learning Python)--3 Python分支结构和布尔逻辑
- Python学习入门基础教程(learning Python)--5 Python文件处理
- Python学习入门基础教程(learning Python)--2.2 Python下的变量基础 .
- Python学习入门基础教程(learning Python)--6.3 Python的list切片高级
- Python学习入门基础教程(learning Python)--2.2.1 Python下的变量解析 .
- Python学习入门基础教程(learning Python)--5.7 Python文件数据记录存储与处理
- Python学习入门基础教程(learning Python)--5.2 Python读文件基础
- Python学习入门基础教程(learning Python)--1.2.2 Python格式化输出基础 .
- Python学习入门基础教程(learning Python)--3.1Python的if分支语句 .
- Python学习入门基础教程(learning Python)--3.3.2 Python的关系运算
- Python学习入门基础教程(learning Python)--2.2.2 Python全局和局部变量 .