Limit Decimal Places (Solution)
It gives you the same number, only written as a fraction instead of using decimal notation. Now, because fractional exponents correspond to calculating the root of a number, this example is equivalent to finding the eighth root of the number
00:31 When you evaluate this expression in the Python shell, it results in an irrational number with an infinite non-recurring decimal expansion. However, because of the limited memory space, Python only shows an approximation of the actual result, which gets rounded to some predefined number of decimal places.
00:51 What you’re seeing here isn’t a mathematically exact result of that computation. In fact, the only way to accurately convey an irrational number such as this would involve symbolic algebra. If you don’t believe me, I can show you the same calculation using a more advanced technique, which entails the decimal data type in Python that was briefly mentioned in the Python basics course.
01:15 You can completely ignore what I’m about to show you. This is just to illustrate the problem with irrational numbers in computers and calculators. Let’s increase the precision to something like a thousand decimal places.
This turns the result into a Python string, which is indicated by the single quotes around the printed value. You can specify an optional second parameter to the
format() function, which must be a string conforming to a special format specification mini-language, which was covered in the corresponding Python basics course.
Note that we’ve been running these code snippets in Python’s interactive shell, which evaluates each expression on the fly. However, if you were to put this code in a Python script, then you’ll also want to wrap each line in a call to the
print() function to see the result. Otherwise, your code would have no visible effect.
Become a Member to join the conversation.