Problem in task 1 week9 from data science class

can anyone please tell me how to write the code for task 1 of week 9 ,using function.

The Task is to calculate the densities of the given heavenly bodies i.e. :

['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto', 'Moon']
  • First, we’ll have to collect data with regard to each of the above’s diameters. They are found to be (ignore the units):
[0.3300, 4.8700, 5.9700, 0.6420, 1898.0000, 568.0000, 86.8000, 102.0000, 0.0146, 0.7346]

respectively.

  • Then we could do the density calculation through the iterative method using the iterrows function however, this isn’t efficient. Therefore, an optimal solution is to directly find the density in a one-liner as follows:
density = mass / ((3.14159 * diameter**3) / 6)

Hope this clarifies the doubt.

1 Like

thank you for your reply.
But i want to solve this problem using a function.
like cant we create a function ,so that when we call that function it will ask us the name of the planet and later give us the density of only that planet.

Yes, you can do it for sure.
You can pass the planet name as an argument to the function, and inside the function you can use pandas operation to access that particular value by filtering the dataframe.
I would suggest you to try this at your own, and let us know if you face any difficulty in implementing it.

1 Like

@sarthak511,

Sorry for the miss understanding, however as @Ishvinder stated you would have to give the function an argument of the planet’s name and give it the diameter’s and the mass data.

The function signature is to be as follows:

def planet_density (name, diameter, mass)