From Python To C++, A Thorough Comparison

Must-Know Topic For All Programmers — Explaining How Similar and Different Python and C++ Are.

Farhad Malik
Jan 8 · 10 min read

During the earlier phases of my programming career, I had to work on a C++ mathematical optimisation application that was required to be highly performant. I still remember how tough the experience was!

I came across some concepts such as pointers, concurrency and garbage collection which were difficult to grasp initially.

Yet if you ask me what I think of C++, I would happily recommend C++ to anyone who wants to build a real-time application

C++ is an extremely powerful programming language

Photo by Ben Sweet on Unsplash

Recently, I have been architecting and implementing machine learning and data science solutions. My language of choice is Python. I acknowledge and appreciate how simple and powerful the language is. Some programmers, for their own valid reasons, consider Python to be a prototype programming language. However, without a second thought, I would recommend Python to anyone who wants to build a data analytical application.

Python is a developer friendly programming language

Photo by Cristofer Jeschke on Unsplash

1. Article Aim

Both C++ and Python are extremely popular programming languages. This article aims to highlight the similarities and differences between the two languages so that we can understand when to use one over the other.

There are many differences and similarities but I will only focus on the key features which we must know

First thing first, both of the programming languages have a large number of similarities and it is not difficult to learn the other language.

Photo by Benjamin Davies on Unsplash

2. Python Is Interpreted And C++ Isn’t

The way C++ works is that the code is first written in files with .cpp extension.

The .cpp files are then compiled. The compiler converts the C++ code into native machine code. This machine code is then executed on the machine. Therefore, C++ is very close to the hardware.

This makes C++ extremely fast and ideal for real-time applications. The key to note is the word “native”. It implies that the compiler is an operating system and processor dependent component.

The compiler is machine-dependent hence C++ is machine dependent

However, what it means is that the code can only run on the operating system (and processor) which is compatible with the compiler that was used to compile the code. To elaborate,

  • If a Windows compiler was used on a Windows machine to compile the code then the windows machine code is produced.
  • On the other hand, if a Linux compiler is used to compile the cpp code then the compiled code can only run on a Linux machine.

Plus sometimes, some of the C++ modules/packages are not compatible with all of the operating systems.

Photo by Anthony Tori on Unsplash

Python, on the other hand, is an interpreted language. The way Python works is that firstly a Python virtual machine is created where the packages are installed.

The python code is then written in .py files.

CPython compiles the Python code to bytecode for the Python virtual machine.

Now, this Python virtual machine is machine-dependent, just like C++ but the Python code isn’t.

When you want to execute the bytecode then the code will be interpreted at runtime. This makes Python machine-independent.

Python byte code is Python virtual machine-dependent and this makes Python machine-independent.

The point to note is that we can write Python code in one OS, copy it to another OS and simply run it. C++ is different. We have to compile the C++ code on each OS. This very difference makes C++ extremely fast when compared to Python.

Because Python is an interpreted language, it makes Python slower than C++

Photo by Ashley Batz on Unsplash

3. The Coding Conventions

Firstly, some notes on the ease of use

Python is a super high-level language whereas C++ is a low level language. Python is readable, simple, straight-forward and it does not take long to understand the language.

This is a big plus point because it means more and more developers have started to adopt Python. This feature also makes it quicker to bring Python applications to the market.

Python is becoming the programming language of choice for data science and machine learning projects due to its ease of use and the wealth of libraries available. It’s also gaining popularity in web development.

Python is great for testing out new concepts and ideas.

However, sometimes it results in programmers seeing Python as a prototype language. But then again, there are valid reasons for this view. We should always attempt to follow the best software principles and practices when implementing Python.

Python is a great language for those who are new to the programming world.

On the other hand, C++ has its roots in the C language. It is a powerful language and hence it is complicated because it can let you perform more low-level tasks.

C++ comes with a large number of conventions and rules which the developer needs to abide by.

Photo by frank mckenna on Unsplash

C++ is heavily used in game development, back end server applications and distributed trading applications that are required to perform tasks fast. C++ is extremely portable.

Let’s talk about syntax

  1. Python coding is heavily dependent on whitespaces and tabs (indentation). Everything is an object. All class properties and methods are public. There is no way to hide the properties/methods etc of a class like we can do in C++ using the access modifiers.

I found this whole indentation and formatting harder to get used to initially.

2. I guess, most developers know that there are no semi-colons or curly brackets in Python. You can write neat list comprehensions. Boolean expressions can be written without any brackets in Python. On the other hand, C++ relies on brackets and semi-colons. It takes a while to get used to C++ syntax.

3. C++ is a strongly typed language and the type of each variable needs to be declared up-front. Python, on the other hand, is dynamically typed. We do not need to indicate the type of the object. We often experience unexpected results with this dynamic nature. Python relies on the programmer to use the object in the right manner/context hence its both a positive and negative point.

Expect to experience run-time errors when executing Python code. And trust me, run-time type errors in Production can be embarassing to experience!

Photo by Paola Chaaya on Unsplash

Worth mentioning that many developers have spent time and effort on implementing Python solutions that can find issues before runtime.

4. Memory Management And Concurrency

Before I explain how memory is managed, it is worth noting that both programming languages support object-oriented programming and inheritance.

