Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

what language is Python written in

Understanding Programming Languages

Before we dive into the language that Python is written in, it's important to understand what we mean by a programming language being "written in" another language. Imagine you want to build a house. You could build your house out of wood, but where does the wood come from? It comes from trees, which can be thought of as the raw material. In the world of programming, we also build things (software) using raw materials (programming languages), and sometimes we use tools (other software) that are made out of these raw materials as well.

The Core of Python: CPython

Python, the programming language, is not just a standalone entity. It is actually a specification, and this specification can be implemented in various ways. The most common implementation of Python is called CPython, and as the name suggests, it is written in the C programming language.

So, what does it mean for Python to be written in C? It means that the tools and mechanisms that interpret and run your Python code are actually a bunch of C code working behind the scenes. C is a lower-level language compared to Python, which means it is closer to machine language (the language that computers understand directly) and it allows programmers to control exactly how the computer will manage memory and hardware.

C: The Building Block

C is often referred to as a "building block" language because it is simple, yet powerful and versatile. It's like the Lego bricks of the programming world. You can create almost anything with it, from small programs to entire operating systems. This is why C is a popular choice for implementing programming languages - it's like using a strong foundation to build a house that can withstand any storm.

Python's Interpreter: An Analogy

Think of Python's interpreter as a translator. You speak to the translator in English (Python code), and the translator then speaks to the local (the computer) in their native tongue (binary code). The translator is fluent in both languages and can convert your requests (code) into actions (programs running) that the local understands.

In this analogy, the translator was taught the local language by someone who speaks C. The rules and grammar that the translator uses to understand and speak the local language were written in C. This is similar to how CPython uses C code to interpret and execute Python code.

Code Examples: Python vs. C

To give you a sense of the difference between Python and C, let's look at a simple example: printing "Hello, World!" to the screen.

In Python, you would write:

print("Hello, World!")

In C, the same operation would look like:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

You can see that Python is more straightforward and requires less code. This simplicity is one of the reasons why Python is so approachable for beginners.

Why Is Python Written in C?

Python is written in C for several reasons. C provides a balance between programming ease and control over the system. It allows the developers of Python to create a language that is both powerful and easy to write in, without sacrificing speed or the ability to interact with the system at a low level.

Python and C: Working Together

When you write Python code, you are writing instructions that the Python interpreter, which is written in C, will follow. This means that your Python code indirectly controls what the C code does. It's a bit like puppetry; your Python code is the puppeteer, and the C code is the puppet, performing actions on your behalf.

Extending Python with C

One of the great things about Python being implemented in C is that you can extend Python with C. This means if you need to do something that requires the speed or low-level access of C, you can write a C module that can be called from Python. This is like having a special tool that you can use alongside your regular tools when you need it.

Performance Considerations

While Python is known for its ease of use and readability, it is not always the fastest language due to its high-level nature. Since C is closer to the machine level, it can execute tasks much faster. When Python is implemented in C, it benefits from this speed, making Python programs run more efficiently than if they were written in a higher-level language alone.

The Evolution of Python Implementations

CPython is not the only way to run Python code. There are other implementations, such as PyPy, Jython, and IronPython, which are written in different languages and have different goals. PyPy, for example, is an implementation of Python written in a language called RPython, which aims to be faster than CPython.

Conclusion: The Symphony of Languages

In the grand orchestra of programming, Python is like the violin - versatile and popular with a clear sound that is easy to pick up for beginners. C, on the other hand, is like the double bass - foundational and powerful, providing the essential undertones that support the entire ensemble. Together, they create harmonious music, with Python offering the melody and C supporting it with depth and structure.

As a beginner, understanding that Python is written in C is like peeking behind the curtain at a magic show. It demystifies the process and shows you that, at the end of the day, all the wonders of programming are created by combining simple, well-understood elements in complex ways. And just like a magician learning to perform their first trick, as you learn to program, you begin to join the ranks of those who create magic with code.