Playing with Python


Bouncing Balls
  1. Download the following files 1.py, 2.py, 3.py, 4.py, 5.py, gas1.py, gas2.py, gas3.py
  2. Install vpython on your computer (under ubuntu you can install package python-visual)
  3. Type "idle"
  4. From the python shell window go to file and select open...
      This will cause a new window to pop offering a number of possible files for slection.
  5. In the new window and under file name type 1.py and then click on open.
      This will cause the file menu to close and another window to pop up called 1.py and containing the python code.
  6. In the 1.py window select run and run module
  7. You should get a nice picture of a green ball and red wall!
  • Play around with the mouse to move the image. Can you shrink or enlarge it?
  • Can you change the color of the ball or wall?
  • Can you make the ball larger?
You can kill a window by clicking on the "X" in the top righthand corner.

Return to step 2 above and read in file 2.py. Can you guess what this program will do just by looking at the source code? Now try running the program to confirm your expectation!

  • File 2.py contains a line timestep=0.05. Change the value from 0.05 to something else. What do you expect to happen? Run the code and see.
Now repeat the above with the code in files 3.py, 4.py and 5.py


From Bouncing Balls to Reality
While drawing balls bouncing around may seem like a bit of fun, this can also be used as a simple model of a gas.
  • Try gas1.py - this contains a bunch of balls colliding into each other and bouncing off the walls of a box.
  • gas2.py - is the same as above, but also contains a histogram showing the number of balls of a given speed. Superimposed on the plot is a curve showing a theoretical prediction of the speed distribution (a gaussian distribution). After some time you will find the histogram begins to closely resemble the ttheoretical distirbution.
  • gas3.py has a bunch of balls bouncing around in a box with that has a "lid" which is movable. The lid falls compressing the balls, until the pressure of the balls bouncing on the lid is sufficient to support the weight of the lid.