<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.physicsbook.gatech.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Krish+katariya</id>
	<title>Physics Book - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://www.physicsbook.gatech.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Krish+katariya"/>
	<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/Special:Contributions/Krish_katariya"/>
	<updated>2026-04-30T22:05:57Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.42.7</generator>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48210</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48210"/>
		<updated>2026-04-27T22:53:01Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: /* How to Write Loops */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;strong&amp;gt;Krish Katariya, Spring 2026 Editing This Page!&amp;lt;/Strong&amp;gt; Please don&#039;t remove until wiki resource is graded! For my contribution, I noticed the page didn&#039;t really explain why we need to use loops with small time steps for physics, so I explained the importance of loops for motion. I also created a simulation using glowscript+trinket, which highlights the importance of using smaller time steps instead of large ones for the motion of objects. I also added a section explaining how to actually write the loops using syntax, a section on common mistakes one would make when writing loops. I noticed that some of the examples given were using python 2 instead of python 3 syntax (or the syntax was just wrong), so I fixed that and generally improved the other sections as well when I saw fit. &lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Mathematical Model: Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===Computational Model: How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Common Mistakes When Writing Loops===&lt;br /&gt;
&lt;br /&gt;
There are a lot of small mistakes that can happen when writing loops, and hare are some of the more common ones.&lt;br /&gt;
&lt;br /&gt;
The most common mistake is forgetting to indent the lines that should be repeated, if you don&#039;t do this, the code will not run. If you don&#039;t indent all parts of the loop, the un-indented parts will only run once. &lt;br /&gt;
&lt;br /&gt;
Another common mistake one might make is forgetting to update the loop variable within the loop itself. For example, in a while loop where we are using time, the variable t must increase inside the loop. If t is never updated, the loop condition will never be satisfied so the loop will run forever. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print(i)&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48209</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48209"/>
		<updated>2026-04-27T22:52:38Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: /* Why Loops Work for Motion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;strong&amp;gt;Krish Katariya, Spring 2026 Editing This Page!&amp;lt;/Strong&amp;gt; Please don&#039;t remove until wiki resource is graded! For my contribution, I noticed the page didn&#039;t really explain why we need to use loops with small time steps for physics, so I explained the importance of loops for motion. I also created a simulation using glowscript+trinket, which highlights the importance of using smaller time steps instead of large ones for the motion of objects. I also added a section explaining how to actually write the loops using syntax, a section on common mistakes one would make when writing loops. I noticed that some of the examples given were using python 2 instead of python 3 syntax (or the syntax was just wrong), so I fixed that and generally improved the other sections as well when I saw fit. &lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Mathematical Model: Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Common Mistakes When Writing Loops===&lt;br /&gt;
&lt;br /&gt;
There are a lot of small mistakes that can happen when writing loops, and hare are some of the more common ones.&lt;br /&gt;
&lt;br /&gt;
The most common mistake is forgetting to indent the lines that should be repeated, if you don&#039;t do this, the code will not run. If you don&#039;t indent all parts of the loop, the un-indented parts will only run once. &lt;br /&gt;
&lt;br /&gt;
Another common mistake one might make is forgetting to update the loop variable within the loop itself. For example, in a while loop where we are using time, the variable t must increase inside the loop. If t is never updated, the loop condition will never be satisfied so the loop will run forever. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print(i)&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48208</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48208"/>
		<updated>2026-04-27T22:51:08Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;strong&amp;gt;Krish Katariya, Spring 2026 Editing This Page!&amp;lt;/Strong&amp;gt; Please don&#039;t remove until wiki resource is graded! For my contribution, I noticed the page didn&#039;t really explain why we need to use loops with small time steps for physics, so I explained the importance of loops for motion. I also created a simulation using glowscript+trinket, which highlights the importance of using smaller time steps instead of large ones for the motion of objects. I also added a section explaining how to actually write the loops using syntax, a section on common mistakes one would make when writing loops. I noticed that some of the examples given were using python 2 instead of python 3 syntax (or the syntax was just wrong), so I fixed that and generally improved the other sections as well when I saw fit. &lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Common Mistakes When Writing Loops===&lt;br /&gt;
&lt;br /&gt;
There are a lot of small mistakes that can happen when writing loops, and hare are some of the more common ones.&lt;br /&gt;
&lt;br /&gt;
The most common mistake is forgetting to indent the lines that should be repeated, if you don&#039;t do this, the code will not run. If you don&#039;t indent all parts of the loop, the un-indented parts will only run once. &lt;br /&gt;
&lt;br /&gt;
Another common mistake one might make is forgetting to update the loop variable within the loop itself. For example, in a while loop where we are using time, the variable t must increase inside the loop. If t is never updated, the loop condition will never be satisfied so the loop will run forever. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print(i)&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48207</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48207"/>
		<updated>2026-04-27T22:50:31Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Krish Katariya, Spring 2026 Editing This Page!* Please don&#039;t remove until wiki resource is graded! For my contribution, I noticed the page didn&#039;t really explain why we need to use loops with small time steps for physics, so I explained the importance of loops for motion. I also created a simulation using glowscript+trinket, which highlights the importance of using smaller time steps instead of large ones for the motion of objects. I also added a section explaining how to actually write the loops using syntax, a section on common mistakes one would make when writing loops. I noticed that some of the examples given were using python 2 instead of python 3 syntax (or the syntax was just wrong), so I fixed that and generally improved the other sections as well when I saw fit. &lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Common Mistakes When Writing Loops===&lt;br /&gt;
&lt;br /&gt;
There are a lot of small mistakes that can happen when writing loops, and hare are some of the more common ones.&lt;br /&gt;
&lt;br /&gt;
The most common mistake is forgetting to indent the lines that should be repeated, if you don&#039;t do this, the code will not run. If you don&#039;t indent all parts of the loop, the un-indented parts will only run once. &lt;br /&gt;
&lt;br /&gt;
Another common mistake one might make is forgetting to update the loop variable within the loop itself. For example, in a while loop where we are using time, the variable t must increase inside the loop. If t is never updated, the loop condition will never be satisfied so the loop will run forever. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print(i)&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48206</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48206"/>
		<updated>2026-04-27T22:50:18Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;**Krish Katariya, Spring 2026 Editing This Page!** Please don&#039;t remove until wiki resource is graded! For my contribution, I noticed the page didn&#039;t really explain why we need to use loops with small time steps for physics, so I explained the importance of loops for motion. I also created a simulation using glowscript+trinket, which highlights the importance of using smaller time steps instead of large ones for the motion of objects. I also added a section explaining how to actually write the loops using syntax, a section on common mistakes one would make when writing loops. I noticed that some of the examples given were using python 2 instead of python 3 syntax (or the syntax was just wrong), so I fixed that and generally improved the other sections as well when I saw fit. &lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Common Mistakes When Writing Loops===&lt;br /&gt;
&lt;br /&gt;
There are a lot of small mistakes that can happen when writing loops, and hare are some of the more common ones.&lt;br /&gt;
&lt;br /&gt;
The most common mistake is forgetting to indent the lines that should be repeated, if you don&#039;t do this, the code will not run. If you don&#039;t indent all parts of the loop, the un-indented parts will only run once. &lt;br /&gt;
&lt;br /&gt;
Another common mistake one might make is forgetting to update the loop variable within the loop itself. For example, in a while loop where we are using time, the variable t must increase inside the loop. If t is never updated, the loop condition will never be satisfied so the loop will run forever. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print(i)&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48205</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48205"/>
		<updated>2026-04-27T22:49:33Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: /* Simple */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove until wiki resource is graded! For my contribution, I noticed the page didn&#039;t really explain why we need to use loops with small time steps for physics, so I explained the importance of loops for motion. I also created a simulation using glowscript+trinket, which highlights the importance of using smaller time steps instead of large ones for the motion of objects. I also added a section explaining how to actually write the loops using syntax, a section on common mistakes one would make when writing loops. I noticed that some of the examples given were using python 2 instead of python 3 syntax (or the syntax was just wrong), so I fixed that and generally improved the other sections as well when I saw fit. &lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Common Mistakes When Writing Loops===&lt;br /&gt;
