''' Purpose: introduction to dictionary' ''' # mappings[ key ] = value # CREATES A NEW MAPPING IN DICTIONARY mappings = {} # Initialize an empty dictionary mappings[ 0 ] = 'even' mappings[ 1 ] = 'odd' mappings[ 2 ] = 'even' mappings[ 3 ] = 'odd' mappings[ 4 ] = 'even' mappings[ 5 ] = 'odd' mappings[ 6 ] = 'even' mappings[ 7 ] = 'odd' mappings[ 8 ] = 'even' mappings[ 9 ] = 'odd' mappings[ 'even' ] = 'a number is even if its remainder divided by 2 is 0' mappings[ 'odd' ] = 'a number is odd if its remainder divided by 2 is 1' reply = input( 'Enter a number: ' ) n = int( reply ) # These two ways get the value for a particular key. # One is a function that returns None if the key doesn't exist - the other # won't give you None. # status = mappings[ n ] # GET THE DICTIONARY VALUE FOR THE KEY N status = mappings.get( n ) # If key is not in dictionary, the mapping doesn't # exist so .get() hands back None. # The first way will not give you None it will just # throw an error if you don't have the key in the dict. print( n, status ) # PRINT THE NUMBER AND THE VALUE IN THE DICTIONARY FOR # THAT NUMBER AS A KEY reply = input( 'Enter even or odd: ' ) meaning = mappings[ reply ] print( meaning )