VPython Lists

From Physics Book
Revision as of 11:16, 2 December 2025 by Yzh79850 (talk | contribs)
Jump to navigation Jump to search

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.

A Mathematical Model

A Computational Model

Examples

Simple

Middling

Difficult

Connectedness

History

See Also

Further Reading

External Links

References