What is Python and why you should use it?
Python is an interpreted, object-oriented, high-level programming language created by Guido Van Rossum in 1991.
Interpreted language
Python is interpreted because it does not require a compiler to excecute a program. In fact, before executing a program, the Python source code is transformed into a bytecode.
A bytecode is representation of the Python source code in a lower-level, platform-independent format that is produced by the Python interpreter.
For your information, byte codes are those files with the .pyc extension.
Moreover, bytecode is generated for every Python file that is imported into the Python environment, whether it is a module or a standalone script. When a Python file is imported, the Python interpreter reads the source code, compiles it into bytecode, and stores the bytecode in a .pyc file. This .pyc file is then used for subsequent imports of the same file, to avoid the overhead of recompiling the source code each time it is imported.
These .pyc files are stored in the pycache directory, which is located in the same directory as the source code.
The bytecode uses a language called intermediate language.
The Intermediate Language (IL) is a language generated from the source code but which cannot directly be executed by the CPU.
The use of an IL is to provide a neutral representation of the source code that is independent of the target platform (platform-independent) allowing for further processing, optimization (reordering instructions, removing redundant instructions).
One thing to bear in mind is that Intermediate Language is lower level than the source code but higher level than machine code.
Once the bytecode is generated by Python, it is then executed by the Python interpreter, which reads the code, interprets its meaning, and carries out the instructions it contains.
Note: If a bytecode already exists, it is not generated during the execution phase. In fact, Python generates a bytecode only if it does not already exist or if the source code has been modified more recently than the existing .pyc file. If a .pyc file exists and is up-to-date, the Python interpreter will use that file instead of generating new bytecode from the source code.
The goal of this behavior is to improve the startup time of Python programs.
In addition, if a .pyc file exists and is up-to-date, the Python interpreter will use the bytecode from that file, even if the source code is not available. This allows for distribution of Python programs without the need to distribute the original source code, providing some level of protection for the source code.
To excecute a bytcode file, you can proceed as follows using the python3.11 interpreter:
python3.11 <filename>.pyc
Alternatively, you can make the .pyc file executable and run it directly, just like a regular executable, by using the following command:
./<filename>.pyc
Object-Oriented Programming (OOP)
Python uses multiple programming paradigms like procedural paradigm, functional paradigm and object-oriented paradigm.
A paradigm is a style or approach to solving problems using computer programming. It provides a set of guidelines and principles that dictate how you structure and organize your code.
In this section, we will be focusing on OOP.
Object-Oriented Programming is a programming paradigm that is based on the concept of “objects”. An object is composed of attributes and methods.
Some principles of OOP languages are:
- Encapsulation
- Inheritance
- Polymorphism
High-level
Python is a high-level language because it uses a syntax closer to human language and it provides higher-level constructs, such as variables, data structures, functions, and control structures, which make it easier to write complex programs.
Python domain applications
Python is an ease of use programming language very simple to learn and understand.
It is utilized in numerous domains because of its multiple advantages listed above. For instance, it is used in:
Cybersecurity: Python permits to cybersecurity workers to develop their own hacking tools (scapy, requests, bs4, hashlib, socket, mysql, threading)
Machine Learning: create ML models (keras, numpy, maplotlib, scikit-learn).
Web development: create websites (django, flask).
Let’s recap!
Python is a powerful programming language used in various domains of IT.
It allows developers to simply carry out complex tasks thanks to its multiple packages and its community members which are very active and help each other resolving programming issues using platforms like stackoverflow for instance.
Python is a high-level and multiple paradigm programming language.
Before executing the python code, Python transforms the source code into byte code.