VPython basics: Difference between revisions

From Physics Book
Jump to navigation Jump to search
No edit summary
No edit summary
 
(101 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Claimed by Liubov Nikolenko
This is a beginning overview to vPython with some basic syntax and functions.


==Installation or Online Use==


VPython is a Python graphics module used for modeling objects in 3-dimensional space. In the field of physics, this is especially useful for calculating and modeling complex relationships between objects and their properties.
Python can be rather difficult to install. However, you can use an online version in order to do all code online.  
===Online/ Glowscript===
1. Go to [http://www.glowscript.org/]


2. Log into a Google account.


3. Click here in "You are signed in as [account name you select]  and your programs are here."


4. Click the blue Create New Program.


===Computer Installation===


[[File:EMWave-Maxwell v275.gif]]
To install VPython simply follow the instructions in the links given below. After the installation is completed just open the VIDLE for Python (usually there is a shortcut created on your desktop) and start typing your code.
==Installation==
 
To install VPython simply follow the instructions in the links given below.  


===Windows===
===Windows===
Line 28: Line 32:


The first two lines of your program should be:
The first two lines of your program should be:
<code>
::from __future__ import division
::from __future__ import division
::from visual import *
::from visual import *</code>
The first line enables float division, so 3 / 2 = 1.5 (not 1) and the second line enables graphics module.  
The first line enables float division, so 3 / 2 = 1.5 (not 1) and the second line enables graphics module to plot your 3D simulation.
 
When you reach the point where graphing is needed, you will need this line of code below the ones above:
<code>
::from visual.graph import *
</code>
 
===Variables===
A variable is the name given to a chosen value. Variables can be manipulated, changed, or renamed within vPython.
 
'''Assigning Variables'''
<nowiki>
x = 5 </nowiki>
 
This assigns the variable x the number 5.
'''''Note:''''' The equal sign does not mean equal. Think of it more as an assignment.
 
Python allows allows you to redefine variables
<nowiki>
y = 0
 
x = y </nowiki>
 
In this case, we have taken the original x and assigned it the value from the variable y making the value of x equal to 0.
 
'''Increasing a Variable'''
 
Let's say you want x to increase by one after a certain line of code, to keep variables to a minimum. Python allows you do to so.
<nowiki>
x = 2
 
x = x + 1
 
x += 1</nowiki>
 
 
In this case, we have taken the original value of x and added one to it. If you call x again, its value will now be 3. The third method performs the exact same action as the second method. It just contains different syntax. This will be helpful in loops.
 
===Creating VPython Objects===
===Creating VPython Objects===
*Sphere
In VPython you can create an object with certain attributes (e.g. radius, position). You can store the information about that object in a variable. This way you can access a certain object using a variable it is stored in.
ball = sphere(pos=(x_coordinate,y_coordinate,z_coordinate), radius=radius_of_the_sphere, color = color.color_of_the_sphere)
'''Here are examples of some objects you can create and how to define them'''
*Arrow
*[http://vpython.org/contents/docs/sphere.html Sphere]
myArrow = arrow(pos=(x0_coordinate,y0_coordinate,z0_coordinate), axis=(x1_coordinate,y1_coordinate,z1_coordinate), color = color.color_of_the_arrow)
[[File:Sphere.jpg]]
*Vector
 
myVector = vector(x_coordinate,y_coordinate,z_coordinate)
 
<code> variableName = ball( pos=vector(x_coordinate, y_coordinate, z_coordinate), radius = radius_measure, color = color.blue) </code>
 
''
'''Note:''''' There are different colors you can assign some of which are red, blue, magenta, cyan, yellow, and more.
[[File:Arrow1.jpg]]
 
<code>myArrow = arrow(pos=vector(x_coordinate,y_coordinate,z_coordinate), axis=vector(x1_coordinate,y1_coordinate,z1_coordinate), color = color.color_of_the_arrow)</code>
 
The axis refers to the more so thickness of the arrow. When coding, you can multiply this vector by a scalar value.
 
*[http://vpython.org/contents/docs/vector.html Vector]
<code>myVector = vector(x_coordinate,y_coordinate,z_coordinate)</code>
 
Outside of objects, you can just create a vector numbers to be used later, such as making the force or velocity vector.
*[http://vpython.org/contents/docs/trail.html Trail]
[[File:Trail1.jpg]]
 
 
You can leave a path behind a moving object simply by specifying <code>make_trail=True</code>:
 
<code>ball.make_trail = True</code>
 
Each time you change (object).pos, this new position is added to a curve, thereby leaving a trail behind the moving sphere. This works with arrow, box, cone, cylinder, ellipsoid, pyramid, ring, and sphere.
 
===Manipulating VPython values===
===Manipulating VPython values===
*Accessing attributes of the object
*Accessing attributes of the variable
To access the attribute of a given object just use the syntax ''object.attribute'' (e.g. to access the position of the ball object, you should do ball.pos)
To access the attribute of a given variable just use the syntax <code>object.attribute</code> (e.g. to access the position of the ball variable, you should do <code>ball.pos</code>)
*Updating values
*Updating variable
To update a value (such as time, speed, force or the position of the object) you should do ''value = value + delta_value''
To update a variable (such as time, speed, force or the position of the object) you should do <code>variable = variable + updateIncrement</code>
 
===Running VPython code===
===Running VPython code===
To run VPython code simply press F5. While running your code you will see the visual module and a shell (a place where all print out information is displayed)  
To run VPython code simply press F5. While running your code you will see the visual module and a shell (a place where all print out information is displayed)  
===Loops===
===Loops===
There are two types of loops that can be use in Python: [https://wiki.python.org/moin/ForLoop for loop] and [https://wiki.python.org/moin/WhileLoop while loop]. Basically, loops are used to tell the computer to execute the same task multiple times. While loops are more common for modeling physics concepts.
There are two types of loops that can be used in Python: [https://wiki.python.org/moin/ForLoop for loop] and [https://wiki.python.org/moin/WhileLoop while loop]. Loops repeat the actions within them until a certain pre-determined point is reached.
 
In this course, only while loops are used, so you will not need to worry about for loops.
 
*While loops  
*While loops  
The body of a while loop is executed, while a certain condition is true. Make sure to indent the statements that should be done in the while loop. While loop is equivalent to integration, because it manually breaks down the physical process into small parts and adds them up. While loop has the following structure:
The body of a while loop is executed, while a certain condition is true. '''All the statements within the while loop are indented.''' While loop is equivalent to integration, because it manually breaks down the physical process into small parts and adds them up (see [[Iterative_Prediction | Iterative Prediction]] as a good example of the physical application of a while loop). While loop has the following structure:
:while(condition)
<code>
:while (condition):
::the body of the while loop
::the body of the while loop
::updating the loop
::updating the loop</code>
 
Make sure that the while loop is followed by a colon: (:), or else your program will issue an error.
 
For example:
For example:
:while(t < 60)
<nowiki>
::distance = distance + velocity * deltat
while t < 60:
::t = t + deltat
  distance = distance + velocity * deltat
  t = t + deltat</nowiki>


Note: to see the process clearly it is common to slow down the frame rate by using ''rate(200)'' as a first line of a body of the while loop
'''Note:'''To animate, put '''rate(integerNumber)'''. The higher the rate, the faster the animation. A good number to pick is '''rate(200)'''.
'''Note:''' ''All variables called within the while loop should be created first outside the original loop as it makes it a little easier to troubleshoot at times.''


===Comments===
===Comments===
Comments in the code have no impact on the code execution, because they are just ignored by the computer. Comments in VPython start with a pound sign (#) and end when a line ends. It is a good practice to place some useful information (like an explanation to a certain step) in the comments, so other people will find it easier to understand your code. For example:
Comments in the code have no impact on the code execution, because they are just ignored by the computer. Comments in VPython start with a pound sign (#) and end when a line ends. It is a good practice to place some useful information (like an explanation to a certain step) in the comments, so other people will find it easier to understand your code. For example:
 
<nowiki>
x = x + 1 # incrementing x variable  
x = x + 1 # incrementing x variable</nowiki>
===Debugging techniques===
Unfortunately, sometimes programs do not work in a way we intended them to work. Here are some tips to fixing your code.
#If you get an error try to understand the error message and fix your code accordingly. The most common errors are syntax errors, like parenthesis mismatch or wrong arguments passed into the function.
#Make sure you updating your condition for a while loop, so you don't have an infinite loop.
#Check if your are dividing by zero anywhere.
#Make sure you have correct indentation everywhere.
#Put plenty of ''print()'' statements, so you can know what is going on in every single stage of your code.
#Check your mathematical expressions for the order of precedence (e.g. x / y*z ! = x / (y * z))
#Try commenting out some steps to see which parts of your code do not work.


===Useful built-in functions===
===Useful built-in functions===
====Printing out information====
====Printing out information====
*print(value)
*<code>print(value, variable)</code>
Prints out the given value in the programming shell.  
Prints out the given value in the programming shell. An example is as follows
<nowiki>print("x =", x)</nowiki>
 
The program will print x = and then the value of x from the code you have in the form of: (assuming x equals 5)
x = 5
Or
<nowiki>print("200")</nowiki>
 
This will just print the number 200.
 
 
'''The print function is useful in checking your math while coding!!'''
 
====Math====
====Math====
*x**y
Raises x to the y-th power.
Raises x to the y-th power.
*<code>x ** y</code>
Computes normal addition
*<code>x + y</code>
Computes normal subtraction
*<code>x - y</code>
Computes multiplications
*<code>x * y</code>
Computes divisions
*<code>x / y</code>
Computes the remainder between two values
*<code>x % y</code>
'''''Note: You cannot multiply two vectors!! You can, however, multiply a vector by a scalar.
'''''
===Vectors===
===Vectors===
*cross(vectorA, vectorB)
Calculates the cross product of two vectors
Calculates the cross product of two vectors
*mag(vector)
*<code>cross(vectorA, vectorB)</code>
Calculates the magnitude of the vector
Calculates the magnitude of the vector
*norm(vector)
*<code>mag(vector)</code>
Calculates the unit vector of the vector
Calculates the unit vector of the vector
*<code>norm(vector)</code>
Calculates the dot product of two vectors
*<code>dot(vector_1, vector_2)</code>
==The structure of VPython program==
VPython reads linearly meaning that it reads down the lines as you type. Therefore, the following give you a good idea of how to begin your coding in terms for this class and physics in general.
'''1. Copy the''' from__future import division    /    from visual import*
'''2. Create and declare all variables.''' This includes any constants and any initial conditions. It's important to create your initial conditions before any loops.
'''3. Create objects.''' In VPython, you are creating 3D objects that will most likely move. Characteristics of the objects are most likely going to be updated; therefore, they need to be initialized.
'''4. Initialize a while loop.''' Once you have all your variables, write the condition of your while loop (condition being what you are working towards).
'''5. Begin updating the necessary variables and characteristics of objects.''' This will include things such as velocity, force, momentum, object positions, and etc.
'''6. Print any necessary pieces of data after the while loop or during, if needed.
This program uses a simple algorithm that produces an approximate motion of Earth around the Sun.
<nowiki>
from __future__ import division
from visual import *


==Connectedness==
r=5.0
theta=0.0
dtheta=0.01
sun=sphere(color=color.yellow)
earth=sphere(pos=(r,0,0),color=color.blue)
t = 0
while t < 60:
    rate(20)
    theta=theta+dtheta
    x=r*cos(theta)
    y=r*sin(theta)
    earth.pos=(x,y,0)
    t = t + 0.1</nowiki>
[[File:Earth_Sun.jpg]]


VPython will be used heavily in lab activities in modern sections of both Physics I and Physics II. One of the reasons it is used is because it is very easy to learn, especially when compared to languages like Java and MATLAB, and if provided with a shell, the process of changing values in order to manipulate the program is very straight forward.
https://trinket.io/embed/glowscript/c8bd5fcfea


Another aspect of VPython that is very applicable to this class is the ease at which loops can be created. A loop in a computer program is when a set of commands or lines of code are processed several times until some condition is satisfied. For example, a very simple loop can print out the numbers 1 to 10 by using a counter. Each time the code within the loop is processed, the value of the counter is printed out and the counter increases by one. Once the counter reaches 10, the loop can end and the commands within the loop will no longer be evaluated. This is applicable to many topics in this class, because VPython can loop to update velocity, position, momentum, and many other values in order to evaluate Electric fields, Magnetic fields, Gravitational forces, etc.
Here is a longer, animated example if you follow the link above. It's a Glowscript trinket of a 3D spring.  


The basics learned from VPython can be extended beyond the scope of the class to topics from other class, or maybe even personal projects. For example, if you wanted to analyze the magnetic force between two objects, one moving and one stationary, VPython can be used to compute the magnetic field on the moving object at several different points in it's path of motion. This information can be used to determine what type of magnets to use in that specific application. This application of VPython is applicable to most majors, but especially engineering majors dealing with moving parts, electric currents, or magnetic components.
===Debugging techniques===
Unfortunately, sometimes programs do not work in a way we intended them to work. Here are some tips to fixing your code.
#If you get an error try to see what the error is. The most common errors are with syntax and include forgetting a parentheses, using the wrong punctuation in coding lines,
#Make sure you are updating the variable on which your while loop is based upon. This could cause an infinite while loop if you never approach a terminating condition.
#Double check math. Check for imaginary number or dividing by zero.
#Make sure you have correct indentation everywhere. Remember that the code in the while loop needs to be indented. Python is space-dependent. If an indentation is off, then your whole code will be off.
#Put plenty of <code>print()</code> statements, so you can know what is going on throughout the code. Usually, put these after you are changing the values of variables.
#Check your mathematical expressions for the order of precedence (e.g. <code>x / (y * z)</code> is '''not''' equivalent to <code>x / y * z</code>). Try to fix by adding parenthesis on what needs to be done first.
#It's always a good idea to perhaps do coding in parts and check through the parts as you write the code to ensure not too many errors when approaching the end of coding.  
# Always feel free to ask your TA for help!
For further information on debugging see [[VPython_Common_Errors_and_Troubleshooting | VPython Common Errors and Troubleshooting]].


VPython also has industrial uses, which include modeling the movement of components. However, an advantage over other modeling techniques is that VPython allows for a visual representation of the moving components, as well as dynamic arrows or other symbols that can represent a changing velocity, magnetic field, or various other properties that will change with time or position.  
==Studying for Exams==
Most exam questions will be based on coding similar to labs or coding having to deal with concepts that you've already covered in physics. One good way to prepare is to solve the problems with a set number of variables, then looking back and writing down the variables that change. Finally, create the objects and write the needed characteristics of these objects.  
The exam questions will not be perfectly similar to a lab or any previous question; however, if you practice coding outside of the lab, create other simulations, and understand how the basic syntax of VPython, things should turn out well.  


==Connectedness==


How is this topic connected to something that you are interested in?
How is this topic connected to something that you are interested in?


I like coding and I would love more people to share my passion, so I tried my best to make the coding part of physics more approachable
I enjoy coding and believe coding to be helpful in trying to visualize physics and the mathematics behind it, especially through vPython.
 


How is it connected to your major?
How is it connected to your major?


I am CS major and Python was the first language I have learned at Tech.
I'm a Computer Science major and am currently learning Python in another class. It's very rewarding to see your code be applied in a way that allows you or others to visualize data.
 
   
   
Is there an interesting industrial application?
Is there an interesting industrial application?


Every simulation starting from interaction of molecules can be modeled in VPython to get the general idea of the process.
VPython could be used to simulate different scenarios that can occur virtually.
 
==History of Python==


==History==
VPython was created by a man named Guido van Rossum. He graduated in 1982 with a master's degree in mathematics and computer science from the University of Amsterdam.  
VPython was originally released in 2000 by David Scherer after he took an introductory physics class at Carnegie Mellon University. At the time, the school used the cT programming language for 2D modeling, and David saw the need for something better. Working with several people including professors Ruth Chabay and Bruce Sherwood, he developed a Python module called VisualVisual Python or VPython featured 3D modeling, as well as an easier-to-understand object-oriented syntax.  VPython is released under an Open Source license, and development continues today.
Originally, the program was released in 1989The first open source version was released in 1991 and van Rossum had the following goals in mind: 1. It should be easy and intuitive, 2. It should be open sourced, which means anyone can contribute to its development on the language, 3. It should be easily understood like a language and suitable to be used for multiple purposes.  
He has been recognized by ACM in 2006 as a Distinguished Engineer. Sources: [4] [http://www.computerhistory.org/fellowawards/hall/guido-van-rossum/]


== See also ==
== See also ==
===GlowScript===
===General Overview of VPython===
[http://www.glowscript.org/ GlowScript] is an easy-to-use, powerful environment for creating 3D animations and publishing them on the web. You can write and run GlowScript programs right in your browser, store them in the cloud for free, and easily share them with others. GlowScript uses VPython as a programming language.
[[VPython]]
 
===External links===
===External links===
[http://vpython.org/ The official VPython website with the links to YouTube tutorials]
[http://vpython.org/contents/doc.html Documentation]
[http://vpython.org/contents/doc.html Documentation]


[https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users VPython User forum]
[https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users VPython User forum]
[https://www.computer.org/csdl/mags/co/2015/03/mco2015030008.pdf]
==References==
==References==


This section contains the the references you used while writing this page
This section contains the the references you used while writing this page


1. [http://vpython.org/contents/cTsource/cToverview.html The cT Programming Language]
1. [http://openbookproject.net/thinkcs/python/english3e/index.html Python tutorial]


2. [http://vpython.wikidot.com/ VPython Wiki Site]
2. [http://vpython.wikidot.com/ VPython Wiki Site]
Line 133: Line 283:
3. [http://www.glowscript.org/ GlowScript]
3. [http://www.glowscript.org/ GlowScript]


4. [http://www.visualrelativity.com/vpython/ The image source]
4. Information on van Rossum [http://www.computerhistory.org/fellowawards/hall/guido-van-rossum/]
 
5. More information of python development [https://www.computer.org/csdl/mags/co/2015/03/mco2015030008.pdf]


[[Category:Which Category did you place this in?]]
[[Category:VPython]]

Latest revision as of 01:51, 22 October 2019

This is a beginning overview to vPython with some basic syntax and functions.

Installation or Online Use

Python can be rather difficult to install. However, you can use an online version in order to do all code online.

Online/ Glowscript

1. Go to [1]

2. Log into a Google account.

3. Click here in "You are signed in as [account name you select] and your programs are here."

4. Click the blue Create New Program.

Computer Installation

To install VPython simply follow the instructions in the links given below. After the installation is completed just open the VIDLE for Python (usually there is a shortcut created on your desktop) and start typing your code.

Windows

Install VPython here

OSX

Install VPython here

Linux

Install VPython here

Getting started with Python

Introduction to basic Python use

The first two lines of your program should be:

from __future__ import division
from visual import *

The first line enables float division, so 3 / 2 = 1.5 (not 1) and the second line enables graphics module to plot your 3D simulation.

When you reach the point where graphing is needed, you will need this line of code below the ones above:

from visual.graph import *

Variables

A variable is the name given to a chosen value. Variables can be manipulated, changed, or renamed within vPython.

Assigning Variables

x = 5 

This assigns the variable x the number 5. Note: The equal sign does not mean equal. Think of it more as an assignment.

Python allows allows you to redefine variables

y = 0

x = y 

In this case, we have taken the original x and assigned it the value from the variable y making the value of x equal to 0.

Increasing a Variable

Let's say you want x to increase by one after a certain line of code, to keep variables to a minimum. Python allows you do to so.

x = 2

x = x + 1 

x += 1


In this case, we have taken the original value of x and added one to it. If you call x again, its value will now be 3. The third method performs the exact same action as the second method. It just contains different syntax. This will be helpful in loops.

Creating VPython Objects

In VPython you can create an object with certain attributes (e.g. radius, position). You can store the information about that object in a variable. This way you can access a certain object using a variable it is stored in. Here are examples of some objects you can create and how to define them


variableName = ball( pos=vector(x_coordinate, y_coordinate, z_coordinate), radius = radius_measure, color = color.blue)


Note: There are different colors you can assign some of which are red, blue, magenta, cyan, yellow, and more.

myArrow = arrow(pos=vector(x_coordinate,y_coordinate,z_coordinate), axis=vector(x1_coordinate,y1_coordinate,z1_coordinate), color = color.color_of_the_arrow)

The axis refers to the more so thickness of the arrow. When coding, you can multiply this vector by a scalar value.

myVector = vector(x_coordinate,y_coordinate,z_coordinate)

Outside of objects, you can just create a vector numbers to be used later, such as making the force or velocity vector.


You can leave a path behind a moving object simply by specifying make_trail=True:

ball.make_trail = True

Each time you change (object).pos, this new position is added to a curve, thereby leaving a trail behind the moving sphere. This works with arrow, box, cone, cylinder, ellipsoid, pyramid, ring, and sphere.

Manipulating VPython values

  • Accessing attributes of the variable

To access the attribute of a given variable just use the syntax object.attribute (e.g. to access the position of the ball variable, you should do ball.pos)

  • Updating variable

To update a variable (such as time, speed, force or the position of the object) you should do variable = variable + updateIncrement

Running VPython code

To run VPython code simply press F5. While running your code you will see the visual module and a shell (a place where all print out information is displayed)

Loops

There are two types of loops that can be used in Python: for loop and while loop. Loops repeat the actions within them until a certain pre-determined point is reached.

In this course, only while loops are used, so you will not need to worry about for loops.

  • While loops

The body of a while loop is executed, while a certain condition is true. All the statements within the while loop are indented. While loop is equivalent to integration, because it manually breaks down the physical process into small parts and adds them up (see Iterative Prediction as a good example of the physical application of a while loop). While loop has the following structure:

while (condition):
the body of the while loop
updating the loop

Make sure that the while loop is followed by a colon: (:), or else your program will issue an error.

For example:

while t < 60:
  distance = distance + velocity * deltat
  t = t + deltat

Note:To animate, put rate(integerNumber). The higher the rate, the faster the animation. A good number to pick is rate(200). Note: All variables called within the while loop should be created first outside the original loop as it makes it a little easier to troubleshoot at times.

Comments

Comments in the code have no impact on the code execution, because they are just ignored by the computer. Comments in VPython start with a pound sign (#) and end when a line ends. It is a good practice to place some useful information (like an explanation to a certain step) in the comments, so other people will find it easier to understand your code. For example:

x = x + 1 # incrementing x variable

Useful built-in functions

Printing out information

  • print(value, variable)

Prints out the given value in the programming shell. An example is as follows print("x =", x)

The program will print x = and then the value of x from the code you have in the form of: (assuming x equals 5) x = 5 Or print("200")

This will just print the number 200.


The print function is useful in checking your math while coding!!

Math

Raises x to the y-th power.

  • x ** y

Computes normal addition

  • x + y

Computes normal subtraction

  • x - y

Computes multiplications

  • x * y

Computes divisions

  • x / y

Computes the remainder between two values

  • x % y

Note: You cannot multiply two vectors!! You can, however, multiply a vector by a scalar.


Vectors

Calculates the cross product of two vectors

  • cross(vectorA, vectorB)

Calculates the magnitude of the vector

  • mag(vector)

Calculates the unit vector of the vector

  • norm(vector)

Calculates the dot product of two vectors

  • dot(vector_1, vector_2)


The structure of VPython program

VPython reads linearly meaning that it reads down the lines as you type. Therefore, the following give you a good idea of how to begin your coding in terms for this class and physics in general.

1. Copy the from__future import division / from visual import*

2. Create and declare all variables. This includes any constants and any initial conditions. It's important to create your initial conditions before any loops.

3. Create objects. In VPython, you are creating 3D objects that will most likely move. Characteristics of the objects are most likely going to be updated; therefore, they need to be initialized.

4. Initialize a while loop. Once you have all your variables, write the condition of your while loop (condition being what you are working towards).

5. Begin updating the necessary variables and characteristics of objects. This will include things such as velocity, force, momentum, object positions, and etc.

6. Print any necessary pieces of data after the while loop or during, if needed.

This program uses a simple algorithm that produces an approximate motion of Earth around the Sun.

from __future__ import division
from visual import *

r=5.0
theta=0.0
dtheta=0.01
sun=sphere(color=color.yellow)
earth=sphere(pos=(r,0,0),color=color.blue)
t = 0
while t < 60:
    rate(20)
    theta=theta+dtheta
    x=r*cos(theta)
    y=r*sin(theta)
    earth.pos=(x,y,0)
    t = t + 0.1

https://trinket.io/embed/glowscript/c8bd5fcfea

Here is a longer, animated example if you follow the link above. It's a Glowscript trinket of a 3D spring.

Debugging techniques

Unfortunately, sometimes programs do not work in a way we intended them to work. Here are some tips to fixing your code.

  1. If you get an error try to see what the error is. The most common errors are with syntax and include forgetting a parentheses, using the wrong punctuation in coding lines,
  2. Make sure you are updating the variable on which your while loop is based upon. This could cause an infinite while loop if you never approach a terminating condition.
  3. Double check math. Check for imaginary number or dividing by zero.
  4. Make sure you have correct indentation everywhere. Remember that the code in the while loop needs to be indented. Python is space-dependent. If an indentation is off, then your whole code will be off.
  5. Put plenty of print() statements, so you can know what is going on throughout the code. Usually, put these after you are changing the values of variables.
  6. Check your mathematical expressions for the order of precedence (e.g. x / (y * z) is not equivalent to x / y * z). Try to fix by adding parenthesis on what needs to be done first.
  7. It's always a good idea to perhaps do coding in parts and check through the parts as you write the code to ensure not too many errors when approaching the end of coding.
  8. Always feel free to ask your TA for help!

For further information on debugging see VPython Common Errors and Troubleshooting.

Studying for Exams

Most exam questions will be based on coding similar to labs or coding having to deal with concepts that you've already covered in physics. One good way to prepare is to solve the problems with a set number of variables, then looking back and writing down the variables that change. Finally, create the objects and write the needed characteristics of these objects. The exam questions will not be perfectly similar to a lab or any previous question; however, if you practice coding outside of the lab, create other simulations, and understand how the basic syntax of VPython, things should turn out well.

Connectedness

How is this topic connected to something that you are interested in?

I enjoy coding and believe coding to be helpful in trying to visualize physics and the mathematics behind it, especially through vPython.

How is it connected to your major?

I'm a Computer Science major and am currently learning Python in another class. It's very rewarding to see your code be applied in a way that allows you or others to visualize data.

Is there an interesting industrial application?

VPython could be used to simulate different scenarios that can occur virtually.

History of Python

VPython was created by a man named Guido van Rossum. He graduated in 1982 with a master's degree in mathematics and computer science from the University of Amsterdam. Originally, the program was released in 1989The first open source version was released in 1991 and van Rossum had the following goals in mind: 1. It should be easy and intuitive, 2. It should be open sourced, which means anyone can contribute to its development on the language, 3. It should be easily understood like a language and suitable to be used for multiple purposes. He has been recognized by ACM in 2006 as a Distinguished Engineer. Sources: [4] [2]

See also

General Overview of VPython

VPython

External links

The official VPython website with the links to YouTube tutorials

Documentation

VPython User forum

[3]

References

This section contains the the references you used while writing this page

1. Python tutorial

2. VPython Wiki Site

3. GlowScript

4. Information on van Rossum [4]

5. More information of python development [5]