#!/usr/bin/python3 # -*- coding: utf-8 -*- from typing import List # %% Control Flow and Iterables """ To jump around to other lines of code, rather than execute a strict sequence. """ # %% Conditionals / Branching """ if : Things that evaluate to False: any number equal to zero (0, 0.0, 0+0j) an empty container (list, tuple, set, dictionary, …) False, None Things that evaluate to True: everything else """ # Indentation matters x: int = 1 if x == 1: # indented four spaces, not \t tabs print("x is 1.") # Don't need type-hints after first instance. x = 2 print(x == 2) # prints out True print(x == 3) # prints out False print(x < 3) # prints out True name: str = "John" age: int = 23 if 37: print("eh?") if name == "John" and age == 23: print("You are John, and you are 23") if name == "John" or name == "Rick": print("Your name is either John or Rick.") # Handy in operator (more to come later) if "string" in "longerstring": print("found it a sub-sequence") if 3 not in [4, 5, 6]: print("it's not in there") name = "John" if name in ["John", "Rick"]: print("Your name is either John or Rick.") x = 2 if x == 2: print("x equals two!") else: print("x does not equal to two.") # elif if 0 and 1: print("and") elif 0 or 1: print("or") else: print("nothing above") # elif equivalence if 0 and 1: print("and") else: if 0 or 1: print("or") else: print("nothing above") # Let's just make a variable some_var: int = 5 # Here is an if statement. Indentation is significant in Python! # Convention is to use four spaces, not \t tabs. # This prints "some_var is smaller than 10" if some_var > 10: print("some_var is totally bigger than 10.") elif some_var < 10: # This elif clause is optional. print("some_var is smaller than 10.") else: # This is optional too. print("some_var is indeed 10.") # ternary statemnts user_val: int = 4 print("neg") if (user_val < 0) else print("pos") # if can be used as an expression # Equivalent of C's '?:' ternary operator print("yay!") if 3 > 2 else print(2) # => "yay!" print("yay!") if 3 < 2 else print(2) # => "2" print(not False) # Prints out True print((not False) == (False)) # Prints out False # short circuiting (protect against 0 denominator here?) # evaluated greedily, left to right x = 2 y: int = 0 # this produces an error # >>> print(x / y) # Will these too? print((x >= 2) and (y != 0) and ((x / y) > 2)) print((x >= 3) or (y != 0) and ((x / y) > 2)) # How about this? x = 7 y = 9 print((x >= 2) and (y != 0) and ((x / y) > 2)) print((x >= 3) or (y != 0) and ((x / y) > 2)) # While the below example is a look-ahead preview, # it illustrates that == is not the same as is. # Unlike the double equals operator "==", # the "is" operator does not match the values of the variables, # but the instances themselves (their ids). For example: xarr: List[int] = [1, 2, 3] yarr: List[int] = [1, 2, 3] print(xarr == yarr) print(xarr is yarr) print(xarr is not yarr) # Assignment like this makes a reference xarr = yarr print(xarr == yarr) print(xarr is yarr) print(xarr is not yarr) # copy xarr = yarr[:] print(xarr == yarr) print(xarr is yarr) print(xarr is not yarr) var1 = 100 if var1: print("not 0 - Got a true expression value") print(var1) var2 = 0 if var2: print("not 0 - Got a true expression value") print(var2) print("Good bye!") amount = int(input("Enter amount: ")) if amount < 1000: discount = amount * 0.05 print("Discount", discount) else: discount = amount * 0.10 print("Discount", discount) print("Net payable:", amount - discount) # nested num = int(input("enter number")) if num % 2 == 0: if num % 3 == 0: print("Divisible by 3 and 2") else: print("divisible by 2 not divisible by 3") else: if num % 3 == 0: print("divisible by 3 not divisible by 2") else: print("not Divisible by 2 not divisible by 3")