Numerical Calculations in Python
In this example, we demonstrate the use of Python as a calculation device and how to enter some of the functions it can evaluate. Here we evaluate the expression ln(5)*e^1.4 . Note the hashtag, or pound symbol at the start of the line-this indicates a user comment which Python will ignore. We begin by entering in the Python Command Window “the natural logarithm of 5 times e to the 1.4” and pressing SHIFT/ENTER. When we do this, Python gives us an error “name e is not defined”. This is because in Python, lowercase “e” is not a number, but a variable for storing values. To determine how to enter the expression e^1.4 in Python, search online for “Python exponential function” or go to the link “Basic Functions” in the 151/152 help documentation. In doing so, you discover that e^1.4 can be entered as exp(1.4) or representing the number with a capital E and using two multiplication signs for the exponent: E**1.4. Therefore, if we enter ln(5)*exp(1.4) in our input line and then press SHIFT/ENTER, Python returns the value 4.05519996684467 log(5). If we want to convert the entire expression to a floating-point decimal, we can either change the integer 5 to a floating point value, 5.0, or put the entire expression in parentheses and ask Python to evaluate it as a floating-point decimal, evalf(). Notice the syntax for this: instead of giving the command, evalf, then the expression, we enter the expression, followed by a dot, followed by the command. The empty parentheses asks Python to use default options, in this case, the number of characters in the expression. Notice what happens if I put a number, such as 50, in the parentheses.