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.0
print(9 / 4) # → 2.25
4.0
2.25

Division (/) always returns a float, even when dividing integers.

Practical Example: Temperature Conversion

# Convert Celsius to Fahrenheit
celsius = 28.5
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C = {fahrenheit}°F")
28.5°C = 83.3°F

In this example:

  • celsius stores a float value (28.5)
  • The calculation uses float arithmetic with * and /
  • fahrenheit automatically becomes a float
  • The f-string formats both numbers with decimal places
Don't worry about perfect formatting yet - we'll cover f-strings in depth later. The key concept here is how floats maintain precision through calculations.

Output:

Click "Check" to run your code.