# Category Archives: Python

# ROOT Histograms in Python

ROOT is a data analysis framework developed by CERN that is well-suited for the analysis of certain large scientific data sets such as particle collision events and astronomical data. One useful ROOT feature are the histograms. A simple program that histograms 100,000 Gaussian random numbers is shown here: It’s output is shown here: A second […]

# ROOT’s TLorentzVector Class in Python

ROOT is a data analysis framework developed by CERN that is well-suited for the analysis of certain large scientific data sets such as particle collision events and astronomical data. One useful part of ROOT is their TLorentzVector class—objects that store and can work with relativistic four-vectors such as the space-time 4-vector or the energy-momentum 4-vector. […]

# Barnsley Fern in Python

The Barnsley Fern is a beautiful fractal that can easily be generated in Python. If we zoom in on one branch, we see that the pattern is repeated: The python code follows.

# Prime Number Sieve in Python

This program computes all the prime numbers up to 10,000 using an efficient algorithm. Instead of checking if a number n is prime by dividing by all previous numbers, or even all previous primes, this program only divides by all previous primes less than or equal to the square root of n.

# Newton’s Cradle in Visual Python

This simple program shows an animation of a two-pendulum version of Newton’s cradle. One pendulum begins at some starting angle and the other pendulum is stationary. When the moving pendulum slams into the stationary one, its momentum is transferred to the other pendulum. If you want to include damping, you can change the line to […]

# Cannonball in Visual Python

Just a very basic example of projectile motion in Visual Python. The cannon fires when the mouse is clicked in the window.

# Simple Pendulum in Visual Python

Below is the code for a simple pendulum in visual python.

# Animated GIFs from Visual Python in Windows

One option for creating animated GIFs from your visual python program is add the pillow package that allows you to export images of your vpython window from within your program. You can then use GIMP to create an animation such as the one shown here. To use python’s image library, you can install the “pillow” […]

# Fancy Simple Pendulum in Visual Python

Here is the visual python code for a “fancy” pendulum exhibiting simple harmonic motion. Clicking anywhere in the window will reset the pendulum to that angle.