&lt;br /&gt;
There are a lot of small mistakes that can happen when writing loops, and hare are some of the more common ones.&lt;br /&gt;
&lt;br /&gt;
The most common mistake is forgetting to indent the lines that should be repeated, if you don&#039;t do this, the code will not run. If you don&#039;t indent all parts of the loop, the un-indented parts will only run once. &lt;br /&gt;
&lt;br /&gt;
Another common mistake one might make is forgetting to update the loop variable within the loop itself. For example, in a while loop where we are using time, the variable t must increase inside the loop. If t is never updated, the loop condition will never be satisfied so the loop will run forever. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print(i)&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48204</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48204"/>
		<updated>2026-04-27T22:49:25Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove until wiki resource is graded! For my contribution, I noticed the page didn&#039;t really explain why we need to use loops with small time steps for physics, so I explained the importance of loops for motion. I also created a simulation using glowscript+trinket, which highlights the importance of using smaller time steps instead of large ones for the motion of objects. I also added a section explaining how to actually write the loops using syntax, a section on common mistakes one would make when writing loops. I noticed that some of the examples given were using python 2 instead of python 3 syntax (or the syntax was just wrong), so I fixed that and generally improved the other sections as well when I saw fit. &lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Common Mistakes When Writing Loops===&lt;br /&gt;