When programmers create objects in a program, these objects consume memory. When an object is not used then the program cleans this garbage using a garbage collector utility.

The garbage collector works in a non-deterministic manner.

As Python programmers, we don’t need to worry about how and when the memory is cleared. Python automatically handles the memory for us using its intelligent garbage collector. This garbage collector does not exist in C++ and you have to handle the memory yourself.

Photo by Joshua Earle on Unsplash

Memory management in C++ is a manual task.

There is a good reason for it. C++ is designed to be a high performant programming language. Garbage collectors, that manage memory and remove unused objects from memory, impact the performance of the application.

On top, a garbage collector is non-deterministic in nature. There is no guarantee of whether an object is in memory after it has been dropped.

Python is an extremely developer-friendly programming language because we do not need to worry about forgetting to free memory.

Little On Concurrency

For I/O or CPU bound applications, programmers often intend to create multiple threads in their code so that the threads can run simultaneously and it can potentially speed up the computations.

Python only lets us execute one thread at a time regardless of how many cores there are in our machine. This is controlled by its global interpreter lock. This can be problematic for number churning CPU bound applications.

We can always create multiple processes though.

On the other hand, C++ allows us to implement multithreaded applications.

Photo by Ben White on Unsplash

5. C++ have pointers but Python don’t

We are discussing C++ and we haven’t mentioned pointers!

Pointers do not exist in Python (not out of the box). However, they do in C++.

Let me explain what they are.

Imagine that a variable exists. This variable is an integer and its value is set to 5. So, a variable is synonymous with its value e.g. x = 5
x is the name of the variable and its value is 5.

In C++, variables are passed to a function by value.

Now the question is where does this value live?
Every value of a variable lives at memory addresses on your computer. The memory address of a variable can be accessed using the & operator in C++.
A pointer is a variable that holds a memory address of where the value lies.

Pointers allow us to create efficient programs.

A pointer is declared using the * operator before an identifier.

So if you do:

int* y = &x

What we have done above is to create a pointer variable y that holds the memory address of variable x.

A pointer is a variable that holds a memory address of where the value lies.

Imagine you have a function that takes a variable as an input (argument). Any modifications to the value mean creating a new variable inside the function. Remember this will take a lot of space in your memory as in C++, variables are passed to a function by value.

To elaborate, let’s create a function that increments a variable by 100.

void add_hundred(int x) {
x += 100;
}

This code takes a variable x and then increments the value by 100.

Inside the function, any modifications to the value are performed to a new variable and not the original variable.

Here is the main function that executes the code:

int main(void) {
int x = 2337;
add_hundred(x);
return 0;
}

The value of x will be 2337 outside the function, even after the function is executed.

So, why do we need a pointer?

For that, you have to remember the difference between value and reference type. Value type is like physically copying a variable and giving it a new variable each time. It takes more space and each variable gets its own memory address.

We can change the function so that it accepts a pointer as an argument. This lowers the memory footprint of the program. Plus unnecessary duplicate variables aren’t created.

The function can modify the variable’s value directly.

void add_hundred(int *x) {
*x += 100;
}
int main(void) {
int x = 2337;
int *y = &x
add_hundred(y);
return 0;
}

We created a pointer y and passed it to a function that will then increment the value of x, even outside the function. The value of x will be 2437 once the function is executed.

Photo by Daniel Jensen on Unsplash

My view on pointers

One reason to use pointers is so that a variable or an object can be modified in a called function. However, I would suggest avoiding using pointers if you can.
In C++ it is a better practice to use references than pointers because you can easily change the calling function without having to modify the semantics of passing it.

Pointers are a complex topic and programmers often end up with memory management bugs due to them. Python is a great programming language for beginners hence this complex type doesn’t exist in Python.

6. Final Verdict

What do I recommend? Python or C++?

Well, it depends on the use cases of the solution you want to implement. Does it need to be cross-platform, high performant, machine learning ready solution, etc?

If you can, use both.

Use Both

I highly recommend learning and using both Python and C++ because this will really broaden your understanding of the programming languages.

  • If you know C++, it will not take long before you can start writing Python programs.
  • If you know Python then you already have a good grasp of most of the concepts, C++ can further help you in understanding memory management, concurrency, and pointers, etc, therefore, it’s a wise idea to learn both.

Plus, one can also integrate Python and C++ together and implement superior real-time analytical solutions.

Photo by Jason Briscoe on Unsplash

7. Summary

Both C++ and Python are extremely popular languages and I highly encourage everyone to learn both.

This article aimed to highlight the similarities and differences between the two languages so that we can understand when to use one over the other.

Let me know if you consider another feature of Python or C++ that requires mentioning.

Thanks for reading.

FinTechExplained

This blog aims to bridge the gap between technologists, mathematicians and financial experts and helps them understand how fundamental concepts work within each field. Articles

Farhad Malik

Written by

My personal blog, aiming to explain complex mathematical, financial and technological concepts in simple terms. Contact: FarhadMalik84@googlemail.com

FinTechExplained

This blog aims to bridge the gap between technologists, mathematicians and financial experts and helps them understand how fundamental concepts work within each field. Articles

More From Medium

More from FinTechExplained

More from FinTechExplained

More from FinTechExplained

More from FinTechExplained

What Is Brownian Motion?

109

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade