''' Purpose: look under hood about variables, values, objects, types, and identity ''' # define three variables x = 11 y = '11' z = 11.0 print( 'What are the values of the variables?' ) print( 'x =', x ) print( 'y =', y ) print( 'z =', z ) print() ; input( 'Enter when ready: ' ); print() ##################################################################### # Python stores values internally as objects (digital information objects) # Python keeps track of the type of value that an object hold # Python has a built-in function type() to find out the type of value # held by an object x_type = type( x ) y_type = type( y ) z_type = type( z ) print( 'What type of objects do the variables hold?' ) print( ' x holds a', x_type ) print( ' y holds a:', y_type ) print( ' z holds a:', z_type ) print() ; input( 'Enter when ready: ' ); print() ##################################################################### # The value of a Python variable is an indicator where in memory to find # its associated object. The indicator is called in Python its id. # Programmers commonly refer to an id as an address or reference. # Every Python object has a unique id. The id can be found out with # built-in function id(). x_id = id( x ) y_id = id( y ) z_id = id( z ) print( 'What are the ids of the variables?' ) print( ' x id is', x_id ) print( ' y id is', y_id ) print( ' z id is', z_id ) print() ; input( 'Enter when ready: ' ); print() ##################################################################### # Variables can indicate (reference) the same object. a = 11 b = 11 a_id = id( a ) b_id = id( b ) print( 'Can variables indicate the same object; i.e., can they reference the same object?' ) print( ' variable a has value', a, 'and id', a_id ) print( ' variable b has value', b, 'and id', b_id ) print() ; input( 'Enter when ready: ' ); print() ##################################################################### # Objects can have different occurrences of the same value. If they do their id's will be distinct a = '11' b = str( 11 ) a_type = type( a ) b_type = type( b ) a_id = id( a ) b_id = id( b ) print( 'Can objects have the same value?' ) print( ' Variable a references value', a, 'of type', a_type, 'with id', a_id ) print( ' Variable b references value', b, 'of type', b_type, 'with id', b_id ) print(); input( 'Enter when ready: ' ); print() ##################################################################### # The value of a variable can change during a program. If so, its id changes a = 11 a_id = id( a ) print( 'Can variables change their values? ' ) print( ' Variable a references value', a, 'with id', a_id ) a = 12 a_id = id( a ) print( ' Variable a references value', a, 'with id', a_id ) print()