I can't believe this title wasn't already taken—I couldn't find it anywhere. But I guess some context is needed first for my international audience. "Numpty" is a predominantly British slang term for someone who's a bit silly or stupid. But it's normally used in a somewhat endearing manner and not as an insult.
And ever since the first time I encountered Python's NumPy library, I couldn't help myself thinking "numpty" instead of "NumPy" each time I read or said the library's name. I still do! This is probably why I mostly pronounce NumPy to rhyme with "bumpy" rather than "plum pie".
So, "NumPy for Numpties" is a bit like "NumPy for Dummies". But better!
I'll get to NumPy shortly, I promise, but here's another twist in the NumPy-numpty story. I looked up the etymology of "numpty", and guess what? The word was originally "numpy", not "numpty". No, I'm not making this up. The "t" apparently came in later, inspired by the Humpty Dumpty nursery rhyme.
The Python Coding Place is now live and the Members’ Area will launch in January. It will include members-only video courses, weekly videos on varied Python topics, workshops, live cohort courses, and more. Membership is through a one-time fee that gives you access to everything, forever
Meandering Through NumPy • A Loose Series
I wrote about NumPy in my pre-Substack days. Some articles were deep dives into particular NumPy topics. Others were step-by-step walk-throughs of projects centred around NumPy. In fact, the most popular article I ever wrote, used by physics and engineering departments around the world, and the only one that makes it to the top of a Google search is based on NumPy: How to Create Any Image Using Only Sine Functions | 2D Fourier Transform in Python.
I haven't written about NumPy in The Python Coding Stack yet. This is about to change. But I also want to spread NumPy articles and other science-y topics so there aren't too many too close to each other.
The NumPy for Numpties series here on The Stack is not a NumPy course. There will be courses covering NumPy and other topics at The Python Coding Place soon. (You can become a lifetime member now if you want—lifetime membership via a one-time fee is the only type of membership on offer at The Place! And there's an unmissable pre-launch offer at the moment. That's the advertorial done!)
The series will cover topics in no particular order. It will be a pseudo-random walk across NumPy. Some articles will focus on specific NumPy functions. Others will be mini-projects centred around NumPy and, inevitably, some Matplotlib. This is what I mean by a "loose series".
A Taste of NumPy
The aim of this post is to introduce the NumPy for Numpties series. But I can't really say my goodbyes without any Python code, can I? So here's a taste of NumPy.
The first time I saw the following trick, I thought it was magic. Full disclosure: this "first time" was using MATLAB, another programming language I used in the past, but the Python NumPy version is very similar.
You'll use NumPy and Matplotlib in this example. Neither package is part of the standard library, so you'll need to install them using the following line in the terminal/shell (or whichever way you prefer to install packages):
$ python -m pip install numpy matplotlib
Let's create a disc:
This code generates the following image:
I won't dwell too much on the code (there'll be plenty of opportunity in future articles in this series) but here's a brief overview:
You import
numpy
(without the "t"!) andmatplotlib.pyplot
using aliases. This is the convention with these libraries.You use
np.linspace()
to create a one-dimensional array from-1
to1
. The array has500
equally spaced values. You're in luck. I wrote a detailed article aboutnp.linspace()
for Real Python a few years ago: np.linspace(): Create Evenly or Non-Evenly Spaced Arrays.The following line is where the magic happens. You convert the 1D array into 2D counterparts that you can use to convert mathematical equations into 2D arrays and 2D plots. The magic happens with
np.meshgrid()
. And look, you're in luck again, as I also have a detailed article about this function from my pre-Substack days: numpy.meshgrid(): How Does It Work? When Do You Need It? Are There Better Alternatives?Next, you create
disc
, which is a 2D array. BothX
andY
are 2D arrays. Therefore,(X ** 2) + (Y ** 2)
is also a 2D array. But you also use the<
operator, which returns a 2D array of Booleans. I'll demonstrate this below.The last two lines plot the image.
Let's look at a scaled-down version of the 2D arrays X
and Y
. In this version, the initial array x
only has 10
values. The outputs from the calls to print()
are shown as comments:
Note how np.meshgrid()
converts the 1D array into a 2D one by repeating the array. Each row in X
is identical to the original array x
. I'll let you figure out what Y
looks like.
Let me finish off this short article with one more example of a 2D plot using np.linspace()
and np.meshgrid()
:
Here's the output from this code:
Stay tuned for more NumPy fun. And no, you're not a numpty!
Code in this article uses Python 3.12
Stop Stack
#38
The Python Coding Place has moved on to the next stage, with a new website and the next new set of resources that I started to roll out: video courses. The first course is called A Python Tale and it's designed for beginners. But I have many other courses at advanced stages of planning and preparation, so you can expect many new courses at The Place over the coming weeks and months for beginners and intermediate learners. If you know of any beginners starting to learn to code, you can recommend A Python Tale. I'll say more about membership of The Python Coding Place in future Stop Stacks, but in the meantime you can have a look at the details about membership on the website.
Recently published articles on The Python Coding Stack:
The Curious Little Shop at The End of My Street • Python's f-strings Many people's favourite Python feature: f-strings
Python City (Monty and The White Room Series #3) Part 3 in the Monty and The White Room Series
This Page Is Intentionally Left Blank • The Story of
None
(Paid article) Understanding Python'sNone
The Function Room (Monty and The White Room Series #2) Part 2 in the Monty and The White Room Series • Understanding functions
Monty and The White Room (Monty and The White Room Series #1) Understanding a Python program through The White Room analogy • Part 1
Recently published articles on Breaking the Rules, my other substack about narrative technical writing:
The Selfish Reason (Ep. 13) Another reason for authors to innovate • Enjoying the writing process
The Consequential Detail (Ep. 12). Can a single letter or one blank line make a difference? (Spoiler Alert: Yes)
The Unexpected Audience (Ep. 11). What I'm learning from listening to Feynman's physics lectures
The Story So Far (Mid-Season* Review). Are you back from your holidays? Catch up with what you've missed
Broken Rules (Ep. 10). Let's not lose sight of why it's good to break the rules—sometimes
Stats on the Stack
Age: 7 months and 2 days old
Number of articles: 38
Subscribers: 1,323
Each article is the result of years of experience and many hours of work. Hope you enjoy each one and find them useful. If you're in a position to do so, you can support this Substack further with a paid subscription. In addition to supporting this work, you'll get access to the full archive of articles and some paid-only articles. Alternatively, if you become a member of The Python Coding Place, you'll get access to all articles on The Stack as part of that membership.
This is great! Love it and can’t wait for more numpy and maybe even pandas? ...