FAQ: Modules: Python - Modules Python Decimals

This community-built FAQ covers the “Modules Python Decimals” exercise from the lesson “Modules: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Modules Python Decimals

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: Is three decimal places part of the question?

3 posts were merged into an existing topic: Is three decimal places part of the question?

5 posts were split to a new topic: How can we control the rounding of our equation?

It seems that Decimal module is necessary for almost every math operation we calculate. But why we rarely see this module in real projects?

2 Likes

H there - quick question I hope.

Why in this exercise did we not have to identify the decimal module in namespace when using the Decimal function?

for example in the previous exercise when using the randint function we used it as random.randint() so I thought it would be a similar syntax for Decimal like decimal.Decimal()

The syntax

from <library> import <function>

brings that function into your program’s namespace, as though you had written the function yourself at the top of your program. In previous examples, we needed to specify which of our imported libraries we were referencing.

1 Like

I understand the usefulness of the decimal module but, contrary to the exercise, Python on my local machine does not return strange answers to decimal addition as was spoken of in the tutorial. Why is this?

What happened with these lessons? We go from the previous module to ‘Modules’ with explanations on the syntax of what we’re using, and then get asked to use some decent logic to solve many of the problems, and then this whole unit they haven’t explained a ■■■■ thing! I’ve had to scour the internet for a half hour for each of the 4 pages so far and still haven’t found the correct syntax for any of these functions. I sure hope this improves

10 Likes

Same here. This lesson doesn’t make sense, but all the previous ones were crystal clear

3 Likes

I agree. This exercise was a bit strange and it seemed to be lacking explanations. I also still don’t understand why we have to turn the floats into strings in order for the Decimal() module to work.

7 Likes

This is kind of insane. There’s been no instruction on how to specify decimal points desired, and when you get the solution code, I feel like I see no difference at all. There have been quite a few poorly explained lessons in Codecademy so far but this one is REALLY bad, and is brushing over almost everything it addresses, if it even touches on it at all.

This lesson is the equivalent of hey there’s this thing called decimal, import it and use it.

$40 ■■■■■■■ dollars a month for that eh?

4 Likes

Hey all,

Quick question: the code

</> two_decimal_points = Decimal(‘0.2’) + Decimal(‘0.69’)

print(two_decimal_points)

four_decimal_points = Decimal(‘0.53’) * Decimal(‘0.65’)

print(four_decimal_points) </>

prints out 0.89 and 0.3445 accordingly. I’m just curious as to why four_decimal_points behaves the way it does. Is it something to do with the code within the library, or simply just multiplication vs addition?

0.53 * 0.65  ==  0.3445

The function likely has some stuff under the hood that truncates the floating point error.

>>> 0.53 * 0.65
0.34450000000000003
>>> 
1 Like

So, then, if this isn’t the default Decimal module , then why are we using it? And if it is, I still don’t understand how it knew to give a two decimal answer for:
two_decimal_points = Decimal('0.2') + Decimal('0.69')
print(two_decimal_points)

and a four decimal answer for:

four_decimal_points = Decimal('0.53') * Decimal('0.65')
print(four_decimal_points)

Which was basically what grmmf14 was asking.

1 Like
>>> 10 ** -1 + 10 ** -2  == 0.11
True
>>> 
>>> 10 ** -2 * 10 ** -2  ==  10 ** -4
True
>>> 0.01 * 0.01  ==  0.0001
True

That might be how it knows.

2 Likes

It seems that Decimal is used when the number is known. Can the function be used for unknown numbers i.e. any variable?

Assallam Aleikum awesome people.

can someone please explain kindly why this code wouldn’t work for the proposed question? thanks!

Import Decimal below:

from decimal import Decimal

Fix the floating point math below:

two_decimal_points = Decimal(‘0.2 + 0.69’)

print(two_decimal_points)

four_decimal_points = Decimal(‘0.53 * 0.65’)

print(four_decimal_points)

Doesn’t look right, and neither does this,

With the operator written in the string there is no way to mathematically evaluate it.

1 Like

Why are the arguments passed through Decimal strings (Decimal('3.14')) rather than floats (Decimal(3.14))?

1 Like