VPython Lists: Difference between revisions
No edit summary |
No edit summary |
||
| (20 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Yu Zhou Fall 2025 | '''Yu Zhou Fall 2025''' | ||
<p> | |||
An introduction to creating and using lists in VPython. | An introduction to creating and using lists in VPython. | ||
</p> | |||
==The Main Idea== | ==The Main Idea== | ||
| Line 16: | Line 18: | ||
<pre> | <pre> | ||
# This is where we create our list | |||
ball_list = [] | ball_list = [] | ||
# We set how many balls to add to our list | |||
num_balls = 1000 | num_balls = 1000 | ||
# Inside this loop, we add num_balls amount of balls to the list | |||
for i in range(num_balls): | for i in range(num_balls): | ||
# Every time we add a ball, we put it next to the previous ball | |||
pos = vector(0, 0, i) | pos = vector(0, 0, i) | ||
# radius is 1 for the ball | |||
radius = 1 | radius = 1 | ||
# We create the ball | |||
ball = sphere(pos=pos, radius=radius, color=color.red) | ball = sphere(pos=pos, radius=radius, color=color.red) | ||
# Finally, we add it to the list | |||
ball_list.append(ball) | ball_list.append(ball) | ||
</pre> | </pre> | ||
| Line 30: | Line 42: | ||
Now that's a lot of balls. | Now that's a lot of balls. | ||
<p></p> | |||
VPython lists are pretty flexible, unlike lists from other languages, such as: | |||
*containing duplicate items | |||
*easily modifying, replacing, or removing elements | |||
*keeps track of the order in which the elements were added | |||
*elements accessed through their position | |||
*containing different data types (integers, strings, booleans, etc.) | |||
===A Computational Model=== | ===A Computational Model=== | ||
In creating the basic list, there are two components: the name and the contents. If we look at the first part of this line of code, we can see that we have named the list 'ball_list'. | |||
<pre> | |||
ball_list = []; | |||
</pre> | |||
A list can have any kind of name. It could be called 'x', 'alotofballs','billnyethescienceguy', or anything you want. However, the best names are intuitive and help both you and other people understand your code easily. | |||
<p></p> | |||
The second part of creating a list is the contents. The contents of a list can either be empty or have something already there. | |||
<pre> | |||
ball1 = sphere(color=color.blue, radius=0.1) | |||
ball2 = sphere(color=color.red, radius=0.2) | |||
ball3 = sphere(color=color.yellow, radius=0.3) | |||
ball_list = [ball1, ball2, ball3] | |||
box_list = [] | |||
duck_list = [sphere(color=color.white, radius=0.2)] | |||
</pre> | |||
These are just some examples! | |||
<p></p> | |||
Lists are dynamic arrays that automatically resize when needed. When an object is stored in a list, it is stored as a reference, so changing an item in the list will update itself in the scene instantly. The access cost is also O(1), making updates and finding objects fast. For searching, the time complexity is O(n). It takes a while, but it gets the job done. | |||
==Examples== | ==Examples== | ||
| Line 40: | Line 83: | ||
===Simple=== | ===Simple=== | ||
Let's say we want to create 5 disco balls and make them change colors repeatedly. We first create a list and set how many balls we want. Afterwards, we add all of the balls into the scene and the list. Finally, we create a loop that changes the color of each ball depending on whether the variable 't' is even or odd. | |||
<pre> | |||
ball_list = [] | |||
num_balls = 5 | |||
=== | for i in range(num_balls): | ||
pos = vector(i*2-3, 0, 0) | |||
radius = 1 | |||
ball = sphere(pos=pos, radius=radius, color=color.white) | |||
ball_list.append(ball) | |||
t = 0 | |||
dt = 1 | |||
while True: | |||
rate(100) | |||
t += dt | |||
if t % 2 == 0 : | |||
ball_list[0].color = color.blue | |||
ball_list[1].color = color.red | |||
ball_list[2].color = color.green | |||
ball_list[3].color = color.yellow | |||
ball_list[4].color = color.orange | |||
else : | |||
ball_list[0].color = color.red | |||
ball_list[1].color = color.green | |||
ball_list[2].color = color.blue | |||
ball_list[3].color = color.purple | |||
ball_list[4].color = color.yellow | |||
</pre> | |||
[[File:Disco.gif|thumb|5 disco balls rapidly changing colors (click to see the gif if you can't)]] | |||
===Difficult=== | ===Difficult=== | ||
For more of a challenge, let's create a circle of balls and make them rotate! First, we would need to create a few variables just to define how many balls and how big the circle is. For this example, we use two lists. One for the individual balls, and the other for their corresponding position/angle in the circle. We can see now how important it is that we can access each ball and its position quickly using a list. Finally, we create a while loop to continuously update the balls' position so that they appear to be rotating in a circle! | |||
<p></p> | |||
For a bonus challenge, what would we do to make the balls turn in the opposite direction? | |||
[[File:BallSpinning.gif|thumb|various cyan balls spinning counter clockwise in a circle (click to see the gif if you can't)]] | |||
<pre> | |||
num_balls = 50 | |||
radius = 5.0 | |||
omega = 0.5 | |||
balls = [] | |||
angles = [] | |||
for k in range(num_balls): | |||
theta = 2*3.14 * k / num_balls | |||
pos = vector(radius * cos(theta), radius * sin(theta), 0) | |||
b = sphere(pos=pos, radius=0.15, color=color.cyan) | |||
balls.append(b) | |||
angles.append(theta) | |||
dt = 0.01 | |||
while True: | |||
rate(100) | |||
for k in range(num_balls): | |||
angles[k] += omega * dt | |||
theta = angles[k] | |||
balls[k].pos = vector(radius * cos(theta), radius * sin(theta), 0) | |||
</pre> | |||
==Connectedness== | ==Connectedness== | ||
Lists are incredibly important, and they interact naturally with other data structures in VPython. Combinations like lists, vector math, 3D rendering, loops, and randomness make VPython an incredibly versatile and ideal teaching model for students. Not only will they be able to review physics concepts, but students will also begin to think computationally and develop more programming skills. In the state of the world, even understanding a bit of programming is vital. | |||
==History== | ==History== | ||
The word ''list'' comes from the Middle Ages, where it was used to refer to the bordered grounds of a jousting field. This early meaning, being derived from Germanic roots of the word ''band'' or ''border'', was meant to emphasize order and structure. Over the following centuries, the term began to expand. Medieval administrations kept names on "rolls", merchants in the 1600s "listed" goods for sale, and political revolutions formalized "ordered" records through roll calls. By the 19th century, mail-order catalogs and growing commercial record-keeping practices helped establish the list as a standard tool for organizing information. | |||
<p></p> | |||
Later, the computational list began to emerge in the 20th century due to the introduction of data-processing machines that could arrange information sequentially on punch cards. This idea reached a major milestone in 1958 with the creation of LISP, a programming language built entirely around list processing. As computer science progressed, the term became essential to data structures for storing ordered collections of values. VPython integrates lists perfectly within its own language, creating something for you and me to learn and experiment with. | |||
==See Also== | |||
[http://www.physicsbook.gatech.edu/VPython_Animation VPython Animation] | |||
[http://www.physicsbook.gatech.edu/VPython_Loops VPython Loops] | |||
===External Links=== | ===External Links=== | ||
https://medium.com/circa-navigate/the-long-listed-history-of-the-list-4c4da36a4ed5 | |||
https://www.glowscript.org/#/user/yzh79850/folder/MyPrograms/program/WikiTest | |||
==References== | ==References== | ||
https://www.youtube.com/watch?v=4RVZ9UvLoB0 | |||
https://www.youtube.com/watch?v=upMOE4XhpJk | |||
https://www.geeksforgeeks.org/python/python-lists/ | |||
https://medium.com/circa-navigate/the-long-listed-history-of-the-list-4c4da36a4ed5 | |||
https://www.w3schools.com/python/python_lists.asp | |||
Latest revision as of 18:26, 2 December 2025
Yu Zhou Fall 2025
An introduction to creating and using lists in VPython.
The Main Idea
Let's say you want to create 100 or maybe even 12,000 tennis balls to help you stop a 17, 500 kg block of mass sliding right at you using VPython. Now, you could perhaps do it like this:
ball1 = sphere(color=color.blue, radius=0.2) ball2 = sphere(color=color.blue, radius=0.2) ball3 = sphere(color=color.blue, radius=0.2) ball4 = sphere(color=color.blue, radius=0.2) ball5 = sphere(color=color.blue, radius=0.2)
and so on and so on. However, this can get really disorganized, and you probably don't want to count all the way to 'ball12000'. Fortunately, we can use lists to contain all of these balls!
# This is where we create our list
ball_list = []
# We set how many balls to add to our list
num_balls = 1000
# Inside this loop, we add num_balls amount of balls to the list
for i in range(num_balls):
# Every time we add a ball, we put it next to the previous ball
pos = vector(0, 0, i)
# radius is 1 for the ball
radius = 1
# We create the ball
ball = sphere(pos=pos, radius=radius, color=color.red)
# Finally, we add it to the list
ball_list.append(ball)
Now that's a lot of balls.
VPython lists are pretty flexible, unlike lists from other languages, such as:
- containing duplicate items
- easily modifying, replacing, or removing elements
- keeps track of the order in which the elements were added
- elements accessed through their position
- containing different data types (integers, strings, booleans, etc.)
A Computational Model
In creating the basic list, there are two components: the name and the contents. If we look at the first part of this line of code, we can see that we have named the list 'ball_list'.
ball_list = [];
A list can have any kind of name. It could be called 'x', 'alotofballs','billnyethescienceguy', or anything you want. However, the best names are intuitive and help both you and other people understand your code easily.
The second part of creating a list is the contents. The contents of a list can either be empty or have something already there.
ball1 = sphere(color=color.blue, radius=0.1) ball2 = sphere(color=color.red, radius=0.2) ball3 = sphere(color=color.yellow, radius=0.3) ball_list = [ball1, ball2, ball3] box_list = [] duck_list = [sphere(color=color.white, radius=0.2)]
These are just some examples!
Lists are dynamic arrays that automatically resize when needed. When an object is stored in a list, it is stored as a reference, so changing an item in the list will update itself in the scene instantly. The access cost is also O(1), making updates and finding objects fast. For searching, the time complexity is O(n). It takes a while, but it gets the job done.
Examples
Simple
Let's say we want to create 5 disco balls and make them change colors repeatedly. We first create a list and set how many balls we want. Afterwards, we add all of the balls into the scene and the list. Finally, we create a loop that changes the color of each ball depending on whether the variable 't' is even or odd.
ball_list = []
num_balls = 5
for i in range(num_balls):
pos = vector(i*2-3, 0, 0)
radius = 1
ball = sphere(pos=pos, radius=radius, color=color.white)
ball_list.append(ball)
t = 0
dt = 1
while True:
rate(100)
t += dt
if t % 2 == 0 :
ball_list[0].color = color.blue
ball_list[1].color = color.red
ball_list[2].color = color.green
ball_list[3].color = color.yellow
ball_list[4].color = color.orange
else :
ball_list[0].color = color.red
ball_list[1].color = color.green
ball_list[2].color = color.blue
ball_list[3].color = color.purple
ball_list[4].color = color.yellow
Difficult
For more of a challenge, let's create a circle of balls and make them rotate! First, we would need to create a few variables just to define how many balls and how big the circle is. For this example, we use two lists. One for the individual balls, and the other for their corresponding position/angle in the circle. We can see now how important it is that we can access each ball and its position quickly using a list. Finally, we create a while loop to continuously update the balls' position so that they appear to be rotating in a circle!
For a bonus challenge, what would we do to make the balls turn in the opposite direction?
num_balls = 50
radius = 5.0
omega = 0.5
balls = []
angles = []
for k in range(num_balls):
theta = 2*3.14 * k / num_balls
pos = vector(radius * cos(theta), radius * sin(theta), 0)
b = sphere(pos=pos, radius=0.15, color=color.cyan)
balls.append(b)
angles.append(theta)
dt = 0.01
while True:
rate(100)
for k in range(num_balls):
angles[k] += omega * dt
theta = angles[k]
balls[k].pos = vector(radius * cos(theta), radius * sin(theta), 0)
Connectedness
Lists are incredibly important, and they interact naturally with other data structures in VPython. Combinations like lists, vector math, 3D rendering, loops, and randomness make VPython an incredibly versatile and ideal teaching model for students. Not only will they be able to review physics concepts, but students will also begin to think computationally and develop more programming skills. In the state of the world, even understanding a bit of programming is vital.
History
The word list comes from the Middle Ages, where it was used to refer to the bordered grounds of a jousting field. This early meaning, being derived from Germanic roots of the word band or border, was meant to emphasize order and structure. Over the following centuries, the term began to expand. Medieval administrations kept names on "rolls", merchants in the 1600s "listed" goods for sale, and political revolutions formalized "ordered" records through roll calls. By the 19th century, mail-order catalogs and growing commercial record-keeping practices helped establish the list as a standard tool for organizing information.
Later, the computational list began to emerge in the 20th century due to the introduction of data-processing machines that could arrange information sequentially on punch cards. This idea reached a major milestone in 1958 with the creation of LISP, a programming language built entirely around list processing. As computer science progressed, the term became essential to data structures for storing ordered collections of values. VPython integrates lists perfectly within its own language, creating something for you and me to learn and experiment with.
See Also
VPython Animation VPython Loops
External Links
https://medium.com/circa-navigate/the-long-listed-history-of-the-list-4c4da36a4ed5 https://www.glowscript.org/#/user/yzh79850/folder/MyPrograms/program/WikiTest
References
https://www.youtube.com/watch?v=4RVZ9UvLoB0 https://www.youtube.com/watch?v=upMOE4XhpJk https://www.geeksforgeeks.org/python/python-lists/ https://medium.com/circa-navigate/the-long-listed-history-of-the-list-4c4da36a4ed5 https://www.w3schools.com/python/python_lists.asp