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
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下