With examples using python.
- Statement: A command given to the computer.
- Assign a value of -9.8 to a variable called “g”:
g = -9.8
print g
- strings:
x = "hello"
x = 9.8
x = 5
x = True
- Add two numbers and assign the result to a variable called “y”:
y = 3 + 5
z = y / 2
- + – * / are used for addition, subtraction, multiplication, and division respectively
- ** is used for exponents so 5 squared (52) is:
a = 5**2
- “For” loops are good for doing things a set number of times
for i in range(5): print i
results in:
0 1 2 3 4
- Assign a value to a variable called “x”, check to see if the value is greater than 10, and print out a different sentence based on the result:
x = 12 if (x > 10): print "x is greater than 10" else: print "x is less than 10"
-
Create a function to calculate the force of gravity at the Earth’s surface if you give it a mass:
def forceOfGravity(mass): Fg = mass * -9.8 return Fg
Call the function to find the force of gravity acting on a mass of 100 kg and print out the result:
x = forceOfGravity(100) print x
the result should be:
-980
-
In vpython there is a class called “sphere”. It renders a 3d sphere on the screen. Here we’ll create a sphere, assign it to a variable “ball” and then change one of its built-in properties, the color, from the default (white) to red. (if you are using Glowscript.org then don’t use the first line that imports the visual module).
from visual import * ball = sphere() ball.color = color.red