We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Data Types

Before you manipulate data, you need to know what kind of data you're looking at.

If you think a value is a number but it's actually a string, your calculations break. If you think a field always holds a string but sometimes it's None, your cleaning logic breaks. Here's a quick refresher on the Python types you'll most often encounter in data work:

  • int – whole numbers like 42
  • float – decimal numbers like 3.14
  • str – text like "hello"
  • boolTrue or False
  • list – ordered collection of values
  • tuple – ordered collection that shouldn't change
  • set – unordered collection of unique values
  • dict – key-value pairs
  • NoneType – i.e. None, representing the absence of a value

Bad Types, Bad Calculations

Raw data almost never arrives in the exact type you want. For example:

movie = {"title": "Stranger Things", "rating": "8.7", "seasons": "4"}

Numbers as strings?! Unfortunately, this is common. If you're not careful, you might write:

boosted_rating: float = movie["rating"] + 0.5

Which will fail on a string. You need to convert the type first:

rating = float(movie["rating"])
boosted_rating: float = rating + 0.5

Assignment

SnackStack's kitchen sensors are sending readings with numbers stored as strings... classic appliance behavior.

Complete the clean_sensor_reading function. It accepts a dictionary of "raw" string-based sensor data and should return a new dictionary with the correct types.