# ================================================================== # Python has for loops rather than foreach loops # ================================================================== import os import sys # ------------------------------------------------------------------- # support functions # ------------------------------------------------------------------- def RunningPython3(): ##print(sys.version_info) if sys.version_info[0] == 3: return True return False def GetUerInput(prompt): return imput(prompt) def Pause(): GetUserInput('Press enter to continue') def FileExists(fil): if not fil: return False if not os.path.isfile(fil): return False return True # --- failure test def DivideByZero(x): print('DivideByZero({})'.format(x)) return x / 0.0 # ------------------------------------------------------------------ # main # ------------------------------------------------------------------ # --- running Python 3? py3 = RunningPython3() if not py3: print('Not running Python 3 - exit') exit(1) d = { 'a' : 'z', 'b' : 'y', 'c' : 'x' } # --- loop thru keys print('---- Key only loop -----------------------') for i in d.items(): print('key = {}'.format(i[0])) print('----------') for k in d.keys(): print('key = {}'.format(k)) # --- loop thru values print('---- value only loop ---------------------') for i in d.items(): print('value = {}'.format(i[1])) print('----------') for v in d.values(): print('value = {}'.format(v)) # --- loop thru keys and values print('---- key/value loop ----------------------') for k,v in d.items(): print('key = {} value = {}'.format(k,v))