#! /usr/bin/python3 # ================================================================== # # Notes: # #------------------------------------------------------------------- # The difference between a for loop and a while loop: # # Loops are used for repeating sections of code. Unlike a for # loop, the while loop will not run n times. While loops run # until a defined condition is no longer met. If the condition # is initially false, the loop body will not be executed at all. # # ------------------------------------------------------------------ # Using else Statement with Loops: # # Python supports to have an else statement associated with a # loop statement # # - If the else statement is used with a for loop, the else # statement is executed when the loop has exhausted iterating # the list. # # - If the else statement is used with a while loop, the else # statement is executed when the condition becomes false. # # ================================================================== # lx list # tx tuple # dx dictionary # sx string lx = ['a','b','c','d','e'] tx = ('aa','bb','cc') dx = { 'A':'aaa','B':'bbb','C':'ccc'} sx = 'abcdef' print('-----string-------------------------------------------') l = len(sx) print('sx length is ',l, ' type is ',type(sx)) for c in sx: print(c) for idx,c in enumerate(sx): print(idx,c) print('-----list---------------------------------------------') l = len(lx) print('lx length is ',l, ' type is ',type(lx)) for x in lx: print(x) for idx,x in enumerate(lx): print(idx,x) print('-----tuple--------------------------------------------') l = len(tx) print('tx length is ',l, ' type is ',type(tx)) for x in tx: print(x) for idx,x in enumerate(tx): print(idx,x) print('-----dictionary---------------------------------------') # note: # python3 - use dx.items() # python2 - use dx.iteritems() l = len(dx) print('dx length is ',l, ' type is ',type(dx)) for k in dx: print('key=',k,' value=',dx[k]) for k,v in dx.items(): print(k,v) print('-----loop else----------------------------------------') print('for loop example') for i in range(4): print(i) else: print('end of for loop, i=',i) print('while loop example') i = 97 while i < 100: print(i) i += 1 else: print('end of while loop, i=',i)