Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is Python written in

The Building Blocks of Python

When you're just starting out in the world of programming, it's natural to wonder about the origins of the tools you're using. Python, with its readable syntax and wide-ranging applicability, is a favorite among beginners. But what is Python itself made of? What language was used to create Python?

Python is written in "C". This might be a bit confusing at first—how can a programming language be written in another language? Think of it like this: just as you might write a script for a play in English to be performed by actors, Python was written in C to be 'performed' by computers.

C: The Mother Tongue of Python

C is a high-performance programming language that has been around since the early 1970s. It's known for its efficiency and control, and it's often used to create software that's close to the hardware, like operating systems or game engines.

Why C?

C was chosen as the language to write Python for several reasons:

  • Speed: C is a very fast language because it is compiled. This means that before the code is run, it is transformed into a language that the computer's processor can understand directly.
  • Control: C gives programmers a lot of control over how the computer's memory is managed. This is important for a programming language like Python, which aims to be efficient and fast.
  • Portability: Code written in C can be run on many different types of computers. This is why Python, which is written in C, can be used on Windows, macOS, Linux, and many other operating systems.

How Python Uses C

Python is often referred to as an "interpreted" language. This means that when you write Python code, it is not directly converted into computer-readable instructions. Instead, another program reads the Python code and carries out the instructions. This program is called the Python interpreter, and it is written in C.

The Python Interpreter

The Python interpreter is like a translator. It takes the Python code you write and translates it into something the computer can understand. Because it's written in C, it can do this very quickly and efficiently.

Here's an analogy: imagine you're at a United Nations meeting, and you only speak English. You're given a speech in French, a language you don't understand. Luckily, there's an interpreter there who can translate the French speech into English for you. The Python interpreter does something similar—it translates Python code into 'computer language'.

A Peek into Python's C Code

Now, let's look at a simple example. Consider the built-in Python function len(), which is used to get the length of an object, like a list or a string. The Python code might look like this:

my_list = [1, 2, 3, 4, 5]

This would output 5 because there are five items in my_list. But how does the len() function work under the hood? It's actually a wrapper around a C function that does the real work.

The C code that implements this function is far more complex than the Python code and involves interacting with the core structures of Python, which are also written in C. But you don't need to know this C code to use len() in Python. That's the beauty of Python—it simplifies many of the complexities of programming.

The Benefits of Python Being Written in C

Being written in C bestows Python with several advantages:

  • Extensibility: If you ever find that Python doesn't have a feature you need, you can extend it with C. This means you can write your own C code that Python can use.
  • Libraries: Many Python libraries, especially those that need to be fast, like NumPy for numerical computations, are also written in C. This allows them to run quickly while still being accessible from Python's simpler syntax.
  • Embeddable: Just as C can extend Python, Python can be embedded within C programs. This can be useful if you need to add some scripting capabilities to a C application.

The Intuition Behind Python and C's Relationship

To help you understand the relationship between Python and C, consider the following analogy: Imagine Python as a sleek, modern car that's easy to drive, even if you're new to driving. Underneath the hood, C is the powerful engine that makes the car go. You don't need to know how to build or repair the engine to drive the car, but it's reassuring to know that there's a robust and efficient mechanism powering your journey.

Conclusion: The Symphony of Languages

In the orchestra of computer programming, Python is like the conductor—visible, leading the performance, and easy to follow. C, on the other hand, is like the first violin—less conspicuous but vital, playing the complex notes that underpin the symphony. As a beginner, you're invited to direct the orchestra using Python's baton, creating beautiful music without needing to master the intricate techniques of the first violin.

Understanding that Python is written in C is like a peek behind the curtain of the programming world. It's a testament to how languages can build upon one another, creating tools that are both powerful and accessible. As you continue your programming journey, remember that each line of Python code you write is backed by decades of computing history and innovation, all made possible by the foundational language of C.