Trailing Whitespace

Python vs C++

Python and C++ are both programming languages. As such, they can be made to solve nearly any problem you can think of. Furthermore, Python and C++ come from similar camps of thought about how programs should be written; that is, they are both support object-oriented design and encourage imperative algorithms. If these terms do not mean much to you, the only thing you need to understand is that a program written in Python can quite easily be translated to an equivalent program in C++.

While the essential parts of programming (that is, the things you learn in an introductory programming class) remain the mostly the same, there are some parts of each language that make it more suited to different tasks.

Interpreter (Python) vs. Compiler (C++)

To run a program in C++, one runs commands similar to

gcc program.cpp
./a.out

The gcc command is a program that reads a C++ file and produces a format more native to the computer’s CPU. In Python, one does not compile programs; instead one runs them using

python program.py

The cool (and scary) thing about this is that python may not look at all of your program before beginning to run it. Your program may have many errors in it, but the Python interpreter will not complain until it reaches that portion of your program.

This difference makes Python slightly more convenient because there is only one command to run, and it begins almost instantaneously; in C++ the compilation step is an extra command, and it can a long time for large programs.

Because Python uses an interpreter rather than a compiler, one can also run individual lines. For example, the following is a paste from some commands that I ran on my computer.

/home/philip λ>python
Python 2.7.16 (default, Mar  2 2019, 18:34:01)
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5
5
>>> 5 + 6
11
>>> 12 + 98
110
>>> x = 89
>>> y = 34
>>> x + y
123
>>>                                                       ``

This is called a REPL (Read-Evaluate-Print-Loop); I can type a python statement, press enter, and get the result printed. This makes Python good for getting immediate feedback about whether the function you have written does what you think it does.

Dynamic types (Python) vs. Static types (C++)

The “type” of a variable roughly refers to the kind of things it can contain. In C++, one will often declare int variables, or perhaps string or float. By telling the compiler what kind of values you expect to put in the variable, you can be assured that you will not accidentally set the variable myAge to the string "56"; only once you have used a convert_string_to_integer function will the compiler comply with your wishes.

In contrast to C++, Python performs no checks on what kinds of things you put in a variable. You may notice that the Python statement x = 89 had not type information. Python is happy to let me assign a string to x right after I assigned a number to x.

>>> x = 1
>>> x
1
>>> x = "hello world"
>>> x
'hello world'
>>>

This difference means that Python is much more forgiving when you try to run your program, but it will also do almost nothing to prevent you from making type mismatch mistakes. It is not very often that you want to assign to kinds of things to the same variable, so this is not a useful feature.

Lots of libraries (Python) vs. Lots of libraries (C++)

Both C++ and Python have a lot of code that has already been written to do a lot of common things. The difference between them is that Python libraries are often much easier to install, and also much easier to use correctly.

Garbage Collection (Python) vs. Manual memory management (C++)

In C++, one deals directly with pointers. To create a pointer, one can call malloc, and to get rid of a pointer, one can call free. A lot of bugs happen when someone creates a pointer, but forgets to call free on it, or if someone calls free on a pointer and then tries to keep using. Python removes this concern entirely from the programmers mind; one does not need to worry about allocating and freeing memory. Python will keep track of which objects you might still be using, and which ones you can’t possible use again (and this it can safely delete them).

This difference means that Python programmers do not have to worry about the biggest source of bugs in C++ programs, which means they can often write correct programs much quicker.

What makes one language better than another?

This is a hotly debated question. Here are some questions one might ask about language:

  1. Can I write programs that run fast? (C++ wins)
  2. Can I write programs quickly? (Python wins)
  3. Will the language prevent mistakes? (Both C++ and Python lose)
  4. Is it easy to work on big projects (> 30,000 lines of code)? (Both C++ and Python lose)

In my opinion, both C++ and Python are rather bad languages because they do not provide much help when the programmer makes mistakes, which also means they are more difficult to build big projects with.

Python is really good for writing proof-of-concept programs really quickly. In addition, Python has a ton of libraries available for analysing data; Python itself isn’t necessarily the most perfect language for data science, but it is pretty good, and the libraries that already exist make it one of the best options.

C++ is really good for making fast programs. That’s all, really.