Calculate time dilation with python

In this example imagine two object one is Earth And another object is traveling in the space

\[ t_0 = t \sqrt{1 – \frac{v^2}{c^2}} \]
  • t0: Represents the dilated time experienced by the object which is traveling or seems traveling from earth
  • t: Time passed on earth
  • v: Velocity of the traveling object.
  • c: Speed of light in the vacuum.

Now back to our example. Here user will enter the time passed on earth and the speed of the traveling object. The program will calculate the time dilation.


from math import sqrt
c=299792458 #speed of light in m/s

t=eval(input("Enter the time passed on earth in year: "))
v=c
while not v<c:
	v=eval(input("Enter the speed of the object in m/s: "))
	if v>c:
		print("Speed of the object must be less than the speed of light:",c)
def time_dilation(c,v,t):
	t_0=t*sqrt(1-(v**2/c**2))
	return t_0
time_passed_to_object=time_dilation(c,v,t)
print("time passed to the object",time_passed_to_object," year")
print("time difference",t-time_passed_to_object," year")


For example if time pass on earth 20 years and the object is traveling in speed 7497924 m/s

The program will print this result based on user input

Enter the time passed on earth in year: 20
Enter the speed of the object in m/s: 7497924
time passed to the object 19.993743829255635  year
time difference 0.006256170744364908  year