VPython Lists: Difference between revisions

From Physics Book
Jump to navigation Jump to search
No edit summary
No edit summary
Line 117: Line 117:
</pre>
</pre>


[[File:Disco.gif|thumb|Caption]]
[[File:Disco.gif|thumb|Caption for your GIF]]


===Middling===
===Middling===

Revision as of 17:05, 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 elements were added in
  • 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 as '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 resizes 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 if 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
Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Caption for your GIF

Middling

Difficult

Connectedness

History

See Also

Further Reading

External Links

References