Truth and flow control in Python: Difference between revisions

wikademia>AdamG
m Python lesson four moved to Truth and Flow Control in Python: Moving from numeric to descriptive titles
Line 39: Line 39:
  >>> "2"+"4" == "24"
  >>> "2"+"4" == "24"
  True
  True
*Todo: Add other operators (and, not, or, is, in...)
 
=== Conjunction ===
The Python operator for conjunction is '''''and'''''.
>>> True and False
False
>>> True and True
True
>>> False and False
False
>>> False and True
False
 
The '''and''' operator works by evaluating until it runs into something false, and return the value that evaluated to false. If everything is True, it returns the last thing it evaluated.
 
>>> "a" and "b"
<nowiki>'b'</nowiki>
>>> "" and "b"
<nowiki>''</nowiki>
>>> True and 7
7
>>> True and [] and "a"
[]
 
=== Disjunction ===
The '''''or''''' operator works similar to the '''and''' operator. It returns the first value to be true, or the final value if there is no true value.
 
>>> False or ('a', 'b') or 'c'
('a', 'b')
>>> False or True
True
>>> <nowiki>''</nowiki> or False
False
>>> <nowiki>''</nowiki> or False or ()
()
 
 
*Todo: Add other operators (not, is, in...)
 
==Flow Control==
==Flow Control==
Now that you know all these great ways to work with booleans, let's use them!
Now that you know all these great ways to work with booleans, let's use them!