## module 3, example - not needing selection statement x = 5 # I want to make sure that x is not smaller than 0 # One way of achieving this: if x<0: x = 0 # A better way of achieving the same: x = max(x,0) #%% module 3, example - short-circuit operators name = 'Vedrana' # I want to check if the name starts with 'Ved'. But what if name has less than # 3 letters? I need to make sure that name[:3] is not evaluated if name is too # short. I add the first condition and the second condition will not be # evaluated if the first is false. if len(name)>2 and name[:3]=='Ved': print('Cool') #%% module 3, example -- an extra condition x = 4 # I want to check whethr x is bigger than 5 # A bad way of achieving this: if x>5: print('Bigger than 5') elif x<=5: print('Smaller or equal 5') # A better way of achieving this: if x>5: print('Bigger than 5') else: print('Smaller or equal 5') #%% module 3, example -- nothing to fall to # Make sure that return value is assigned in the function call def give_verdict(x): x = '' if x>0: verdict = 'Bigger than 0' elif x<0: verdict = 'Smaller than 0' return verdict print(give_verdict(0)) #%% module 3, example -- using boolean flags is_rgb = False if is_rgb: # dont write "if is_rgb==True" print('special treatment for color images') else: print('no special treatment') #%% module 3, example -- debugging, line by line x = 41.7 if x > 137/4: x = 2*x + 154/18 elif x< 117/4: x = x + 154/18