&lt;br /&gt;
There are a lot of small mistakes that can happen when writing loops, and hare are some of the more common ones.&lt;br /&gt;
&lt;br /&gt;
The most common mistake is forgetting to indent the lines that should be repeated, if you don&#039;t do this, the code will not run. If you don&#039;t indent all parts of the loop, the un-indented parts will only run once. &lt;br /&gt;
&lt;br /&gt;
Another common mistake one might make is forgetting to update the loop variable within the loop itself. For example, in a while loop where we are using time, the variable t must increase inside the loop. If t is never updated, the loop condition will never be satisfied so the loop will run forever. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48203</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48203"/>
		<updated>2026-04-27T22:47:45Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove until wiki resource is graded! For my contribution, I noticed the page didn&#039;t really explain why we need to use loops with small time steps for physics, so I explained the importance of loops for motion. I also created a simulation using glowscript+trinket, which highlights the importance of using smaller time steps instead of large ones for the motion of objects. I also added a section explaining how to actually write the loops using syntax, and generally improved the other sections as well. &lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Common Mistakes When Writing Loops===&lt;br /&gt;
&lt;br /&gt;
There are a lot of small mistakes that can happen when writing loops, and hare are some of the more common ones.&lt;br /&gt;
&lt;br /&gt;
The most common mistake is forgetting to indent the lines that should be repeated, if you don&#039;t do this, the code will not run. If you don&#039;t indent all parts of the loop, the un-indented parts will only run once. &lt;br /&gt;
&lt;br /&gt;
Another common mistake one might make is forgetting to update the loop variable within the loop itself. For example, in a while loop where we are using time, the variable t must increase inside the loop. If t is never updated, the loop condition will never be satisfied so the loop will run forever. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48202</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48202"/>
		<updated>2026-04-27T22:44:19Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove until wiki resource is graded! For my contribution, I noticed the page didn&#039;t really explain why we need to use loops with small time steps for physics, so I explained the importance of loops for motion. I also created a simulation using glowscript+trinket, which highlights the importance of using smaller time steps instead of large ones for the motion of objects. I also added a section explaining how to actually write the loops using syntax, and generally improved the other sections as well. &lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48201</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48201"/>
		<updated>2026-04-27T22:41:46Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: /* How to Write Loops */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. &lt;br /&gt;
