Limit Decimal Places (Solution)
00:00
The mathematical expression to evaluate in this exercise reads as 3
to the power of .125
. The exponent in this case is a fraction that you can write as 1 / 8
.
00:14
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 3
.
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.
01:28
Now, I can rewrite the same computation by wrapping the values in the special Decimal
data type.
01:40 As you can see, the decimal expansion of the result just goes on and on because there are an infinite number of digits in it. Anyway, back to the exercise.
01:50 Let’s restore the original expression, and by the way, you can include or skip the leading zero in a fractional number like this.
01:59
Now, there are several ways to format a floating point number in Python. You can, for example, call the built-in format()
function with a math expression as an argument.
02:09
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.
02:29
If you want to format the value as a floating-point number, then you must use the letter f
. When you do, it’ll round the number and display up to six decimal places by default.
02:41 To change this default behavior, you can add a prefix to the formatting string that starts with a dot, followed by the desired number of decimal places, which is three in this case.
02:53
Another way to format a number in Python is by calling the format()
method on a string literal representing how you want the number to be formatted.
03:02 The number to format is the result of our math expression, and the format has to go inside of curly brackets with an extra colon.
03:15
However, in practice, you’ll often want to use f-strings, which have the most compact and flexible syntax. You can move the format()
method’s argument inside of the curly brackets,
03:28
prefix the string literal with a letter f
, and remove the call to format()
.
03:35
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.
03:55 All right, that concludes this exercise, but you can do so much more with the format specification mini-language in Python. Head over to the next lesson to practice it some more.
Become a Member to join the conversation.