# The Fork in the Road: logic. # 1. Introducing a new type of value: BOOLEAN. # There are only TWO bollean values, True and False (note the capital letters!) true = True # True is a reserved keyword in python, true isn't # so we can use it as a name of a variable false = False # same here! print("Value of variable true:") print(true) print("Value of variable false:") print(false) # There are expressions which can be evaluated as boolean, for example # comparisons: <, >, <= (less or equal), >= (greater or equal) print("Evaluating boolean expressions:") print(5<6) print(5>6) print(5<=6) print(5>=6) print(5<=5) print(5>=5) # Another important comparison operation is 'equal to': == # Be very careful not to confuse it with the assignment operator!!! # Assignment: a = 13 (variable a gets value 13, this is NOT an expression) # Comparison: a == 13 (a boolean expression: it # evaluates to True is a has value 13, and to False in ALL other cases) a = 13 print("Is a == 13?", a==13) print("Is a == 14?", a==14) #------------------------------------------------------------- # 2. IF-ELIF-ELSE: Operator if is used to choose between available # actions depending on some condition. Here's an example: print("\nAn example of how 'if' operator works:") a = 10 if a>10 : # keyword "if" followed by a boolean expression followed by ":" print("a is greater than 10") # note that this clause is moved to the right! # This is how python deals with hierarchy # of blocks of code - more on this later. else: # keyword "else" and a semicolon. It's on the same level as your 'if'! print("a is not greater than 10") # moved to the right again! # When the boolean expression after 'if' is evaluated to True, the first # block of code will be executed; if it evaluates to False, then the # second block will be executed. # You can also have more than one line of code in your clauses: print("\nLet's try again with different a:") a = 11 if a>10 : print("a is greater than 10") print("this is another line of code") else: print("a is not greater than 10") print("the more lines of code you have, the better!") # If you have more than two outcomes to take care of, use 'elif' # (it is short for else if): print("\nAn example of how 'elif' operator works:") if a<=5: print("a is less than 5") elif a<=10: # a is more than 5 already because otherwise # the previous branch would've been used print("a is greater than 5 and less than or equal to 10") elif a<=20: # similarly this branch will be executed if a is # greater than 10 but less than or equal to 20 print("a is greater than 10 and less than or equal to 20") else: print("all other values of a") # if none of the three conditions # are true, you'll end up here. print("\nAnother example (Amy's idea):") print("Are you happy? y/n") answer = input() if answer=="y": print(":)") elif answer=="n": print(":(") else: print(":|") #------------------------------------------------------------------ # 3. Converting values: # you can convert strings into numbers by using function int() or float() # (obviously your string should only contain numbers and a floating point!) print("\nConverting strings to numbers") a = "34" b = int(a) c = float(a) print("b:", b) print("c:", c) print("b+c+1:", b+c+1) # You can also convert numbers to string using str(): print("Converting numbers to strings:") x = str(5) y = str(10) print("x+y?", x+y) # And both numbers and string can by converted to boolean! # bool(0) and bool('') are evaluated to False, everything else to True: print("Converting numbers and strings to boolean:") print("bool(0):", bool(0)) print("bool(100):", bool(100)) print("bool(''):", bool('')) print("bool('anything else'):", bool('anything else')) # Now you have enough material to write a program with logical forks. # Try a variation hello_age.py or hello_language.py #---------------------------------------------------------------------- # 4. Additional information: # 4.1 Logical operators 'or' and 'and' evaluate as you would expect: # True and True -> True, any other combinations is False # False or False -> False, any other combination will be True. # Examples: # 10<5 and 10>5 -> False and True -> False # 10<5 or !0>5 -> False or True -> True print("\nAn example of and/or operators:") a = 10 b = 20 c = 30 if a<20 and b<20: # False and False evaluate to False print("a<20 and b<20") elif c==30 or b>20: print("c==30 or b>20") # True and False evaluate to True so this branch # will be executed else: print("something") # 4.2 You can stack your ifs: # (just don't forget to shift your blocks of code to the right!) print("\nAn example of stacking if operators:") if a == 10: if b == 20: print("branch 1") else: print("branch 2") else: if c < 100: print("branch 3") else: print("branch 4") # try this code with other values of a, b and c, and see what happens!