Python Program to Find Gravitational Force between Two Objects

This tutorial is going to show you a simple Python program to calculate the Gravitational Force between two objects.

To find out the gravitational force between two objects, for example, two planets or between a planet and the sun we need some values that are given below:

  • Mass of those two objects. We will store these values in the variables m1 and m2 for the first and the second object accordingly. In our program, the units of these masses are going to be in KG.
  • Distance between the centres of these two objects. r is going to be the variable name to store it.
  • Gravitational constant which is 6.673*(10**-11). We will store it in the variable G.

In our example, we are going to take m1, m2 and r from the user input.

Now let’s see the Python program to calculate the gravitational force:

m1=float(input("Enter the mass for first object's mass: "))
m2=float(input("Enter the mass for second object's mass: "))
r=float(input("Enter the distance between the centres of the masses of the objects: "))
G=6.673*(10**-11)
f=(G*m1*m2)/(r**2)
print("The gravitational force between these masses is: ",round(f,2),"N")

Sample output:

Enter the mass for first object's mass: 9854000
Enter the mass for second object's mass: 690984755000
Enter the distance between the centres of the masses of the objects: 45898
The gravitational force between these masses is: 0.22 N

As you can see in the above sample output, we have successfully been able to find the gravitational force with the help of a Python program of a few lines.

So what we did do in our program?

I am sure you have got the formula in Physics in high school. The formula is:

F=(G*m1*m2)/(r**2)

Where,

  • F is the gravitational force
  • G is the gravitational constant
  • m1 and m2 are the masses of two objects
  • And finally, r is the distance between centers of the masses of two objects.

Leave a Reply

Your email address will not be published. Required fields are marked *