We use Python coding in all of my high school physics classes. Here is what I'll get into here.
- Why we do it -- an argument for including coding in all physics classes.
- What we do -- a bit on my experience, and what we do with computational solving in physics.
- Where to go from here -- what you might do if you want to try Python in physics class and some of my plans to expand.
Why we do it
I was initially inspired to include Python coding in my physics classes by a few former students. They shared with me that their college physics homework was all done in Python, and they recommended I include some Python in my high school courses. These were students who took two years of physics with me -- Honors Physics followed by AP C Mechanics, and they were engineering and science track students. After more inquiries to former students I discovered a lot of Python work happening in college physics, and a lot of struggle with it. Learning coding for the first time in college is not ideal.
Skills and the changing job market
The world changes rapidly, and it trends towards an increase in jobs that require computer science skills. There are already many fields with jobs that rely heavily on computer science skills, from market analysis to advertising research to graphic design. There is also a trend of robots replacing people in jobs. Artificial intelligence replaces people in jobs now too. The new people jobs become designing, developing, selling, installing, programming, maintaining, upgrading, and training the robots and AI. Who knows what the jobs of the future will be? But I think it's safe to bet that many of them will be completely different than what we have now, and many of the new jobs will require some level of comfort, knowledge and skill with computer science. There are many who predict that there won't be enough jobs to employ most of the employable in the future. Having the right skills will be key if you want to be one of the ones with a job.
They want computer science but can't/won't take the class
Many students don't get to work on coding skills in high school for various reasons. The might do some coding in elementary or middle school, and then not again until college or career. They skip the step where they are learning the useful math for coding. For some, their school doesn't offer computer science courses. For others, they cant fit in in their schedule, or they prioritize other electives over it, or the class is stigmatized, or they are scared of difficulty and failure. I believe computer science should be embedded in the curriculum in as many classes as possible throughout K-12 education. Students really buy into the idea of learning useful skills if you can show them they are actually useful. I'll be doing a whole blog post about that some other time.
Most science fields of research have heavy elements of coding and computer science these days. A lot of cutting edge research now involves complex systems or stuff so small that we cannot see it directly. These situations often require computer simulations to make real predictions. Weather patterns, the spread of a virus in a pandemic, cosmological evolution of the universe, or quantum field theory predictions. There are many examples of research in science requiring computer simulation of data alongside measurement and observations. This is also true in research and development in industry. You can still prototype, model with clay, use wind tunnels and whatnot, but you can also do some pretty amazing streamlined digital prototyping and performance testing with 3D computer design software and physics environment simulation. Any time we can simulate what happens in research in the field we are making science learning more meaningful.
When a problem is complex enough to be interesting and solvable with code, there is often a wonderful element of freedom of expression and creativity in coding solutions. No two solutions will be identical, and you often get some pretty wildly different ones to celebrate. This kind of freedom of expression is often missing in science education. Coding can be a great way to get some design process experience in a science class. We sometimes do this with engineering projects, but coding is another great option, or even something to include with that engineering project.
Python makes physics easier. Once students develop some basic coding skills and write a few tools they have the power make their physics lives more comfortable. If they can write a 1D kinematics solver that asks for given information, unknown variables and then solves for an unknown using vector math, all of a sudden the headache of solving kinematics problems for the first time, which is often the early topic that makes many students absolute hate physics, is not so bad any more. They cannot program the tool without truly understanding and manipulating the vector equations, so it's not a way around understanding the physics. They cannot use the tool without understanding the setup of a problem. But they can avoid making some silly little algebra mistake or a data transfer error which often happens on paper and completely derails a problem. They also have a tool that will tell them if they solved their problem by hand correctly. Show them that they can write code to do their homework problems and they get really excited! Change the assignment to a simulation of a lab setup or a project design and they might end up writing some code that streamlines their design process, or gives them a simulated prediction to test with hands-on materials.
What we do
After doing some exploring on the internet about the plausibility of incorporating coding in physics lcass (I think this was in 2011?), and reading great articles like Rhett Allain's You Should Be Coding in Your Physics Course, I decided to try it out. I had my students and me (I learned Python alongside them!) spend a class period or two on Codecademy's Learn Python course and then I had them coding some basic equation calculators to use with some of their engineering projects in small groups. The ones who were not into just looked over shoulders and nodded or often didn't even bother looking, and the ones who were interested seemed to really get a lot out of it. We used Trinket.io, which is an awesome browser-based coding environment. It was all pretty low key and fit into the big project model I had for students playing different roles in their groups. There was a coder role.
Getting everyone involved
Fast forward through a couple years of similar small group coding work and then I change schools. At my new school I start having all of my students coding Python for problem-solving using Trinket or their own machines if they preferred to install it. The regular physics students mostly make calculators with some basic I/O, conditions and control flow, function writing, and plotting. The students taking classical mechanics also write while loops to do iterations on motion and the momentum principle. The honors students do all of that stuff plus a lot with VPython using Glowscript in Trinket -- 3D vector iterations for animation, programming useful vector tools, and writing and testing 3D simulations for use with their labs and projects. The calculus based physics students get into writing orbit simulations and more complex applications. I am going on year five of having all students in every level of physics learn Python.
The Trinket below is an example of the kind of predictive animation code a student can write after having learned to use while loops for momentum principle iterations. Press the play button and click on the mass on a spring to make it run. For my students to write that code it would involve adding a few lines of code to a tool they would have already written in order to incorporate a spring force.
Building up and repurposing tools
Starting with basic iterations on motion all of my mechanics students, which is most of my students at some point in their physics adventures, eventually work their way to writing an animation function that they can call on an object. It usually has a while loop that uses force to make little updates to momentum over small time intervals and then uses that to update position of the mass (redraws the object in the field to animate and/or prints location information). They can use this in all kinds of awesome ways. Building up their tools over time, making them more sophisticated and useful in steps, means new assignments don't all start with staring at that intimidating blank slate and flashing cursor wondering what in the world to code. Rather, they often start with coming up with a plan to modify or use code they already have written to do some cool new physics stuff.
Running programmed spreadsheets in parallel
This past year I also had all of my mechanics students programming spreadsheets alongside their Python while loops for motion iterations to really make sure they understood and appreciated what was going on with the loops. Also, programming spreadsheets can be a useful skill. We broke off the spreadsheet programming once things got complicated with 3D vectors and VPython in the honors classes, because it's just a bit too complicated to be worth the time in spreadsheets. And that's about the time appreciation for the power of code starts to grow.
Let the students design and write the code
I want my students coding their own solutions. I avoid giving my students code written by me for them to modify for assignments. I give them examples, but none of them come close to solving the problems I ask them to solve. Much of the physics coding material I find when I search, written by professors and teachers, involves giving students a fairly impressive piece of code, so students can modify it or finish it to do something more. I have found that method of providing unfinished or easily modified pre-written code is not very beneficial to them. Students have given me feedback about that, telling me that it is definitely easier to modify someone else's code for assignments, but that they don't really understand what the code is doing after completing those assignments. It's easier, but they don't learn to code. That method also skips all of the wonderful, sometimes frustrating, but certainly important steps of designing creative solutions using code. I think back to my multivariable calculus class in college, where I retyped lines of code verbatim from the lab course pack into MATLAB and printed out the results. I got nothing out of it.
Most of the impressive code written for my classes is not written by me. I mean, I write some pretty cool stuff sometimes, but I don't usually do much of anything with it. I often come up with a problem to solve with code and then assign it never having previously solved the problem myself. It gives me something fun to do, and it prevents me from guiding them to my solution. I haven't even thought through a solution at that point, so I can't. This follows a theme for my recent curriculum which I'll blog on some other time -- most of the best stuff I create is opportunity.
My class materials looks pretty empty without student contributions. I have plenty of examples of assignments I give, but the assignment are not impressive at all on their own. They are often just a description of a problem to solve or some code output requirements. I have even moved away from identifying code step requirements, as I often accidentally lay out the steps for a solution design, which I really want them to do. Without a clear path to a solution we end up having tons of robust conversations, and we help each other work through many frustrations, but what comes out the other end of that creative pipeline, including student satisfaction and pride in accomplishment, is well worth all of the struggle and effort.
Where to go from here
Learn Python together
If you are interested in learning Python and incorporating it in your physics classes I encourage you to try it with your students. Just dive in and learn it while your students do. Don't do too much ahead of time. You won't remember most of it, and you'll have to revisit the tutorials or references anyway. Maybe just do a little to convince yourself it's not impossible. I had some programming background from years past, but no Python experience. I learned Python alongside my students that first year, which was a really fun way to learn it. And if you think you won't be good at it, I guarantee there will be a handful of student who are, and they will help you.
Here are some resources I gathered on my website. It's under construction, so don't go exploring expecting to be impressed. I am writing my own tutorial for some reason. It will be a general Python tutorial which I then plan on copying and modifying to make a physics themed tutorial. Who knows when those will be finished, but they probably won't be anywhere near as good as the other ones out there. After you get through Python basics you can throw an assignment at them and revel in the beautiful chaos that is the design process, but it's probably better to hold their hands through the steps on the first one, modeling some of the better methods to solve problems using code.
The top-down design approach is always best. Make sure students (and you) start by planning the steps for your algorithm on paper, or whiteboard, or some platform that is not code. Write out the steps your code will take first and then go code it. I start coding with an outline made of code comments. After planning my algorithm steps in words on a whiteboard I like to type them out and then paste them in to use as hashtag code comments, coding underneath each comment step as I go. The design process can be messy, especially in the first couple assignments, but it is worth the struggle. Once you get to assignments that are complex enough to where solutions will differ greatly you will have all kinds of wonderful results to compare, contrast, and celebrate.
If you want to know more about what I do or you want more access to what I use, feel free to contact me. There are certainly people out there way better than me at this, but I love working with other teachers.
What's on my coding in Physics expansion agenda?
I plan on trying to continue making coding more relevant and useful for physics this year. These are some of the things I want to do more of.
- Python scripts to read and analyze a data file -- plots and statistics done on measurements.
- Using the Python console as a tool for on-the-fly physics (calculator)
- Giving students more freedom and options for how to use coding for physics
- Build a library of cool student-written functions
- Numerical approximations for calculus solutions in 2nd year physics
Python coding is worth doing in high school physics class for many reasons, it makes physics problem-solving more robust and relevant, and it’s a fun way to develop creative design process skills. I hope you try it and I wish you luck!