Running Python in a Browser Is More Awesome Than You Think

With trinket.io, you can run python scripts in your browser. Even cooler is that they also run on your phone.

It started with a lab experiment for my daughter's class. The assignment was to take 200 pennies and put them in a shoe box. Shake up the box and then open it and remove all the pennies with "heads" facing up. Count the number of pennies left and then repeat the process until there are no more pennies.

You might be able to guess that this simple activity gives students an appreciation for radioactive decay and maybe even the idea of half life. As far as it goes, it's not a bad experiment. However, my daughter wasn't pleased. No one wants to count all these pennies. Where do I find 200 pennies anyway?

She was at home and I was away, but I told her that I would write a simple python script that will simulate the same thing. It shouldn't be too hard to create something cool (which is why python is awesome). But wait! What if I make the script now and share it with her online? That's exactly what you can do with trinket.io.

This is the quick program I put together to simulate the penny shaking experiment. Yes, with trinket.io, you don't need to install python. You just run it in your browser.

Alas, at the time I sent this to my daughter she wasn't near a computer. Well that's ok. It also runs on your phone. Isn't that awesome? Later that night, I showed her the code and we talked about how it worked. It wasn't just a demo that she watched, she could change the number of pennies and see what happens. Even better, the code can do things you can't do with real pennies. Look at the code. In line 13 it says Nc = NC +choice([1,0]). This picks either 1 or 0. A zero would be heads and removed from the count. What happens if I change the choice to choice([1,0,0])? Go ahead and change that and see if it makes sense.

Even Better Uses For Trinket

If you can run this simulation on a phone, couldn't you write some program and also run it on my phone. It's sort of like a mini app that you can write in python and share with others. Here is another example - a tip calculator. I know that's a silly thing, but it's still fun.

Yup, it runs on your phone (well, it runs on my iOS device). In fact, I even made a quick url for it - j.mp/calctip.

Ok, I can't stop. Here is another one. This is a quadratic equation solver.

I could make more examples, but I won't. Instead I will assign them as homework.

  • Improve my tip calculator. I don't think I even need suggest something you could do to make it better. Just make it better.
  • What about a plotting program. Enter the coefficients of a polynomial and the range over which to plot and then BOOM---your graph appears. Here is an excellent tutorial on making graphs with Trinket.
  • A cross product calculator. Enter two vectors and it returns the cross product of these vectors.
  • A program that finds the solution of a linear and quadratic equation. You might want to do this either by plotting or with a numerical solution.
  • Random student picker. Create a program that has a list of all the students in your class. When you run it, it randomly picks one student so that you can have that student answer a question. (I think I might make this one)