In this simple python program, we need to calculate the gravitational force. It is a beginner-level python program.
For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:
Gravity is the force that attracts every object to the earth, in this python program we need to calculate the gravitational force between two bodies. Gravitational force act only on the center of the mass of the objects. The force of gravity between two objects is inversely proportional to the square of the distance between them. Here in this python gravitational problem, we need to check the force between the objects which can be found by using the formula.
F = f=(G*m1*m2)/(r**2), where the
To apply this logic in our python programming language, we accept the masses m1, m2, and the distance r from the user using float data type which supports the decimal values. Then we need to apply the formula F = f=(G*m1*m2)/(r**2). Before that, we need to calculate the gravitational constant G. After calculating the gravitational force, and we print the result using
format method to make the value precision of two decimal places using python syntax.
STEP 1: Accept the values of two masses and the distance between the center of the masses using the input method and convert that string to float using float data type in python programming.
STEP 2: Calculate the value of G the gravitational constant using the formula G = 6.673*(10**-11). This is needed for calculating the force.
STEP 3: Calculate the gravitational force using the formula f=(G*m1*m2)/(r**2), where m1 and m2 are the masses and r is the distance between the center of masses.
STEP 4: Using python programming basics print the value of the gravitational force using the round method two make the result exactly two decimal values.
m1=float(input("Enter the first object: ")) m2=float(input("Enter the second object: ")) r=float(input("Enter the distance between the center of the objects: ")) G=6.673*(10**-11) f=(G*m1*m2)/(r**2) print("The gravitational force between objects is: ",round(f,2),"N")
Enter the first object: 1000000 Enter the second object: 500000 Enter the distance between the center of the objects: 20 The gravitational force between objects is: 0.08 N