VPython Lists: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
Yu Zhou Fall 2025 | Yu Zhou Fall 2025 | ||
An introduction to creating and using | An introduction to creating and using lists in VPython. | ||
==The Main Idea== | ==The Main Idea== | ||
Let's say you want to create 100 or maybe even ''12,000'' tennis balls using VPython. Now, you could perhaps do it like this: | |||
<pre> | |||
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) | |||
</pre> | |||
and so on and so on. However, this can get really disorganized. Fortunately, we can use lists to contain all of these balls! | |||
<pre> | |||
ball_list = [] | |||
num_balls = 1000 | |||
for i in range(num_balls): | |||
pos = vector(0, 0, i) | |||
radius = 1 | |||
ball = sphere(pos=pos, radius=radius, color=color.red) | |||
ball_list.append(ball) | |||
</pre> | |||
Now that's a lot of balls. | |||
===A Mathematical Model=== | ===A Mathematical Model=== | ||
Revision as of 02:16, 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 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. Fortunately, we can use lists to contain all of these balls!
ball_list = []
num_balls = 1000
for i in range(num_balls):
pos = vector(0, 0, i)
radius = 1
ball = sphere(pos=pos, radius=radius, color=color.red)
ball_list.append(ball)
Now that's a lot of balls.