From 723e7ae00bf3ec8faab41e46cd8bc0f0d060a7bc Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Thu, 31 Aug 2023 10:23:56 +0300 Subject: [PATCH] Use classes instead of functions --- .../exercises/particle_update_position.ipynb | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/notebooks/exercises/particle_update_position.ipynb b/notebooks/exercises/particle_update_position.ipynb index b3d829a..2b9059f 100644 --- a/notebooks/exercises/particle_update_position.ipynb +++ b/notebooks/exercises/particle_update_position.ipynb @@ -17,7 +17,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2018-07-27T15:05:51.531289Z", @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 18, "metadata": { "pycharm": { "name": "#%%\n" @@ -52,34 +52,44 @@ ], "source": [ "class Particle:\n", - " def __init__(self, mass, velocity):\n", + " def __init__(self, mass, velocity, position):\n", " self.mass = mass\n", " self.velocity = velocity\n", + " self.position = position\n", "\n", " def momentum(self):\n", " return self.mass * self.velocity\n", + " \n", + " def update_position(self, dt):\n", + " return self.position + self.velocity * dt\n", "\n", - "particle = Particle(mass=2.1, velocity=0.8)\n", + " def set_position(self, new_position):\n", + " self.position=new_position\n", + " \n", + " def get_position(self):\n", + " return self.position\n", + " \n", + "particle = Particle(mass=2.1, velocity=0.8, position=0.0)\n", "print(particle.momentum())" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "8.28\n" + "8.2\n" ] } ], "source": [ - "position = 8.2\n", - "new_position = update_position(particle.velocity, position, dt=0.1)\n", - "print(new_position)" + "particle.set_position(8.2)\n", + "particle.update_position(dt=0.1)\n", + "print(particle.get_position())" ] }, { @@ -118,7 +128,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.11.4" }, "toc": { "nav_menu": {