""" Purpose: demonstrate arithmetic """ # Name some variables and print their values a = 20 b = 3 # you cannot have an integer or float expression with unknown variables print( "Values of interest" ) print( "a:", a ) print( "b:", b ) print() # Perform calculations # all of these are variables storing the results of the computations total = a + b # addition difference = a - b # subtraction product = a * b # multiplication dec_quotient = a / b # normal division int_quotient = a // b # I call this floor division, since it just throws the remainder away power = a ** b # exponentiation -> a^b the carat does not work for exponentiation, only ** a ** b ** c is valid (order of operations we'll get to later) remainder = a % b # remainder of division, ex. 7%2: 7/2 = 3 with a remainder of 1, so 7%2 = 1 # math never goes away, we'll be doing arithmetic for the entire semester # these are building blocks that we'll use later to solve more interesting problems # no calculus :) # whitespace is ignored but can greatly help with readability # think back to learning long division, that's where remainder comes from # % is remainder, you have to write code to calculate percentages # only put integers in the remainder operator this isn't math class # order of operations - we can still use () to group -> "grouping operator" # roots are raised to fractional powers - square root is equal to raising something to the 1/2 power # make sure you group 1/2 if you use it as your exponent! # [] is a list, and # we like to take our time and not do too many things on one line # separate calculations from the display, it will help you catch mistakes - no calculations in print() statements # no calculations in print() statements is a style choice that we use, python doesn't care # we aren't here to learn how to write efficient code, we're here to learn how to write code :) # there is a round() function that we'll see soon! # Print results print( "Results" ) print( "a + b: ", total ) # the strings are to label and organize the output, a + b = 23 is more descriptive than 23 print( "a - b: ", difference ) print( "a * b: ", product ) print( "a / b: ", dec_quotient ) print( "a // b: ", int_quotient ) print( "a ** b:", power ) print( "a % b: ", remainder ) # variables have types, and different types of data are stored in different ways # strings are stored in quotes, as we've seen # "3" =/= 3 , "2" + 2 =/= 4 - numeric strings are different from integers # floats and ints play together a lot more nicely :) # anything in single or double quotes is a string, and python will always just spit it out as is, not parse it as code # when doing arithmetic, any decimal will cause the output to be a decimal # it's not sig figs, it's the data type - floats and ints are stored differently in computer memory # every CS class you take at UVA that introduces a new language will teach you the syntax