Python vs C++: What should a beginner choose?
If you’re new to programming, are a computer teacher, or just a parent planning to get his child started with programming – you must be wondering where to start. C++, C, Java, Python – there are just too many options out there! While all these languages are great in their own right and popular, the debate between C++ and python has been intense for sometime.
Before comparing them, let’s look at each language separately.
A general purpose, high level programming language, Python, was created by Guido van Rossum in 1991. The underlying objective to create python was simplicity. Till date, it ranks as one of the simplest languages to learn – in fact it is pretty close to plain, everyday English. Features like indentation make the code readable and beautiful (while this has also been a reason for the backlash levelled at it), and library functions make it extremely powerful, especially for beginners.
While python is definitely the easier of the two to learn, it also features as a top language in IEEE’s 2018 Top Programming Languages
. Suffices to say that its popularity and ease work hand in hand to make python a very powerful and desirable language in the industry.
Introduced by Bjarne Stroustrup in 1979, C++ is also a high level, general purpose programming language. It is basically an upgrade to C, initially known as ‘C with classes’. This was the introduction to Object Oriented Programming in C.
Compared to Python, C++ is a rather tough language to learn. Nonetheless, despite posing learning difficulties, C++ is an extremely flexible language which lends the programmer considerable power over everything in the computer – even memory allocation, something impossible to control in python.
While both Python and C++ are object oriented, encapsulation – binding of data and functions as one unit – is not available in Python.
Comparing Python and C++
Let us compare the two languages based on a number of generic industry metrics:
Ease of learning
One of the major deciding factors for a beginner would be ease of learning. If the language is too hard, it may even throw the programmer off the track and kill his aspirations of getting better at it.
Python is doubtlessly closer to English and hence easier to learn. C++, on the other hand, is very close to the CPU and deals with memory allocation, following which, if as a beginner, you are not careful, you may end up destroying your system with the wrong C++ program.
To give you a picture of its simplicity, Python has zero compile steps. If your file is saved as myprogram.py, all you have to do to run it is type python myprogram.py.
In this context, reading the Zen of Python gives clarity on the intentions and philosophy of the language. A few lines in the Zen goes as follows:
- Beautiful is better than ugly.
- Sparse is better than dense.
- If the implementation is hard to explain, it’s a bad idea.
C++ has no such documentation or clarity of thought. What it does have is a huge community of supporters working day and night to help solve problems.
Speed matters when working with an industry level software – particularly if the software is a real time, time sensitive entity. The faster a language is, the more efficient it is considered to be. Here, C++ wins the game. It is many times faster than Python
, the reason being Python itself is written in C (the predecessor of C++).
Since our debate is strictly regarding what a beginner should learn, speed does not really matter at the beginner stage.
In C++, you have to allocate memory to new variables and also free them when their job is over. If you do not do so, it might result in memory leak. Since C++ does not provide garbage collection and uses pointers, memory leak is inevitable at some points.
There are, however, no such rules in Python. Python is written to adapt and allocate memory on its own – all hail dynamic memory allocation! If you’re an experienced programmer, you might need to work on projects where you need control over memory allocation. As a beginner, however, you need not mess around with this aspect.
C++ has a lot of syntax to get a grasp on. However, it does not have indentation rules, this makes the code look like garbage at some point. Python, resembling more of English, its indentation helps a coder keep track of every bracket opened. This can be pretty inviting to a beginner and have coding look like a work of art at times!
To explore perspective on the ‘readability’ aspect, let us look at two code snippets giving the same output.
std::cout << "Hello World!\n";
Both these snippets give the same output, but if you are a beginner trying to comprehend the C++ codes likely scared you even as you could make complete sense of the Python code. That’s the magic of Python!
While we do not claim that either one of the two is a ‘better’ language, Python is undeniably easier to learn. However, coming back to the popularity index, we must not forget that popularity means relevance.
is any good and Python is one of the most popular languages out there, chances are the industry will soon adopt it. This makes python a safe language to go with. Furthermore, if you are planning to go into Machine Learning, Data Sciences or Artificial Intelligence, Python web development is the go to language
. An absolute winner, it is the safest for beginners.
As for the unsure souls out there, check out Cython – the power of Python with the elements of C++. Fun, eh?
Subscribe to get your daily round-up of top tech stories!