&lt;br /&gt;
We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A for loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48200</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48200"/>
		<updated>2026-04-27T22:41:30Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: /* How to Write Loops */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
A &#039;for&#039; loop repeats over a set sequence, such as a range of numbers. Use the range(start, end) function to determine what points we iterate over. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0, 5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48199</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48199"/>
		<updated>2026-04-27T22:40:44Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: /* How to Write Loops */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. We write while loops by typing &#039;while&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48198</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48198"/>
		<updated>2026-04-27T22:40:34Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: /* How to Write Loops */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it. We write while loops by typing &#039;while&#039;&#039; and entering a mathematical condition after it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48197</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48197"/>
		<updated>2026-04-27T22:37:52Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt; This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48196</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48196"/>
		<updated>2026-04-27T22:37:40Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
&lt;br /&gt;
Loops are generally used in VPython in order to repeat a set of specific instructions without writing the same code many times over and over. The two most common loop structures are the &#039;for&#039; loop and the &#039;while&#039; loop. A &#039;for&#039; loop is usually used when the number of times we need to loop is known, while a &#039;while&#039; loop is used when code is supposed to repeat until a condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt;. This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this link to see the simulation: https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
&lt;br /&gt;
A loop repeats a block of code until a condition is no longer true. In Python/VPython, the loop statement ends with a colon, and the repeated lines must be indented underneath it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t = t + deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the loop continues as long as t is less than 2. Each time the loop runs, t increases by deltat, so the condition eventually becomes false and the loop stops.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48195</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48195"/>
		<updated>2026-04-27T22:33:35Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
The most commonly used loop structures in VPython are the &#039;for&#039; loop and the &#039;while&#039; loop. In Physics modeling, the &#039;for&#039; loop is useful when one wants to check a condition before running the code in the &#039;for&#039; loop. For example, the code following the statement -- &#039;for i in range(0,3):&#039; -- will only run when i is within the specified range. The &#039;while&#039; loop is useful when one wants to run a code for a specified interval or while a condition is true. For example, the code following the statement -- &#039;while t &amp;lt; 100:&#039; -- will run until t is greater than or equal to 100 (1,2,3).&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt;. This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
Click this [link](https://trinket.io/glowscript/030ca409f33d?outputOnly=true) to see the simulation. &lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
For example, to calculate an approximation from t = 0 to t = 2 using two time steps, one could write the following time update:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t += deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, loops can be used to test much smaller time steps than deltat = 1; the smaller the time step, the more accurate the iteration. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48194</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48194"/>
		<updated>2026-04-27T22:22:08Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
The most commonly used loop structures in VPython are the &#039;for&#039; loop and the &#039;while&#039; loop. In Physics modeling, the &#039;for&#039; loop is useful when one wants to check a condition before running the code in the &#039;for&#039; loop. For example, the code following the statement -- &#039;for i in range(0,3):&#039; -- will only run when i is within the specified range. The &#039;while&#039; loop is useful when one wants to run a code for a specified interval or while a condition is true. For example, the code following the statement -- &#039;while t &amp;lt; 100:&#039; -- will run until t is greater than or equal to 100 (1,2,3).&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt;. This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
https://trinket.io/glowscript/030ca409f33d?outputOnly=true&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
For example, to calculate an approximation from t = 0 to t = 2 using two time steps, one could write the following time update:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t += deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, loops can be used to test much smaller time steps than deltat = 1; the smaller the time step, the more accurate the iteration. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48193</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48193"/>
		<updated>2026-04-27T22:21:08Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
The most commonly used loop structures in VPython are the &#039;for&#039; loop and the &#039;while&#039; loop. In Physics modeling, the &#039;for&#039; loop is useful when one wants to check a condition before running the code in the &#039;for&#039; loop. For example, the code following the statement -- &#039;for i in range(0,3):&#039; -- will only run when i is within the specified range. The &#039;while&#039; loop is useful when one wants to run a code for a specified interval or while a condition is true. For example, the code following the statement -- &#039;while t &amp;lt; 100:&#039; -- will run until t is greater than or equal to 100 (1,2,3).&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt;. This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;https://trinket.io/embed/glowscript/030ca409f33d?outputOnly=true&amp;quot; width=&amp;quot;100%&amp;quot; height=&amp;quot;600&amp;quot; frameborder=&amp;quot;0&amp;quot; marginwidth=&amp;quot;0&amp;quot; marginheight=&amp;quot;0&amp;quot; allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
For example, to calculate an approximation from t = 0 to t = 2 using two time steps, one could write the following time update:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t += deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, loops can be used to test much smaller time steps than deltat = 1; the smaller the time step, the more accurate the iteration. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48192</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48192"/>
		<updated>2026-04-27T22:19:28Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
The most commonly used loop structures in VPython are the &#039;for&#039; loop and the &#039;while&#039; loop. In Physics modeling, the &#039;for&#039; loop is useful when one wants to check a condition before running the code in the &#039;for&#039; loop. For example, the code following the statement -- &#039;for i in range(0,3):&#039; -- will only run when i is within the specified range. The &#039;while&#039; loop is useful when one wants to run a code for a specified interval or while a condition is true. For example, the code following the statement -- &#039;while t &amp;lt; 100:&#039; -- will run until t is greater than or equal to 100 (1,2,3).&lt;br /&gt;
&lt;br /&gt;
===Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt;. This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;https://trinket.io/embed/glowscript/030ca409f33d&amp;quot; width=&amp;quot;100%&amp;quot; height=&amp;quot;600&amp;quot; frameborder=&amp;quot;0&amp;quot; marginwidth=&amp;quot;0&amp;quot; marginheight=&amp;quot;0&amp;quot; allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===How to Write Loops===&lt;br /&gt;
For example, to calculate an approximation from t = 0 to t = 2 using two time steps, one could write the following time update:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t += deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, loops can be used to test much smaller time steps than deltat = 1; the smaller the time step, the more accurate the iteration. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48191</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48191"/>
		<updated>2026-04-27T22:18:13Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
The most commonly used loop structures in VPython are the &#039;for&#039; loop and the &#039;while&#039; loop. In Physics modeling, the &#039;for&#039; loop is useful when one wants to check a condition before running the code in the &#039;for&#039; loop. For example, the code following the statement -- &#039;for i in range(0,3):&#039; -- will only run when i is within the specified range. The &#039;while&#039; loop is useful when one wants to run a code for a specified interval or while a condition is true. For example, the code following the statement -- &#039;while t &amp;lt; 100:&#039; -- will run until t is greater than or equal to 100 (1,2,3).&lt;br /&gt;
&lt;br /&gt;
===Physics Explanation: Why Loops Work for Motion===&lt;br /&gt;
&lt;br /&gt;
In physics, motion is usually described using continuous equations, such as Newton’s Second Law, which will relate force and momentum with: &amp;lt;pre&amp;gt; dP/dt = Fnet &amp;lt;/pre&amp;gt;. This equation tells us how momentum changes continuously over time. &lt;br /&gt;
&lt;br /&gt;
However, in computational modeling, we cannot directly calculate continuous changes easily. To combat this, we instead approximate the process using very tiny time steps (Δt) and updating the values at each step. While this isn&#039;t continuous, if our time steps are small enough, we can approximate the process closely enough. &lt;br /&gt;
&lt;br /&gt;
Using the momentum principle in discrete form, we write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This equation is saying that, during each small time interval (deltat), the momentum of an object changes based on the net force acting on it. After updating the momentum, we then can update position using the kinematics formulas that we are familiar with. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
position = position + velocity * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now imagine we want to have many tiny steps over a long time period, writing these equations over and over in VPython would take way too long, which is why glowscript has the concept of loops. A loop allows us to repeatedly run the same lines of code many times, meaning we can  apply these updates many times. Thus, using loops, we are effectively simulating continuous motion using small time steps.&lt;br /&gt;
&lt;br /&gt;
The smaller the value of deltat, the closer this approximation is to real physical behavior. Large time steps can lead to inaccurate results, while very small time steps produce more realistic simulations.&lt;br /&gt;
&lt;br /&gt;
===Interactive Model: Why Time Step Size Matters===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;https://trinket.io/embed/glowscript/030ca409f33d&amp;quot; width=&amp;quot;100%&amp;quot; height=&amp;quot;600&amp;quot; frameborder=&amp;quot;0&amp;quot; marginwidth=&amp;quot;0&amp;quot; marginheight=&amp;quot;0&amp;quot; allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The embedded GlowScript model below compares falling objects simulated with different values of deltat. A larger deltat updates the motion in bigger jumps, which makes the approximation less accurate.&lt;br /&gt;
&lt;br /&gt;
A smaller deltat updates the motion more frequently, so the simulated motion stays closer to the exact solution. This shows why loops are useful in VPython: they approximate continuous motion by repeatedly updating momentum and position over small time intervals. This is also why we want to use relatively small values for delta t!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===A Mathematical Model===&lt;br /&gt;
When computing iterations for physics problems, using the iterative method with small delta t increments can produce near accurate results. To have truly small delta t increments, however, computational modeling is necessary for computation. &lt;br /&gt;
&lt;br /&gt;
In a problem that requires use of the momentum principle and a specific number of time steps for iteration, we update momentum for each time step using the following equation:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this equation, the final momentum is updated after each time step (deltat) up to a time (t). We can only manually do this with a small number of increments, however, and as a result, with less accuracy than if our delta t increments were smaller. This is where computational modeling and VPython loops come in. &lt;br /&gt;
&lt;br /&gt;
===A Computational Model===&lt;br /&gt;
With computational modeling using VPython, we can reduce the size of delta t and increase the number of time steps in the approximation of an iteration. Instead of two or three time steps, VPython loops make it possible to test infinitely small time steps, making the final result more accurate. For example, to calculate an approximation from t = 0 to t = 2 using two time steps, one could write the following time update:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t += deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, loops can be used to test much smaller time steps than deltat = 1; the smaller the time step, the more accurate the iteration. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48190</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48190"/>
		<updated>2026-04-27T22:07:58Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
The most commonly used loop structures in VPython are the &#039;for&#039; loop and the &#039;while&#039; loop. In Physics modeling, the &#039;for&#039; loop is useful when one wants to check a condition before running the code in the &#039;for&#039; loop. For example, the code following the statement -- &#039;for i in range(0,3):&#039; -- will only run when i is within the specified range. The &#039;while&#039; loop is useful when one wants to run a code for a specified interval or while a condition is true. For example, the code following the statement -- &#039;while t &amp;lt; 100:&#039; -- will run until t is greater than or equal to 100 (1,2,3).&lt;br /&gt;
&lt;br /&gt;
===A Mathematical Model===&lt;br /&gt;
When computing iterations for physics problems, using the iterative method with small delta t increments can produce near accurate results. To have truly small delta t increments, however, computational modeling is necessary for computation. &lt;br /&gt;
&lt;br /&gt;
In a problem that requires use of the momentum principle and a specific number of time steps for iteration, we update momentum for each time step using the following equation:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this equation, the final momentum is updated after each time step (deltat) up to a time (t). We can only manually do this with a small number of increments, however, and as a result, with less accuracy than if our delta t increments were smaller. This is where computational modeling and VPython loops come in. &lt;br /&gt;
&lt;br /&gt;
===A Computational Model===&lt;br /&gt;
With computational modeling using VPython, we can reduce the size of delta t and increase the number of time steps in the approximation of an iteration. Instead of two or three time steps, VPython loops make it possible to test infinitely small time steps, making the final result more accurate. For example, to calculate an approximation from t = 0 to t = 2 using two time steps, one could write the following time update:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t += deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, loops can be used to test much smaller time steps than deltat = 1; the smaller the time step, the more accurate the iteration. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48189</id>
		<title>VPython Loops</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Loops&amp;diff=48189"/>
		<updated>2026-04-27T22:07:51Z</updated>

		<summary type="html">&lt;p&gt;Krish katariya: Claiming this page! Will modify with edits soon.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Krish Katariya, Spring 2026 Editing This Page! Please don&#039;t remove.&lt;br /&gt;
An introduction to creating and using loops in VPython.&lt;br /&gt;
&lt;br /&gt;
==The Main Idea==&lt;br /&gt;
The most commonly used loop structures in VPython are the &#039;for&#039; loop and the &#039;while&#039; loop. In Physics modeling, the &#039;for&#039; loop is useful when one wants to check a condition before running the code in the &#039;for&#039; loop. For example, the code following the statement -- &#039;for i in range(0,3):&#039; -- will only run when i is within the specified range. The &#039;while&#039; loop is useful when one wants to run a code for a specified interval or while a condition is true. For example, the code following the statement -- &#039;while t &amp;lt; 100:&#039; -- will run until t is greater than or equal to 100 (1,2,3).&lt;br /&gt;
&lt;br /&gt;
===A Mathematical Model===&lt;br /&gt;
When computing iterations for physics problems, using the iterative method with small delta t increments can produce near accurate results. To have truly small delta t increments, however, computational modeling is necessary for computation. &lt;br /&gt;
&lt;br /&gt;
In a problem that requires use of the momentum principle and a specific number of time steps for iteration, we update momentum for each time step using the following equation:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
delta P = Fnet * deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this equation, the final momentum is updated after each time step (deltat) up to a time (t). We can only manually do this with a small number of increments, however, and as a result, with less accuracy than if our delta t increments were smaller. This is where computational modeling and VPython loops come in. &lt;br /&gt;
&lt;br /&gt;
===A Computational Model===&lt;br /&gt;
With computational modeling using VPython, we can reduce the size of delta t and increase the number of time steps in the approximation of an iteration. Instead of two or three time steps, VPython loops make it possible to test infinitely small time steps, making the final result more accurate. For example, to calculate an approximation from t = 0 to t = 2 using two time steps, one could write the following time update:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
deltat = 1&lt;br /&gt;
t = 0&lt;br /&gt;
while t &amp;lt; 2:&lt;br /&gt;
    t += deltat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, loops can be used to test much smaller time steps than deltat = 1; the smaller the time step, the more accurate the iteration. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following examples cover a range of loops that can be created in VPython from the simplest &#039;for&#039; loops to more complicated &#039;for&#039; and &#039;while&#039; loops.&lt;br /&gt;
&lt;br /&gt;
===Simple===&lt;br /&gt;
The simplest example is a basic &#039;for&#039; loop. The following code will print each integer in a range:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in range(0,10):&lt;br /&gt;
    print i&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same thing can be accomplished with a &#039;while&#039; loop as well. See the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
while i &amp;lt; 10:&lt;br /&gt;
    print i&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When modeling momentum updates, using a &#039;while&#039; loop allows the code to run until Tfinal has been reached by adding deltat to t each time the loop runs.&lt;br /&gt;
&lt;br /&gt;
===Middling===&lt;br /&gt;
To solve more complex problems, we need to create values and objects before the loop that will then be updated within the loop until a certain time, t. In the following example, the final position and final velocity of object ball is updated until t = 10 using a time step of deltat = 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:&lt;br /&gt;
    Fgrav = vector(0,-ball.m*g,0)&lt;br /&gt;
    Fdrag=(.5)*dragCoeff*airDensity*areaBall*mag(ball.p/ball.m)**2*norm(ball.p)&lt;br /&gt;
    Fnet = Fgrav - Fdrag&lt;br /&gt;
&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/ball.m)*deltat  &lt;br /&gt;
&lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Difficult===&lt;br /&gt;
The following code calculates the final position and velocity of a ball attached to a string mounted to a ceiling. After code is written listing the constants, creating the objects, and setting an initial value of t = 0, the following statements update the position and velocity values until t = 10 seconds.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
t = 0&lt;br /&gt;
deltat = 1&lt;br /&gt;
&lt;br /&gt;
while t &amp;lt; 10:   &lt;br /&gt;
    L = ball.pos - ceiling.pos&lt;br /&gt;
    s=mag(L) - L0&lt;br /&gt;
    Lhat = L/mag(L)&lt;br /&gt;
    Fs = -(ks)*s*Lhat&lt;br /&gt;
    Fg = vector(0,-g*mball,0)&lt;br /&gt;
    Fdrag = (-1)*b*(ball.p/mball)&lt;br /&gt;
    Fnet = Fg + Fs + Fdrag&lt;br /&gt;
    ball.p = ball.p + Fnet*deltat&lt;br /&gt;
    ball.pos = ball.pos + (ball.p/(mball))*deltat&lt;br /&gt;
    spring.axis = ball.pos - ceiling.pos &lt;br /&gt;
    &lt;br /&gt;
    t += deltat&lt;br /&gt;
