add position as class attribute

This commit is contained in:
ASPP Student 2025-09-25 10:42:16 +03:00
parent 35d41e1147
commit f333013abf

View file

@ -52,17 +52,18 @@
],
"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, position, dt):\n",
" return position + self.velocity * dt\n",
" def update_position(self, dt):\n",
" return self.position + self.velocity * dt\n",
"\n",
"particle = Particle(mass=2.1, velocity=0.8)\n",
"particle = Particle(mass=2.1, velocity=0.8, position = 8.2)\n",
"print(particle.momentum())"
]
},
@ -80,7 +81,7 @@
}
],
"source": [
"position = 8.2\n",
"\n",
"new_position = particle.update_position(position, dt=0.1)\n",
"print(new_position)"
]