Accueil Blog Projets CV Contact

Cet article n'est pas encore disponible en français. Vous lisez la version anglaise.

Python Key Characteristics and How Your Code Runs

Sidali Assoul Sidali Assoul 4 min de lecture

Dernière mise à jour le

Introduction

Before starting to write any Python code, it’s useful to understand what makes a language different from others, and what happens under the hood when you actually execute your code.

In this article we will look at Python’s key characteristics which are: interpreted, dynamically typed, garbage collected, multi-paradigm, and batteries-included. And we will explain what’s exactly happening when you run python3 hello.py under the hood.

If that sounds interesting, let’s dive in.

What is Python

Python is a general purpose, interpreted programming language created by Guido Van Rossum in 1991.

Python's author Guido van Rossum

It can run in any operating system, and it’s one of the most popular programming languages, mainly used for for machine learning, data science, automations, web backends, AI…

Key Characteristics

When thinking about Python in general you have to have this mental image in mind.

Python's Key Characteristics

Python has 5 main characteristics that distinguish it

  1. Interpreted: Code runs line-by-line through the Python interpreter, no compilation step is needed before running the program.

  2. Dynamically typed: Since no compilation step is happening, variable types are resolved at runtime dynamically, so there is no need to declare them upfront, in contrast with other statically typed languages like C, C++ or Golang.

  3. Garbage Collected: Python’s garbage collector manages the memory automatically for you, for example, when you declare a variable storing a list, you don’t have to think about freing it when it’s no longer used, Python’s garbage collector does that automatically for you.

  4. Multi-paradigm: Python supports many programming styles such as procedural, functional (FP), object orriented programming (OOP)…

  5. Batteries included: Python ships with a large standard library, it includes anything that you may need from basic math utils to advanced collections such as Minimum heaps and queues.

How Your Code runs

How Your Python Code Runs

When you run:

python3 hello.py # or python hello.py

The Python’s interpreter first reads and parses your code.

And then it compiles it to bytecode which is a compact, fast and optimized binary format of your code ,

After that, the interpreter executes it on the Cpython virtual machine (VM).

In order to optimize future runs of your scripts, the interpreter may save the bytecode of some parts of the script as “.pyc” binary files within a directory named “pycache”.

By default it saves the bytecode only for complex scripts that import multiple modules.

To enforce the compilation of our hello.py script run:

python3 -m py_compile hello.py # or python -m py_compile hello.py 

-m Python’s CLI argument stands for module, here we are running the “hello.py” script using the pre-installed “py_compile” Python module.

Force Python to compile a basic hello.py script.

Summary

Python’s 5 Key Characteristics

  • Interpreted: code is executed by the Python interpreter.
  • Dynamically typed: variable types are determined at runtime.
  • Garbage collected: memory is managed automatically.
  • Multi-paradigm: supports procedural, object-oriented, and functional programming.
  • Batteries included: comes with a rich standard library.

How Python runs your code

How Your Python Code Runs

How to Force a script to compile to bytecode (.pyc file)

python3 -m py_compile hello.py

Key Notes for Interviews

  • Python does compile your code to machine code as other languages like C or C++, it only compiles into an optimized format called bytecode and runs it right away line by line, while caching it for latter uses in .pyc files stored within the __pycache__ directory.

  • In brief, the Python interpreter is responsible for parsing your code, compiling it to bytecode, and executing it on the CPython Virtual Machine (VM).

  • Python’s dynamic typing and garbage collection make it easier to write code without manually managing types or memory.

Conclusion

Now you know what makes Python unique in the programming languages realm, and how your code is executed under the hood by the interpreter, we explain what’s happening starting from the source code parsing, and converting to bytecode, then running it on the CPython virtual machine.

In the next chapter, we’ll start writing Python code, and we will learn about variables and data types, the building blocks of every program and application out there.

Continue to Variables and Data Types

Cet article vous a plu ?

Je suis ouvert à de nouveaux postes, en télétravail ou à l'international. Si quelque chose vous a parlé ou si vous souhaitez collaborer, j'adorerais en discuter.

Se connecter sur LinkedIn
Sidali Assoul

Écrit par

Sidali Assoul

Ingénieur logiciel avec 5 ans d'expérience en développement web et mobile full stack, un bilan éprouvé en data science, recherche et solutions IA modernes, avec une approche orientée business pour livrer des produits à forte valeur ajoutée.