Python-Ref > Basic usage > Operators > Comparison operators
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Comparison operators

Comparison operators are, quite surprisingly, used for comparison of values. The result of such operators is always a boolean value - True or False.
Expand/Shrink
Zdroj: (operators2-1.py)
  1   a = 1
  2   b = 1
  3   c = 2
  4   d = a + b + c
  5   
  6   print a == 1   # equality
  7   print a == 2
  8   print          # empty line
  9   print a != b   # inequality
 10   print c != a + b
 11   print 
 12   print a > b    # greater than
 13   print c < d    # lower than
 14   print
 15   print a >= b   # greater or equal
 16   print c <= 2   # lower or equal
stdout:
True
False

False
False

False
True

True
True
Doba běhu: 21.4 ms
The operator is (is not) is used for object comparison. For simple values (such as numbers or strings) is is the same as ==. However for more complex values, such as lists, it compares the objects themselves, not their values.
Expand/Shrink
Zdroj: (operators2-2.py)
  1   a = 1
  2   b = 1
  3   print a == b   # equality of value
  4   print a is b   # equality of objects
  5   
  6   # but
  7   a = [1]
  8   b = [1]
  9   print a == b
 10   print a is b
 11   
 12   print a is not b   # inequality of objects
stdout:
True
True
True
False
True
Doba běhu: 21.2 ms
More on the difference between objects and their values is discussed in chapter Moving objects around.
Boolean values, results of comparison operations, may be combined together using Logical operators