Python Syntax: Difference between revisions
(Added a basic Python tutorial, cleaned up the page some. Will be back to clean up some more and improve upon the VPython tutorial itself.) |
No edit summary |
||
Line 1: | Line 1: | ||
Edited by Zach Sanchez - Fall 2017 (in progress) | Edited by Zach Sanchez - Fall 2017 (in progress) | ||
==The Main Idea== | ==The Main Idea== | ||
VPython is an extension of the Python programming language that contains a 3D graphics module called Visual, which allows users to create simple simulations of physics problems. Its primary use is for educational purposes, although it has been used in research before in the past. We will use VPython on occasion to aid us in visualizing and experimenting with the ideas that we learn in this course. | VPython is an extension of the Python programming language that contains a 3D graphics module called Visual, which allows users to create simple simulations of physics problems. Its primary use is for educational purposes, although it has been used in research before in the past. We will use VPython on occasion to aid us in visualizing and experimenting with the ideas that we learn in this course. | ||
==Downloading vPython== | ==Downloading vPython== | ||
Line 36: | Line 35: | ||
# That is, until you need to do an exponent. But you shouldn't encounter anything much weirder than that math-wise. | # That is, until you need to do an exponent. But you shouldn't encounter anything much weirder than that math-wise. | ||
2 ** 3 # -> 8 | 2 ** 3 # -> 8 | ||
# There's also various math functions you can use, such as for finding the square root. | |||
sqrt(4) # -> 2 | |||
# However, these math operations are useless if we don't store the values anywhere. | # However, these math operations are useless if we don't store the values anywhere. | ||
Line 50: | Line 52: | ||
y = 3 | y = 3 | ||
# The '==' means that you're directly comparing two values. If they're equal, whatever is on the inside of the if-statement will be executed. | # The '==' means that you're directly comparing two values. If they're equal, whatever is on the inside of the if-statement will be executed. | ||
# Now is a good time to note that whitespace is very important in Python. 'y = 3' will always execute if it isn't tabbed, so even if x is equal to 7, y will still become 3. | # Now is a good time to note that whitespace is very important in Python. | ||
# 'y = 3' will always execute if it isn't tabbed, so even if x is equal to 7, y will still become 3. | |||
# If you ever see me use whitespace in this tutorial, make sure you do as well. | # If you ever see me use whitespace in this tutorial, make sure you do as well. | ||
# Some other useful comparators: != (not equal), >, >= (greater than or equal to), <, <= (less than or equal to), 'and', 'or', 'not' | # Some other useful comparators: != (not equal), >, >= (greater than or equal to), <, <= (less than or equal to), 'and', 'or', 'not' | ||
Line 68: | Line 71: | ||
Thus concludes our very, very brief foray into Python. That was simply the bare minimum required to understand what will be needed for this class. More concepts will be introduced as needed, whether that be in this tutorial or in a lab. For a more complete understanding of Python, you can look up other Python tutorials elsewhere on the Internet. | Thus concludes our very, very brief foray into Python. That was simply the bare minimum required to understand what will be needed for this class. More concepts will be introduced as needed, whether that be in this tutorial or in a lab. For a more complete understanding of Python, you can look up other Python tutorials elsewhere on the Internet. | ||
== | ==VPython Basics== | ||
VPython | |||
In this section we will analyze some of the objects utilized by VPython. Each object is more or less self-explanatory with equally clear fields within. | |||
# The scene has a width and a height; you can set them like so. | |||
scene.width = 1024 | |||
scene.height = 760 | |||
# Beyond that, we have vectors. | |||
# You can access each component with pos.x, pos.y, or pos.z depending on which component you want. | |||
# Now would be a good time to mention that you access an object's field with a period following the variable name. | |||
pos = vector(0, 0, 0) | |||
xComponent = pos.x | |||
yComponent = pos.y | |||
zComponent = pos.z | |||
# You can add two or more vectors together and you can multiply a vector by a scalar, | |||
# but you can't add a scalar to a vector. | |||
# To get the magnitude of or to normalize a vector, use the appropriate function. | |||
initialPos = vector(10, 3, 0) | |||
finalPos = vector(30, 15, 0) | |||
deltaR = finalPos - initialPos # -> vector(20, 12, 0) | |||
rMag = mag(finalPos) | |||
rHat = norm(finalPos) | |||
errorDemo = magR + finalPos # -> error; causes your program to crash | |||
# If you were to make, say, a ball, you would want to draw a sphere. | |||
# It has a position field, a radius field, and a color field. | |||
# When creating a new instance of an object, each field is comma separated. | |||
# You access each field the same way as we did with the position vector. | |||
ball = sphere(pos = vector(10, 10, 10), radius = 9e2, color = color.red) | |||
ballColor = ball.color | |||
ballPos = ball.pos | |||
ballRadius = ball.radius | |||
# You can draw arrows. | |||
# These also have a color and position field, but instead of a radius, they have an axis that determines their length. | |||
velocityArrow = arrow(color = color.blue, pos = vector(-10, -10, -10), axis = velocity * vScale) | |||
# There are also boxes. | |||
# They have a position vector and a size vector. | |||
myBox = box(pos = vector(50, 50, 50), size = vector(20, 15, 12)) | |||
# You can draw helixes, which are useful for depicting springs. | |||
# They have a position field, a color field, a thickness field, a radius field, and a field for the number of coils. | |||
spring = helix(pos=ceiling.pos, color=color.cyan, thickness=.003, coils=40, radius=0.015) | |||
# If you want to trace a moving object, you can have a curve follow it by adding to it every time step. | |||
posTrace = curve(color = color.yellow) | |||
trail.append(ball.pos) | |||
# You also have the option of making graphs as your program progresses; this is particularly useful for potential vs kinetic energy. | |||
Kgraph = gcurve(color = color.cyan) | |||
Ugraph = gcurve(color = color.yellow) | |||
KplusUgraph = gcurve(color = color.red) | |||
# For each time step... | |||
Kgraph.plot(pos = (t, Kenergy)) | |||
Ugraph.plot(pos = (t, Uenergy)) | |||
KplusUgraph.plot(pos = (t, Kenergy + Uenergy)) | |||
Using all of these concepts, you're able to draw and compute what you'll need for this course. | |||
Revision as of 13:10, 29 November 2017
Edited by Zach Sanchez - Fall 2017 (in progress)
The Main Idea
VPython is an extension of the Python programming language that contains a 3D graphics module called Visual, which allows users to create simple simulations of physics problems. Its primary use is for educational purposes, although it has been used in research before in the past. We will use VPython on occasion to aid us in visualizing and experimenting with the ideas that we learn in this course.
Downloading vPython
In all cases you will need to have Python 2.7.9 installed. Future versions may also work, but be warned that it may cause VPython to behave erratically.
Note for Linux: You can only install Classic VPython, which is no longer supported and may be missing some features and bug fixes that are present in the current version of VPython.
Alternatively, you can use GlowScript, a virtual environment that allows you to execute VPython code in your browser. You can create an account at http://www.glowscript.org/.
Python Basics
It's always helpful to have a background knowledge of Python, however basic, before proceeding with the applications more relevant to this course.
# Comments start with a number symbol. Use these liberally in order to keep track of the changes you make/the code you write. # We're always going to start our programs with the following headers to ensure that math works correctly. from__future__ import division from visual import*
# Math works as you would expect. 1 + 1 # -> 2 2 - 1 # -> 1 3 * 2 # -> 6 8 / 4 # -> 4 (1 + 3) * 2 # -> 8 1 + (3 * 2) # -> 7
# That is, until you need to do an exponent. But you shouldn't encounter anything much weirder than that math-wise. 2 ** 3 # -> 8
# There's also various math functions you can use, such as for finding the square root. sqrt(4) # -> 2
# However, these math operations are useless if we don't store the values anywhere. x = 1 + 1 # Now if you try to access x in the future, it will have the value of '2'.
# You can print your variables like this: print(x) # You can also add in some text with them like this: print("The value of x is ", x)
# Sometimes we only want to execute something based on a certain condition. if x == 2: y = 3 # The '==' means that you're directly comparing two values. If they're equal, whatever is on the inside of the if-statement will be executed. # Now is a good time to note that whitespace is very important in Python. # 'y = 3' will always execute if it isn't tabbed, so even if x is equal to 7, y will still become 3. # If you ever see me use whitespace in this tutorial, make sure you do as well. # Some other useful comparators: != (not equal), >, >= (greater than or equal to), <, <= (less than or equal to), 'and', 'or', 'not' # You can add onto the above if-statement by tacking on: elif x == 3: y = 4 else: y = 5 # With elif meaning "else if" for a second possible condition, and "else" for something that you want to execute if none of the earlier conditions are met.
# You can also have a loop that executes code for a certain amount of time or based on a certain condition. while x < 5: print("x is ", x) x = x + 1 # This will output the value of x up until it reaches 4.
Thus concludes our very, very brief foray into Python. That was simply the bare minimum required to understand what will be needed for this class. More concepts will be introduced as needed, whether that be in this tutorial or in a lab. For a more complete understanding of Python, you can look up other Python tutorials elsewhere on the Internet.
VPython Basics
In this section we will analyze some of the objects utilized by VPython. Each object is more or less self-explanatory with equally clear fields within.
# The scene has a width and a height; you can set them like so. scene.width = 1024 scene.height = 760
# Beyond that, we have vectors. # You can access each component with pos.x, pos.y, or pos.z depending on which component you want. # Now would be a good time to mention that you access an object's field with a period following the variable name. pos = vector(0, 0, 0) xComponent = pos.x yComponent = pos.y zComponent = pos.z
# You can add two or more vectors together and you can multiply a vector by a scalar, # but you can't add a scalar to a vector. # To get the magnitude of or to normalize a vector, use the appropriate function. initialPos = vector(10, 3, 0) finalPos = vector(30, 15, 0) deltaR = finalPos - initialPos # -> vector(20, 12, 0) rMag = mag(finalPos) rHat = norm(finalPos) errorDemo = magR + finalPos # -> error; causes your program to crash
# If you were to make, say, a ball, you would want to draw a sphere. # It has a position field, a radius field, and a color field. # When creating a new instance of an object, each field is comma separated. # You access each field the same way as we did with the position vector. ball = sphere(pos = vector(10, 10, 10), radius = 9e2, color = color.red) ballColor = ball.color ballPos = ball.pos ballRadius = ball.radius
# You can draw arrows. # These also have a color and position field, but instead of a radius, they have an axis that determines their length. velocityArrow = arrow(color = color.blue, pos = vector(-10, -10, -10), axis = velocity * vScale)
# There are also boxes. # They have a position vector and a size vector. myBox = box(pos = vector(50, 50, 50), size = vector(20, 15, 12))
# You can draw helixes, which are useful for depicting springs. # They have a position field, a color field, a thickness field, a radius field, and a field for the number of coils. spring = helix(pos=ceiling.pos, color=color.cyan, thickness=.003, coils=40, radius=0.015)
# If you want to trace a moving object, you can have a curve follow it by adding to it every time step. posTrace = curve(color = color.yellow) trail.append(ball.pos)
# You also have the option of making graphs as your program progresses; this is particularly useful for potential vs kinetic energy. Kgraph = gcurve(color = color.cyan) Ugraph = gcurve(color = color.yellow) KplusUgraph = gcurve(color = color.red) # For each time step... Kgraph.plot(pos = (t, Kenergy)) Ugraph.plot(pos = (t, Uenergy)) KplusUgraph.plot(pos = (t, Kenergy + Uenergy))
Using all of these concepts, you're able to draw and compute what you'll need for this course.