''' Purpose: demonstrate arithmetic and use of variables for naming things Something to remember: Python represents an integer as an int ''' # ... is ellipsis (it's a placeholder that says "I will fill this in later!") # name some values a = 7 # integer b = 2 # integer c = -3 # negative integer d = 99.9 # decimal # ASSIGNMENT # Uses = to assign # variable/identifier = value we want to assign # Ex. a = 24 # Whatever is on the left is assigned the value on the right! print() print( 'Values of interest' ) print() print( 'a =', a ) # a = 7 print( 'b =', b ) # b = 2 print( 'c =', c ) # c = -3 print( 'd =', d ) # d = 99.9 # Breakdown: # print( 'a=', a ) # string variable a that holds a number value # Gets printed as a = 7 (space in between 'a=' and 7 # You must assign the variable before you print it! # print(x) # Doesn't know what x is?? We didn't assign x to anything! # print( 2, 'hello', 3.35 ) SEPARATE DIFFERENT DATA TYPES / MULTIPLE THINGS WITH COMMAS # prints # 2 hello 3.35 # If you have a comma in the print statement, it will put a space between the things you want to print. print() reply = input( 'Enter when ready: ' ) print() # perform and display some integer calculations total = a + b # addition difference = a - b # subtraction product = a * b # multiplication dec_quotient = a / b # decimal/ "float" division int_quotient = a // b # integer division remainder = a % b # Remainder (modulus operator) 6%4 = 1 Remainder 2 = 2 power = a ** b # Exponentiation 3 ** 2 = 3 ^ 2 = 9 print( 'a + b =', total ) print( 'a - b =', difference ) print( 'a * b =', product ) print( 'a / b =', dec_quotient ) print( 'a // b =', int_quotient ) print( 'a % b =', remainder ) print( 'a ** b =', power ) # IMPORTANT: # Python can't represent an infinite number of decimal places! # Floats (floating points) are decimals! # We can only represent a finite number of decimals places! # Python usually about 16-17 decimal places of accuracy (about 60 bits). This has shown up on previous tests :) # Remember that Python can't represent an infinite number of decimal places! # DIVISION # / is decimal division # // is integer division (it chops off the decimal part of your division!) # 3/2 = 1.5 (decimal) # 3//2 = 1.5 = 1 (integer) print() reply = input( 'Enter when ready: ' ) print() # introduce some other built-in functions abs_c = abs( c ) # gives you absolute value of c float_c = float( c ) # gives you float conversion of c int_d = int( d ) # gives you integer conversion of d print( 'abs( c ) =', abs_c ) print( 'float( c ) =', float_c ) print( 'int( d ) =', int_d ) print()