Section 1/2Overview
Overview
Float fundamentals
Working with Floating-Point Numbers
While integers handle whole numbers, floating-point numbers (floats) let us represent decimal values. Understanding floats is crucial for precise calculations in science, finance, and statistics.
What are Floats?
Floats are numbers with decimal points. In Python:
3.14159→ Float (decimal)4.0→ Float (even with .0)5→ Integer
Division and Floats
print(8 / 2) # → 4.0print(9 / 4) # → 2.25
→
4.02.25
Division (/) always returns a float, even when dividing integers.
Practical Example: Temperature Conversion
# Convert Celsius to Fahrenheitcelsius = 28.5fahrenheit = (celsius * 9/5) + 32print(f"{celsius}°C = {fahrenheit}°F")
→
28.5°C = 83.3°FIn this example:
celsiusstores a float value (28.5)- The calculation uses float arithmetic with
*and/ fahrenheitautomatically becomes a float- The f-string formats both numbers with decimal places