Python Program to Find the Gravitational Force Acting Between Two Objects

When calculating the gravitational force between two objects, we can use Newton's Law of Universal Gravitation. This law states that every particle attracts every other particle with a force proportional to the product of their masses and inversely proportional to the square of the distance between them.

The formula is: F = G × (m? × m?) / r², where G is the gravitational constant (6.673 × 10?¹¹ N?m²/kg²).

Example

Here's how to calculate gravitational force using a Python function ?

def find_gravity(m_1, m_2, r):
    G_val = 6.673*(10**-11)  # Gravitational constant
    F_val = (G_val*m_1*m_2)/(r**2)
    
    return round(F_val, 2)

# Define masses in kg and distance in meters
m_1 = 6000000  # Mass of first object (kg)
m_2 = 1000000  # Mass of second object (kg)
r = 45         # Distance between objects (meters)

print("The gravitational force is:")
print(find_gravity(m_1, m_2, r), "N")
The gravitational force is:
0.2 N

How It Works

The function find_gravity() takes three parameters:

  • m_1: Mass of the first object (in kilograms)
  • m_2: Mass of the second object (in kilograms)
  • r: Distance between the centers of the objects (in meters)

The gravitational constant G = 6.673 × 10?¹¹ N?m²/kg² is used in the calculation. The result is rounded to 2 decimal places for readability.

Real-World Example

Let's calculate the gravitational force between Earth and Moon ?

def find_gravity(m_1, m_2, r):
    G_val = 6.673*(10**-11)
    F_val = (G_val*m_1*m_2)/(r**2)
    
    return F_val

# Earth and Moon data
earth_mass = 5.972e24    # kg
moon_mass = 7.342e22     # kg
earth_moon_distance = 3.844e8  # meters

force = find_gravity(earth_mass, moon_mass, earth_moon_distance)
print(f"Gravitational force between Earth and Moon: {force:.2e} N")
Gravitational force between Earth and Moon: 1.98e+20 N

Conclusion

Newton's Law of Universal Gravitation can be easily implemented in Python to calculate gravitational forces. The function demonstrates how massive objects exert significant forces even at large distances, as shown in the Earth-Moon example.

Updated on: 2026-03-25T19:23:28+05:30

527 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements