Python vs. C/C++: Why Should Electrical Engineers Bother Learning Python? - LEKULE

Breaking

14 Apr 2019

Python vs. C/C++: Why Should Electrical Engineers Bother Learning Python?

If you learn one 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 to learn yet another programming language. There are many out there: Ruby, PHP, Java, C#, Javascript, Dart, Go, Rust, etc. Not for you, nope. You’re already fluent in C/C++, which takes you from embedded firmware all the way to GUI applications.

Why would you want to spend the time to learn another programming language that will be obsolete before the next president rolls around?
Well, my friend, I’ll let you in on a little secret. If you’re willing to learn one more language, it can potentially open up a whole new world for you.

As electrical engineers, if you learn one 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 dig into the nuts and bolts of the hardware, writing values into different registers, accessing memory buses, and controlling hardware peripherals.

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


Image used courtesy of Christina Morillo

Power and Control vs. Simplicity and Flexibility

The reason C++ is so useful for hardware designers is the exact reason why it’s not so great for writing applications. You get all kinds of control over the bare metal—but, since you control it, it’s all your responsibility. You have to make sure that you de-allocate any memory you allocate, that your pointers actually point to valid locations in memory, and that your data types are assigned properly and don’t overflow.

What you get from all that awareness is raw power and speed but heaven forbid you to make a mistake since it could send the whole application or even the complete system crashing down.
This is where Python is a joy to work with. Sometimes you don’t need all that speed or to control things in memory so tightly. Instead, you just need something to work.

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

Python abstracts away a lot of the details we need to deal with in C++ such as memory management and variable data types. There’s no more worrying if a variable will overflow or if the correct amount of memory was allocated or de-allocated. That all happens magically in the background. You just need to focus on putting code on (virtual) paper.

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

Even something as simple as opening a file and parsing its contents, something that EEs have to do regularly for all sorts of things, is painfully easy in Python. Here’s an example where we open a file and print its contents to the screen:

C++

 

Python

The Case for Python

Now I’m not here to preach which language is better. I use both languages regularly as well as others like Java, C#, and Javascript on an as needed basis. But in my opinion, if I had to give advice on what languages an up and coming EE should learn, my advice would be first C++, then Python. (Hmmm…and perhaps Verilog, depending on whether or not you do programmable logic, but that’s a different article series.)

This article is a starting point for a larger series on Python as it relates to electrical engineers. As we get deeper into this series, I’m hoping it will become more and more apparent why an understanding of Python will open up new worlds to an electrical engineer.

Even though I just used a very trivial example of file parsing above, that’s the very foundation of tons of design automation as well as data science. Beyond that, we will be exploring the various libraries to learn how to set up databases, web servers, creating a REST API, mine large datasets, create slick data visualizations, detect objects with a webcam, and we’ll throw in a bit of machine learning. I threw that last one in since it’s all the rage in the techie (and non-techie) circles. You can impress the tech hipsters you talk to at Starbucks with that one.

Data visualization in Python using Seaborn. Image used courtesy of Insight Data

So put on your programming hats and stay tuned as we embark on a journey to explore new frontiers of technology from the eyes of an electrical engineer. I’m hoping that this series can not only allow you to automate things more efficiently but expand your world beyond designing hardware and circuits and into designing complex systems, systems that not only involve electronics but leverage the mountains of technology that an understanding of Python allows you to access.




What's your take on Python? What would you like to learn about it? Share your views in the comments below.

No comments: