List Comprehension and Numerical, Graphical, and Symbolic Limits
In this example, we use an important Python technique called list comprehension to help us numerically estimate a limit, as well as use Python to graphically estimate a limit and find the exact limit. Suppose we want to find the limit of sqrt(x) - 1 over x^3 - 1 as x approaches 1. To numerically estimate the limit, we choose values of x which get closer and closer to 1, such as 0.9, 0.99, 0.999, and 0.9999 (from the left) and 1.1, 1.01, 1.001, 1.0001 (from the right). List comprehension allows us to evaluate ALL the values in one command. First, we define x as a symbolic variable, then define our expression (call it f). Now we create a list of x values, which we will call xval. Notice the square brackets around the comma-separated list. To substitute all of these at once, we use the command yval = [f.subs(x,i) for i in xval]. We are literally defining yval as a list (so square brackets are needed) and into f we substitute x = i for every element i in the list xval. If we print yval, we see that the first 4 y-values (as x approaches 1 from the left) appear to be getting close to .16666 and the last 4 y-values (as x approaches 1 from the right) appear to be getting close to .16666. We can confirm this graphically by using the plot command, choosing a domain around x=1 such as [0,2]. Finally, we can compute the exact limit using Python’s limit command. We can even specify the left-hand or right-hand limits using the ‘-’ or ‘+’ option.