Python-Ref > Basic usage > Operators > Logical operators
 
 

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

Logical operators

Logical operators work with boolean values. The are often used together with comparison operators to construct more complex logical expressions.
Expand/Shrink
Zdroj: (operators3-1.py)
  1   a = 1
  2   b = 1
  3   c = a + 2*b
  4   ready = True   # this variable has boolean value right away
  5   
  6   
  7   print a == b and c >= a+b
  8   print c < 10 and ready
  9   print ready and a > b
 10   print a != 1 or ready
 11   print not a == b
 12   print not a == 2 and not c == 2
 13   print (not a == 2) and (not c == 2)   # this is the same as above
 14                                         # brackets improve readability of the code
 15   
stdout:
True
True
False
True
False
True
True
Doba běhu: 21.1 ms
Brackets may be used to group values or to improve readability.
Expand/Shrink
Zdroj: (operators3-2.py)
  1   x = 1
  2   y = 2
  3   z = 3
  4   
  5   print (x < y) and (y < z)   # brackets only improve readability
  6   print (x > y) and (y > z) or True   # brackets change meaning
  7   print (x > y) and (y > z or True)
stdout:
True
True
False
Doba běhu: 21.0 ms