Python vs C++: Which One Should You Use?
Python vs C++, which should you choose? Python and C++ are both proven languages that are used by almost any IT company in the world. But let’s pause for a second here and ask the real question. If you’re reading this, you probably want to know:
- When should you use Python vs C++?
- Which should you learn if you are a beginner?
For this post, I’m going to use a very practical approach. Who really cares about getting all the possible small technical details? What you want is the big picture, and some helpful recommendations from someone with real-world experience, so you can make a good choice.
Let’s get something clear first. A programming language is just a tool. When you learn how to program, you don’t learn a “language.” You learn how to solve problems in an efficient way, using code as a means to an end.
Give an experienced carpenter a new hammer and they will be able to use it much better than any newbie around, even if they have never used this specific hammer before. Well, it works the same for programming.
Last Updated January 2023
Get started quickly with Python (Python 3): Only Hands-on Lessons and Practice to Master Python basics. | By Edouard RenardExplore Course
So let’s get started and see what the practical differences are between the Python tool and the C++ tool when it comes to solving problems.
Similarities between Python and C++
Both Python and C++ are very mature languages. Python was created in 1991, and C++ in 1979. Both languages have remained popular through the years and experienced many changes, but they are now both mature and stable — possibly more stable than some other languages that are victims of “hey, let’s create a new flashy framework every six months.”
Both languages are very popular. Python is actually more popular than C++ (2.5* more Python questions than C++ questions on StackOverflow, for example), but overall, both are in the top five more popular languages (source: IEEE).
Both languages have the potential to get you a high-paying job. I’m not going to include numbers here, because salaries vary a lot depending on where you live in the world and what kind of company you want to work for (unfortunately not every company in the world is a U.S.-based company paying U.S. dollars, which is what we often find in “salary studies”). All I can tell you is that if you get good at any language, then you can potentially get paid very well.
When it comes to the technical aspects, both C++ and Python are Object-Oriented Programming languages. Object-Oriented Programming (OOP) is a paradigm (a way to program) used almost everywhere. What this means is that once you know OOP for one language, you can use the same concepts for another language.
Let’s now focus on the differences between Python and C++ when it comes to ease of use and performance. After this, we can look at why one language is better suited for a certain type of application than the other.
Ease of learning/coding — Python wins
We have a clear winner here: Python. And why is that?
Python programming is less strict than C++ programming, and you’ll write fewer lines of code for the same output.
- Dynamically typed
- Interpreted language
- Garbage collection
- Statically typed
- Compiled language
- No garbage collection
Now, what does that mean?
- Dynamically vs statically typed: When creating a variable in C++, you need to provide a data type that exactly matches what’s in the variable. In Python, you just create the variable and the type is automatically assigned to it.
- Interpreted vs compiled language: This means that you can just write Python code and run it on the go to test it. With C++, after you write the code, you first need to compile it, and then run the created executable.
- Garbage collection: Basically, when you have garbage collection, you don’t need to worry about memory leaks. Anything you create in your program is going to be “destroyed” at some point. So, for C++, you need to manually do that. Note: With modern C++, you can use smart pointers, which handle memory management for you.
So, learning Python is going to be faster than learning C++. And usually, for the same program, the Python source code will be something around five times smaller than the C++ source code. If you know both languages, and you have the option to use them both for the same application (more on that later), programming with Python is also probably going to make the process of writing the code quicker.
As a quick illustration, here are two videos showing you how to get the user input with Python and C++, so you can see how many lines you need and how complicated the programs look.
Get the user input with Python:
Get the user input with C++:
Execution speed — C++ wins
Well, you can’t have it all. If Python is the winner when it comes to the ease of use, C++ will completely destroy Python on execution speed. C++ is faster than Python. The main reason for this is that Python is an interpreted language, and C++ is a compiled language.
When you execute a Python program, the Python interpreter is going to read each line one by one, and execute it on the fly. In C++, you first compile all the code into a binary executable. The compiler is going to optimize a lot of things. And running a binary program is much faster for a computer than reading a text document and analyzing each line one by one.
All the ease of use you get with Python is also the price you pay when it comes to performance. Now, don’t get me wrong here: It’s not because C++ is faster that you should use it by default. It all depends on the application you are going to use it for. If Python is fast enough for what you need, then use Python. As Donald Knuth said: “Premature optimization is the root of all evil.” Don’t make your life more complicated without a good reason.
Python and C++ binding
One thing I want to quickly explain before we go further is that you can bind Python and C++ code. That is, you can write Python code that can call a C++ algorithm. This way, you benefit from the ease of use of Python (on the interface level) and the performance of C++.
The great thing is that when you use the Python module, you don’t even know that some C++ code is being executed. This is a pretty common practice, and with this, “Python vs C++” becomes “Python AND C++.” Many Python libraries actually use this technique.
So, be aware of that: It’s not because you’re writing code in Python that you’re not using C++! And this is something we’re going to encounter quite often in the following applications.
Python vs C++ for different application types
OK, we are now clear on two points:
- Python is the winner for ease of use.
- C++ is the winner for performance.
Now, there is another factor you need to take into account. This factor is what type of application you are going to use it for. Some kinds of applications have a very strong Python community, Python libraries/frameworks, and Python courses/tutorials. For other fields, C++ will prevail.
So, what I’m telling you is that for certain types of applications, you don’t even need to think about Python vs C++. Simply because one of them is clearly favored and already working better with a complete ecosystem/community/etc. behind it.
For some types of applications, you will still have a choice, and then you can use the “ease of use/performance” ratio to decide what’s worth your time.
Back-end web development
One exception here: If you’re working on some specific kind of engine running in the background that is used by your Python framework, you might develop with C++. And guess what? In this case, you’d use a Python and C++ binding, so that both languages work together.
Artificial intelligence and data science
For artificial intelligence (AI) and data science, Python is also a winner. Many of the most popular data science libraries are available in Python. Combine these with the ease of use of Python and you will be writing code to analyze data in a very short amount of time.
If you want to go deeper and write your own algorithm and engines, you’ll probably have to use C++. In data science and artificial intelligence, as you can guess, you need speed.
So here’s what to do: Use Python when you are using AI and data science libraries, but use C++ to develop your own complex algorithms. In the later case, use the Python and C++ binding to make your C++ algorithm available from the Python interface.
If you thought you were done with the “Python vs C++” question for data science, wait for it! Python and C++ are not the only languages you can use. For example, R is a quite popular language for data science. So, the question can also be: Should you use R or Python?
The closer you get to hardware, the more performance you need. And here the answer is quite simple: almost any microcontroller you program will require you to write in C++. Python just does not exist in embedded systems. A very popular microcontroller is the STM32 family. It’s very powerful, but also with a steep learning curve. If you want to get started with the world of microcontrollers, you can have a look at Arduino for a gentler learning path.
Robotics is a large ecosystem. It combines many fields: electronics, software engineering, mechanics, artificial intelligence, and more. In robotics, the “Python vs C++” question can be quite tricky, because you can usually use both languages (or a combination of both) to write an application for a robot.
You will use:
- Microcontrollers — here, as we saw before, C++ is the default.
- Embedded computers (like Raspberry Pi), usually running Linux — here, most applications can be written with Python or C++.
- Robotics framework, for example ROS (Robot Operating System) — ROS has a complete Python interface, as well as a C++ interface.
So, which one to choose: Python or C++?
Rapid prototyping is a big part of robotics, so Python is very useful. What you can do (and what I personally do) is go with Python first to prototype the code as fast as possible. What you want to do is show that the idea is working with a proof of concept and build a working prototype as fast as possible. Performance doesn’t matter at this stage.
Once you have validated the idea, you can build the real production code with C++ when you need performance. It’s all a question of costs: In the short term, you want to optimize your time cost. In the long term, you want to optimize the hardware cost for the product (for possible mass production).
You can start writing some small games with Python, but if you want to be serious, you are going to use C++. Video games use a lot of computer resources, and you have to use the fastest tool available to you. Many game engines are written in C++. The video game community is also very heavily C++-oriented, which means you’ll find way more tutorials, courses, and libraries in C++ than you will in Python.
What to choose: Python vs C++?
Let’s have a quick recap.
If you’ve never coded in your life:
- Go with Python first. The learning curve is smaller and you’ll make progress faster. Making progress when you begin is important because it will give you the motivation to continue learning.
- Next, I encourage you to learn C++. Not just for the speed of execution, but because you’ll learn many more things about variable data types, compilation, etc. In my opinion, you have to learn a typed language to get a complete overview of programming.
Now, if you’re looking for which language to use for your next project, then it really depends on what kind of project you’re building:
- Mobile development: You’ll need to learn something other than Python or C++.
- Artificial intelligence, machine learning, data science: Use Python first because you can find many user-friendly libraries that will speed up your development time.
- Embedded systems/robotics: Use Python if you can for rapid prototyping. Then if you need to write production code with real-time constraints, use C++.
- Video games: C++ is really the best choice here. You need speed, a lot of speed.
One thing you can’t go wrong with: learn both Python and C++. As you could see in the few examples above, there is no black-and-white answer to this question. You will often need the combination of both languages. And what’s the good news here? Well you can learn those two languages in a practical way, with these Practical Python and Practical C++ courses for beginners.
Top courses in Python
Python students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.