Python比较运算符
这些运算符比较它们两侧的值并确定它们之间的关系。它们也称为关系运算符。
假设变量a持有10,变量b持有20,则-
如果两个操作数的值相等,则条件变为true。
如果两个操作数的值不相等,则条件为true。
如果两个操作数的值不相等,则条件为true。
如果左操作数的值大于右操作数的值,则条件为true。
如果左操作数的值小于右操作数的值,则条件为true。
如果左操作数的值大于或等于右操作数的值,则条件为true。
如果左操作数的值小于或等于右操作数的值,则条件为true。
示例
假设变量a持有10,变量b持有20,则-
#!/usr/bin/python a = 21 b = 10 c = 0 if ( a == b ): print "Line 1 - a is equal to b" else: print "Line 1 - a is not equal to b" if ( a != b ): print "Line 2 - a is not equal to b" else: print "Line 2 - a is equal to b" if ( a <> b ): print "Line 3 - a is not equal to b" else: print "Line 3 - a is equal to b" if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b" if ( a > b ): print "Line 5 - a is greater than b" else: print "Line 5 - a is not greater than b" a = 5; b = 20; if ( a <= b ): print "Line 6 - a is either less than or equal to b" else: print "Line 6 - a is neither less than nor equal to b" if ( b >= a ): print "Line 7 - b is either greater than or equal to b" else: print "Line 7 - b is neither greater than nor equal to b"
输出结果
当您执行上述程序时,它将产生以下结果-
Line 1 - a is not equal to b Line 2 - a is not equal to b Line 3 - a is not equal to b Line 4 - a is not less than b Line 5 - a is greater than b Line 6 - a is either less than or equal to b Line 7 - b is either greater than or equal to b
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短