Python Examples

Pendulum Project Requirements

 

Here is an example where you give python 2 numbers, it adds them together, and prints the result

import sys
print "Tell me a number"
number1 = float(raw_input())
print "Tell me another number"
number2 = float(raw_input())

number3 = number1 + number2
print number3

Here is an example of a 4 function calculate.  Give python 2 numbers, then pick the arithmetic  function you want to perform.

import sys
print "Tell me a number"
number1 = float(raw_input())
print "Tell me another number"
number2 = float(raw_input())

print "What operation would you like? (add subtract multiply or divide)"
operation = raw_input()
if operation == "add":
   answer = number1 + number2
   print "The answer is", answer
if operation == "multiply":
     answer = number1*number2
     print "The answer is", answer
if operation == "divide": 
     answer = number1/number2 
     print "The answer is", answer
elif operation == "subtract":
   answer = number1 - number2
   print "The answer is", answer

Don’t forget, python does all angles in RADIANS. The following example

  • converts to radians
  • finds the sine of that angle
  • prints the sine  of that angle
import sys math
print "Tell me an angle in degrees"
angle1 = float(raw_input())
angle1_in_radians = math.radians(angle1)
print math.sin(angle1_in_radians)


Example of a pendulum in vpython (stolen shamelessly from  Anil Kumar at Tech For Curious)

from visual import*
from visual.graph import*
display(width=500,height=500,center=vector(6,0,0),background=color.white)
wall=box(pos=vector(0,1,0),size=vector(0.2,3,2),color=color.green) 
floor=box(pos=vector(6,-0.6,0),size=vector(14,0.2,4),color=color.green)
Mass=box(pos=vector(9,0,0),velocity=vector(1,0,0),size=vector(1,1,1),mass=1.0,color=color.blue)
pivot=vector(0,0,0)
spring=helix(pos=pivot,axis=Mass.pos-pivot,radius=0.4,constant=1,thickness=0.1,coils=20,color=color.red)
eq=vector(9,0,0)
t=0
dt=0.001
graph1=gdisplay(x=550,y=0,width=400,height=400,title='Energy Vs. Time', xtitle='time',ytitle='Energy',foreground=color.black, background=color.white)
graph2=gdisplay(x=950,y=0,width=400,height=400,title='Phase Diagram', xtitle='Position',ytitle='Velocity',foreground=color.black, background=color.white)
fke=gcurve(gdisplay=graph1,color=color.red)
fpe=gcurve(gdisplay=graph1,color=color.blue)
fte=gcurve(gdisplay=graph1,color=color.black)
fphase=gcurve(gdisplay=graph2,color=color.black)
while (t<50):
 rate(1000)
 acc=(eq-Mass.pos)*(spring.constant/Mass.mass)
 Mass.velocity=Mass.velocity+acc*dt
 Mass.pos=Mass.pos+Mass.velocity*dt
 spring.axis=Mass.pos-spring.pos
 KE=0.5*Mass.mass*(Mass.velocity.x)**2
 PE=0.5*spring.constant*(eq.x-Mass.pos.x)**2
 fke.plot(pos=(t,KE))
 fpe.plot(pos=(t,PE))
 fte.plot(pos=(t,KE+PE))
 fphase.plot(pos=(eq.x-Mass.pos.x,Mass.velocity.x))
 t=t+dt

Examples of an animatable pendulum with inputs in glowscript

Example of an animated pendulum and graphs in glowscript  (stolen shamelessly from Anil Kumar.)

Our Theories Don't Take No For An Answer