Back to Python
Python

Python

14 of 30 Completed

Python as a Programming Language

Python’s biggest strength is that Python code is very human-readable and easy to learn. To demonstrate, a simple program finds the position of "el" in "Hello world" in C++ would look like:

#include <stdio.h>
#include <string.h>

int main () {
   char *haystack = "Hello world";
   char *needle = "el";
   char *ret;
   int index;

   ret = strstr(haystack, needle);

   index = ret - haystack;

   return(0);
}

The following line does the same in Python:

def main():
    return "Hello world".index("el")

The good thing is that Python still provides many of the benefits of more traditional programming languages, such as the inclusion of object-oriented programming and automatic memory allocation while maintaining highly readable code.

Flaws of Python as a programming language

So what’s the catch? Why doesn’t everyone use Python, not just data scientists?

Python’s biggest flaw is that it extremely “high-level.” We saw the contrast between C++ and Python in the last section in terms of readability, but C++ is still a high-level language. Low-level languages, in contrast, look like this:

.LC0:
        .string "Hello world"
.LC1:
        .string "el"
main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 32
        mov     QWORD PTR [rbp-8], OFFSET FLAT:.LC0
        mov     QWORD PTR [rbp-16], OFFSET FLAT:.LC1
        mov     rdx, QWORD PTR [rbp-16]
        mov     rax, QWORD PTR [rbp-8]
        mov     rsi, rdx
        mov     rdi, rax
        call    strstr
        mov     QWORD PTR [rbp-24], rax
        mov     rax, QWORD PTR [rbp-24]
        sub     rax, QWORD PTR [rbp-8]
        mov     DWORD PTR [rbp-28], eax
        mov     eax, 0
        leave

In case you’re curious, this code just does the same thing as the C++ and Python code.

However, there is an important advantage to using a low-level language. Just as a how humans cannot easily read low-level languages, computers can not easily read high-level languages. There is a separate program, called an “interpreter”, which allows translation between high-level and low-level code. If you code in any high-level language, your code must go through an interpreter for your computer to run it. This adds an extra step to the process of running code, and thus increases computation time, sometimes by a significant margin.

Python does have a trick up its sleeve, however. Python can directly transfer inputs to C and run functions from the language. C code is much less high-level than Python code, so interpreters can translate C code much faster. This allows Python to have code that runs partially in C, which allows for much faster performance in a variety of tasks, not least of all statistical analysis.

Still, for programs that require very fast computation speed (think millions of calculations every millisecond), like weather modeling, 3D video games, and virtual reality, Python is extremely slow.

Good job, keep it up!

46%

Completed

You have 16 sections remaining on this learning path.