Skip to content

Commit 59dd918

Browse files
Refine docstring and examples for kinetic_energy function
Updated the docstring to improve clarity and added a formula for kinetic energy. Adjusted parameter descriptions and examples for better understanding.
1 parent 678dedb commit 59dd918

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

physics/kinetic_energy.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,56 @@
11
"""
2-
Find the kinetic energy of an object, given its mass and velocity.
3-
4-
Description : In physics, the kinetic energy of an object is the energy that it
5-
possesses due to its motion.It is defined as the work needed to accelerate a body of a
6-
given mass from rest to its stated velocity.Having gained this energy during its
7-
acceleration, the body maintains this kinetic energy unless its speed changes.The same
8-
amount of work is done by the body when decelerating from its current speed to a state
9-
of rest.Formally, a kinetic energy is any term in a system's Lagrangian which includes
10-
a derivative with respect to time.
11-
12-
In classical mechanics, the kinetic energy of a non-rotating object of mass m traveling
13-
at a speed v is ½mv².In relativistic mechanics, this is a good approximation only when
14-
v is much less than the speed of light.The standard unit of kinetic energy is the
15-
joule, while the English unit of kinetic energy is the foot-pound.
16-
17-
Reference : https://en.m.wikipedia.org/wiki/Kinetic_energy
2+
Compute the kinetic energy of an object given its mass and velocity.
3+
4+
Formula (classical mechanics):
5+
KE = ½ · m · v²
6+
7+
Reference:
8+
https://en.wikipedia.org/wiki/Kinetic_energy
189
"""
1910

2011

2112
def kinetic_energy(mass: float, velocity: float) -> float:
2213
"""
23-
Calculate kinetic energy.
24-
25-
The kinetic energy of a non-rotating object of mass m traveling at a speed v is ½mv²
26-
27-
>>> kinetic_energy(10,10)
14+
Calculate the kinetic energy of a non-rotating object.
15+
16+
Parameters
17+
----------
18+
mass : float
19+
Mass of the object (must be non-negative)
20+
velocity : float
21+
Velocity of the object
22+
23+
Returns
24+
-------
25+
float
26+
Kinetic energy in joules
27+
28+
Raises
29+
------
30+
ValueError
31+
If mass is negative
32+
33+
Examples
34+
--------
35+
>>> kinetic_energy(10, 10)
2836
500.0
29-
>>> kinetic_energy(0,10)
37+
>>> kinetic_energy(0, 10)
3038
0.0
31-
>>> kinetic_energy(10,0)
39+
>>> kinetic_energy(10, 0)
3240
0.0
33-
>>> kinetic_energy(20,-20)
41+
>>> kinetic_energy(20, -20)
3442
4000.0
35-
>>> kinetic_energy(0,0)
43+
>>> kinetic_energy(0, 0)
3644
0.0
37-
>>> kinetic_energy(2,2)
45+
>>> kinetic_energy(2, 2)
3846
4.0
39-
>>> kinetic_energy(100,100)
47+
>>> kinetic_energy(100, 100)
4048
500000.0
4149
"""
4250
if mass < 0:
43-
raise ValueError("The mass of a body cannot be negative")
44-
return 0.5 * mass * abs(velocity) * abs(velocity)
51+
raise ValueError("Mass must be non-negative")
52+
53+
return 0.5 * mass * velocity ** 2
4554

4655

4756
if __name__ == "__main__":

0 commit comments

Comments
 (0)