Member-only story

A Physicist’s Guide to Functions in Python

Rhett Allain
8 min readDec 17, 2020

--

Maybe you are a physicist and maybe you want to use python. Well, then this is for you. OK, let’s just jump right in.

Introduction to Functions

What is a function in python? Honestly, it’s a lot like a mathematical function. So, let’s just start with a function (totally just making up something here).

This basically like a box that you feed a number into and it spits out some other number. But I assume you already know about mathematical functions. You want to know about python functions.

Well, here’s the surprise — at a certain level, a python function is the same as a math function. OK, let’s just jump into python. Technically, I’m going to be using Glowscript. This is an online version of python with some extra stuff added (like vectors, graphing, and 3D visualizations). Trust me, it’s great.

So, let’s make a python function that does the same thing as this mathematical function. Here is the code and then I will make some comments (also, the code is online too so you can edit it and stuff).

Here is the output when you run it.

--

--

Rhett Allain
Rhett Allain

Written by Rhett Allain

Physics faculty, science blogger of all things geek. Technical Consultant for CBS MacGyver and MythBusters. WIRED blogger.

Responses (1)