Convert Celsius to Fahrenheit
C in this case is the
temp_cel variable. So here now you can be very strict with Python formatting and put a space between the slash of
9 / 5 or even put parentheses surrounding it. For the calculation, it doesn’t matter, so here it’s more stylistic if you find it a bit better to read than how it was in the challenge text.
01:37 Remember that I said just a few moments ago that the parentheses are purely aesthetic. Apparently, they are not just purely aesthetic, but it also defines in which order Python calculates the values.
02:05 That may be a bit more specific here. When you divide two integers in Python, the result can be a floating-point number. The representation of this number can sometimes include many zeros after the decimal point due to how floating-point arithmetic works in computers because computers use the binary system, which is a base-2 system, to represent numbers, while we humans—and I assume you are a human watching this—typically use the decimal, which is a base-10 system.
02:37 So when converting numbers between those two systems, some numbers that are easily represented in decimal cannot be precisely represented in binary. As a result, the binary representation can sometimes be an approximation, and that’s why you see the bunch of zeros and the one in this result, depending on if you put the parentheses in there or not.
03:03 If you want to learn more about the floating-point number representation, check out the Python Basics course on numbers and math, where Bartosz explains this topic way better than I did right now.
So if you take away the parentheses and make exactly the same function call with
37, then you get
98.6 without the zeros and the one because the order of how Python executes this formula is different.
And again, you can try this out here in the IDLE shell. So let’s dissect this formula in the shell and re-create it bit by bit. First, we pass in
37, and then it’s multiplied by
9, which is
So here it doesn’t entirely work. So let’s see what
9 divided by
5 is, which is
1.8. And apparently, this one here is the culprit because when you multiply
1.8, then the conversion isn’t clean anymore, and Python needs to do an approximation.
05:14 So you don’t need to care that much about this approximation here. And I like the look with the parentheses more. So I will add the parentheses again and continue with this formula in the next lesson.
Become a Member to join the conversation.