Aan de slagGa gratis aan de slag

Printing floats

Scientific notation is a powerful tool for representing numbers, but it can be confusing to handle when trying to print float values. However, we can use the f strings we learned about previously to make sure we get them printed properly every time by using a format specifier. For example, if we wanted to format a variable in an f string as a float, we can use the f format specifier, such as: print(f"{some_variable:f}"). It also takes an operation precision on the float format specifier, for example, print(f"{some_variable:.4f}") would print four decimal places of precision.

Deze oefening maakt deel uit van de cursus

Data Types in Python

Cursus bekijken

Oefeninstructies

  • Print float1, float2, and float3 notice where the jump to scientific notation occurs.
  • Print float2 and float3 using the default float format specifier, and notice what happened to float3.
  • Print float3 with the float format specifier and a precision of 7.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Print floats 1, 2, and 3
print(____)
print(____)
print(____)

# Print floats 2 and 3 using the f string formatter
print(f"{____}")
print(f"{____}")

# Print float 3 with a 7 f string precision
print(f"{____}")
Code bewerken en uitvoeren