Maria Montero

Python vs. C / C ++: Why do electrical engineers …

If you learn a programming language, it should be C / C ++. But if you learn two, here’s an argument that the other should be Python.

As an engineer, the last thing you probably need right now is learning another programming language. There are many out there: Ruby, PHP, Java, C #, Javascript, Dart, Go, Rust, etc. Not for you, no. You are already fluent in C / C ++, which takes you from embedded firmware to GUI applications.

Why would you want to spend the time learning another programming language that will be out of date before the next president arrives?

Well, my friend, I will tell you a little secret. If you are willing to learn one more language, it can open up a whole new world for you.

As electrical engineers, if you learn a programming language, it should be C / C ++. You need it to program the microcontrollers, configure the registers, and you’ll be designing and writing test firmware to exercise various parts of the circuit. It allows you to drill down into the nuts and bolts of hardware, write values ​​to different registers, access memory buses, and control hardware peripherals.

But if you learn two languages, I would argue that the other should be Python.

Image used courtesy of christina morillo.

Power and control versus simplicity and flexibility

The reason that C ++ is so useful to hardware designers is the exact reason that it is not so good at writing applications. You have all kinds of control over metal, but how you control it is your entire responsibility. You need to make sure that you deallocate any memory that you allocate, that your pointers actually point to valid locations in memory, and that your data types are allocated correctly and not overflowing.

What you get from all that knowledge is raw power and speed, but heaven forbid you make a mistake as it could send the entire application or even the entire system to the crash.

This is where Python is a pleasure to work with. Sometimes you don’t need all that speed or control things in memory so hard. Instead, you just need something to work with.

Python is one of the fastest growing programming languages. Image used courtesy of David Robinson via StackOverflow.

Python extracts many of the details that we need to deal with in C ++, such as memory management and variable data types. No more worrying about whether a variable will overflow or whether the correct amount of memory was allocated or deallocated. All that happens magically in the background. You just have to focus on putting the code on paper (virtual).

What really makes Python stand out as a good second language is its large and growing community, the great support of open source libraries, and that it supports a diverse set of applications such as web application programming, data science , data visualization and general purpose automation. Those are all hugely powerful domains that are awfully complicated if you try to use C ++.

Even something as simple as opening a file and parsing its contents – something EE has to do regularly for all sorts of things – is painfully easy in Python. Here is an example where we open a file and print its content on the screen:

C ++