Python学习教程(learning Python)--3.3.2 Python的关系运算

时间:2023-03-09 16:55:51
Python学习教程(learning Python)--3.3.2 Python的关系运算

如果if的condition不用布尔表达式来做条件判断而采用关系表达式,实际上关系表达式运算的结果要么是True要么是False。下面我们先了解一些有关关系运算符的基础知识,如下表所示。

Python学习教程(learning Python)--3.3.2 Python的关系运算

做个小程序测试一下。

def 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学习教程(learning Python)--3.3.2 Python的关系运算

从Python程序运行结果来看,当x = 100时,x > 1 x >= 100 和x != 0这三个if语句关系运算结果为真,所以其下的打印语句执行。

智普教育Python www.jeapedu.com

——————————————————————————————