#!/usr/bin/python3 # -*- coding: utf-8 -*- # %% Logic # Boolean values are special primitives # Note: the capitalization True False # They can be printed: print(True) print(False) # They're objects that can be "stored" in variables too. varwithtrue: bool = True varwithfalse: bool = False # Logical operators # 'not' flips boolean values """ not Logical NOT operator Used to reverse the logical state of its operand. Not(a and b) is True. """ print(not True) # => False print(not False) # => True # Boolean Operators # Note "and" and "or" are case-sensitive """ and Logical AND operator If both the operands are true then condition becomes true. (a and b) is False. """ print(True and False) # => False """ or Logical OR operator If any of the two operands are non-zero then condition becomes true. (a or b) is True. """ print(False or True) # => True # True and False are actually 1 and 0 but with different keywords print(True + True) # => 2 print(True * 8) # => 8 print(False - 5) # => -5 # Comparison operators look at the numerical value of True and False print(0 == False) # => True print(1 == True) # => True print(2 == True) # => False print(-5 != False) # => True # Use 'is' instead for booleans print(0 is False) # => True print(1 is True) # => True print(2 is True) # => False print(-5 is not False) # => True # Using boolean logical operators on ints, # casts them (turns them into) to booleans for evaluation, # but their non-cast value is returned # Don't mix up with bool(ints) and bitwise and/or (&, |) print(bool(0)) # => False print(bool(4)) # => True print(bool(-6)) # => True print(0 and 2) # => 0 print(-5 or 0) # => -5 # Equality is tested with == """ == If the values of two operands are equal, then the condition becomes true. (a == b) is not true. """ print(1 == 1) # => True print(2 == 1) # => False # Inequality is tested with != """ != If values of two operands are not equal, then condition becomes true. (a != b) is true. """ print(1 != 1) # => False print(2 != 1) # => True # More comparisons """ < If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true. """ print(1 < 10) # => True """ > If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true. """ print(1 > 10) # => False """ <= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true. """ print(2 <= 2) # => True """ >= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true. """ print(2 >= 2) # => True # Determine whether a value is in a range print(1 < 2 and 2 < 3) # => True print(2 < 3 and 3 < 2) # => False # Chaining makes this look nicer, in order of number line for ease: print(1 < 2 < 3) # => True print(2 < 3 < 2) # => False # These are most understandable when in correct order (above), not like below: print(3 > 2 > 1) # Don't do that, numberlines go forward. # None is a special object in python3 None # => None mynone = None print(mynone) # Don't use the equality "==" symbol to compare objects to None # Use "is" instead. This checks for equality of object identity. print("etc" is None) # => False print(None is None) # => True print(0 is None) # => False