Hurricane Analysis Step 1 Data Science - Python

I can’t even get Step 1 completed on this project. I was able to get through the data is not retained portion. But I could not figure out the rest. I looked at the solution and have questions about it.

Specifically this part of the loop.
Why do we look for a damage ‘not equal’ to -1?
What does this portion of the code do? 0:damage.find(‘M’)
I understand it’s looking for an M but I don’t understand the ‘0:’ portion. Is it starting at index 0 and parsing until you find the ‘M’?

        if damage.find('M') != -1:
          updated_damages.append(float(damage[0:damage.find('M')])*conversion["M"])
# damages (USD($)) of hurricanes
damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M',
          '27.9M', '5M', 'Damages not recorded', '306M', '2M', '65.8M',
          '326M', '60.3M', '208M', '1.42B', '25.4M', 'Damages not recorded',
          '1.54B', '1.24B', '7.1B', '10B', '26.5B', '6.2B', '5.37B', '23.3B',
          '1.01B', '125B', '12B', '29.4B', '1.76B', '720M', '15.1B', '64.8B',
          '91.6B', '25.1B']

# 1
# Update Recorded Damages
def convert_damages_data(damages):
    """Convert damages data from string to float and return converted data as a list."""
    conversion = {"M": 1000000,
                "B": 1000000000}

    updated_damages = list()
    for damage in damages:
        if damage == "Damages not recorded":
          updated_damages.append(damage)
        if damage.find('M') != -1:
          updated_damages.append(float(damage[0:damage.find('M')])*conversion["M"])
        if damage.find('B') != -1:
          updated_damages.append(float(damage[0:damage.find('B')])*conversion["B"])
    return updated_damages

Link to project

damage.find('M') would be -1 if ‘M’ was not found in the damage string.
Otherwise, damage.find('M') gives you the index of the first M in the damage string.

damage[0: index_of_M] would gives you the part of the damage string form 0 to just before the index index_of_M.
It’s called string slicing, I think.

2 Likes