Post

Python Programming
Python Programming@PythonPr·
Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Python Programming tweet media
English
20
9
111
25.7K
Sergei Kotov
Sergei Kotov@kotov_dev·
Answer: D) False The 95% beginners trap: floating point precision. › 0.1 + 0.2 isn't exactly 0.3 but a little bit more — because floats in binary use fixed memory cells to store, causing tiny errors › That's ok precision for most operations Pro tip: never store money as float — rounding errors in financial calculations can cause real legal issues. Use the decimal module instead.
English
0
1
9
2K
PyBerry Tech 🐍🍓
PyBerry Tech 🐍🍓@PyBerryTech·
Option D : False Why??👇 In Python (and most programming languages), floating-point numbers like 0.1 and 0.2 cannot be represented exactly in binary. 0.1+0.2 interpret something like 0.30000000000001 ✅ Tip: For such cases use math module. import math math.isclose(0.1 + 0.2, 0.3) # True
English
0
0
10
2.3K
Jimmy Fikes
Jimmy Fikes@akajim·
@PythonPr D. You’re dealing here with low precision floats. Never use floats if you’re sending a rocket to the moon - it might hit the sun first.
English
1
0
8
944
Muhammad Yusuf
Muhammad Yusuf@quarksci·
Correct answer: False ✅ 0.1 + 0.2 ≠ 0.3 in Python (and most programming languages) because of floating-point precision. In binary, 0.1 is actually ~0.10000000000000000555… and 0.2 is ~0.2000000000000000111…, so their sum ends up ≈ 0.30000000000000004 That's why 0.1 + 0.2 == 0.3 → False Quick fixes: • Use round(0.1 + 0.2, 1) == 0.3 • Better: from math import isclose → isclose(0.1 + 0.2, 0.3) Classic gotcha that bites everyone at least once
English
0
0
3
1.8K
Kamal Gurjar
Kamal Gurjar@KamalGurjar8·
@PythonPr Output D) False. This is the classic floating-point arithmetic trap! Because of how floats are represented in binary, 0.1 + 0.2 actually evaluates to 0.30000000000000004 under the hood, so it doesn't strictly equal 0.3.
English
0
0
2
1.3K
Abdullah Munib
Abdullah Munib@munibcodes·
@PythonPr D) False Because of floating-point precision. Computers store decimal numbers in binary, and some decimals (like 0.1 and 0.2) cannot be represented exactly in binary form. So Python is basically checking: 0.30000000000000004 == 0.3 which is false
English
0
0
2
335
Smart💡
Smart💡@capt_ivo·
@PythonPr False. They are infinite recurring fractions, and not binary. Use this instead 👇👇 from math import isclose print(isclose(0.1 + 0.2, 0.3)) Output: True Or you can enclose on abs()
English
0
0
0
54
Teilen