&lt;br /&gt;
print(ball.pos)    #prints final ball position&lt;br /&gt;
print(ball.p/mball)    #prints final ball velocity&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Understanding the basics of VPython creates a framework for more easily learning to write in coding languages other than Python. Additionally, understanding the basics of the &#039;for&#039; loop and the &#039;while&#039; loop enables one to write more complex code using both &#039;for&#039; and &#039;while&#039; loops, even nesting both types of loops in creating complex conditionals. In more advanced Physics modeling, being able to write more complex conditional statements enables these more complex equations and relationships to be solved via computational modeling. &lt;br /&gt;
&lt;br /&gt;
Even for non-computing majors, coding experience is a highly valuable trait employers are increasingly looking for in candidates. In 2016, analytics firm Burning Glass reported that programming jobs were growing 12% faster than the market average. Additionally, half of the projected job openings looking for programming experience are in non-technology fields such as &#039;finance, manufacturing, and healthcare&#039; (4). In 2017, Forbes ranked Python as the top-ranked in-demand coding language among the top five: &#039;Python, Java, JavaScript, C#, and PHP&#039; (5). &lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Python is an interpreted language that originated in the 1980s and was released from development in the 1990s. Because it is interpreted, compiling is not required to convert lines of code into machine-understandable instructions (6). In 1998, David Scherer saw a need for a better 2D and 3D graphics programming environment and created the idea for Visual (a.k.a. VPython), a Python module (7). &lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
===Further Reading===&lt;br /&gt;
&#039;Why Coding Is Still The Most Important Job Skill Of The Future&#039; (Dishman, 2016)&lt;br /&gt;
&#039;The Five Most In-Demand Coding Languages&#039; (Kauflin, 2017)&lt;br /&gt;
&lt;br /&gt;
===External Links===&lt;br /&gt;
http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
http://vpython.org/contents/docs/&lt;br /&gt;
https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
https://en.wikipedia.org/wiki/VPython#History&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
1. http://vpython.org/contents/docs/VisualIntro.html&lt;br /&gt;
2. http://vpython.org/contents/docs/&lt;br /&gt;
3. https://faculty.math.illinois.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/visual/docs/visual/VisualIntro.html&lt;br /&gt;
4. https://www.fastcompany.com/3060883/why-coding-is-the-job-skill-of-the-future-for-everyone&lt;br /&gt;
5. https://www.forbes.com/sites/jeffkauflin/2017/05/12/the-five-most-in-demand-coding-languages/#6b86011fb3f5&lt;br /&gt;
6. https://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
7. https://en.wikipedia.org/wiki/VPython#History&lt;/div&gt;</summary>
		<author><name>Krish katariya</name></author>
	</entry>
</feed>