Python学习入门基础教程(learning Python)--3.3.2 Python的关系运算
2013-09-01 23:00
976 查看
如果if的condition不用布尔表达式来做条件判断而采用关系表达式,实际上关系表达式运算的结果要么是True要么是False。下面我们先了解一些有关关系运算符的基础知识,如下表所示。
做个小程序测试一下。
[python]view plaincopydef if_check():
global x
x = 100
print(" in if_check x = ", x)
if x > 1:
print(" x greater than 1")
if x == 0:
print(" x equal to 0")
if x < -100:
print(" x lowe than -100")
if x >= 100:
print(" x greater than or equal to 100")
if x != 0:
print(" x not equal to 0")
def main():
global x
print(" in main x = ", x)
if_check()
x = 12
main()
程序运行结果如下所示。
从Python程序运行结果来看,当x = 100时,x > 1 x >= 100 和x != 0这三个if语句关系运算结果为真,所以其下的打印语句执行。
做个小程序测试一下。
[python]view plaincopydef if_check():
global x
x = 100
print(" in if_check x = ", x)
if x > 1:
print(" x greater than 1")
if x == 0:
print(" x equal to 0")
if x < -100:
print(" x lowe than -100")
if x >= 100:
print(" x greater than or equal to 100")
if x != 0:
print(" x not equal to 0")
def main():
global x
print(" in main x = ", x)
if_check()
x = 12
main()
程序运行结果如下所示。
从Python程序运行结果来看,当x = 100时,x > 1 x >= 100 和x != 0这三个if语句关系运算结果为真,所以其下的打印语句执行。
相关文章推荐
- Python学习入门基础教程(learning Python)--3.3.2 Python的关系运算
- Python学习入门基础教程(learning Python)--3.3.3 Python逻辑关系表达式
- Python学习入门基础教程(learning Python)--3.3.3 Python逻辑关系表达式
- Python学习入门基础教程(learning Python)--2.3.1 Python传参函数设计
- Python学习入门基础教程(learning Python)--5.7 Python文件数据记录存储与处理
- Python学习入门基础教程(learning Python)--1.2.1 Python输出语句print基本使用 .
- Python学习入门基础教程(learning Python)--2.1 Python下自定义函数 .
- Python学习入门基础教程(learning Python)--2.3.1 Python传参函数设计
- Python学习入门基础教程(learning Python)--6.4 Python的list与函数
- Python学习入门基础教程(learning Python)--5.2 Python读文件基础
- Python学习入门基础教程(learning Python)--2.3.2Python函数实参详解
- Python学习入门基础教程(learning Python)--3.3 分支语句的条件表达式详解 .
- Python学习入门基础教程(learning Python)--2.3.3Python函数型参详解
- Python学习入门基础教程(learning Python)--5.6 Python读文件操作高级
- Python学习入门基础教程(learning Python)--2.2 Python下的变量基础 .
- Python学习入门基础教程(learning Python)--2.3.4Python函数返回值
- Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式
- Python学习入门基础教程(learning Python)--2.3 Python自定义函数传参
- Python学习入门基础教程(learning Python)--5.1 Python下文件处理基本过程
- Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式