Rapid Intro To Python: Part 1

Rapid Intro To Python: Part 1

About The Language, How It Works & Why You Will Need It

Python is a language that holds a special place in my heart due to its intuitive syntax and vibrant developer community. Its ease of use has greatly simplified my work, making it the most preferred language I have used to date. I find great joy in working with Python.

So, let's talk about Python.

What exactly is python?

According to Wikipedia:

Python is a high-level, general-purpose programming language... It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming.

Python is a versatile high-level programming language known for its human-readable syntax and wide range of applications. It is often considered the "all-in-one" package of programming languages, as it allows developers to increase productivity while reducing debugging time. Its syntax, which resembles English in many cases, makes it easy to read and understand for both beginners and experienced programmers alike.

Development of Python began in the late 1980s by Guido Van Rossum, and the language has seen ongoing updates and releases. The latest version, Python 3.11.0, was released in 2022 and the next release, version 3.12, is planned for 2023.

Nuts & bolts

Python is not intelligible to the computer because the computer only understands binary code (1s and 0s) and can only perform tasks specified to it in binary code. Now, for the computer to understand what we're saying in Python, the Python interpreter needs to go through a set of steps to convert the Python code we wrote, into binary code that the machine understands. This process is known as interpretation.

The following is a piece of Python code that is intended as an example. If you are new to the language, some parts of the code may be unclear. However, as we progress through the material (in subsequent blog posts), the concepts will become more familiar to you.

# A simple countdown program.
for i in range(1, 11):
    print(i)

Alright, let's go through all the steps taken by the python interpreter to interpret our code into binary code:

Lexical Analysis

At this stage, the interpreter assesses the syntax of the provided code, checking for proper indentation and other grammatical requirements. If any errors are found, the interpreter returns an error message detailing the issue. Otherwise, the code is broken down into smaller components, called tokens, before moving on to the next stage of interpretation.

Bytecode Generation

During this stage, the interpreter takes the processed tokens and converts them into platform-independent bytecode. These bytecodes are then used by the Python Virtual Machine (PVM) to generate machine-specific code. Once our example code has completed this stage, it is transformed into the following set of bytecodes:

              0 LOAD_NAME                0 (range)
              2 LOAD_CONST               0 (1)
              4 LOAD_CONST               1 (11)
              6 CALL_FUNCTION            2
              8 GET_ITER
        >>   10 FOR_ITER                 6 (to 24)
             12 STORE_NAME               1 (i)
             14 LOAD_NAME                2 (print)
             16 LOAD_NAME                1 (i)
             18 CALL_FUNCTION            1
             20 POP_TOP
             22 JUMP_ABSOLUTE            5 (to 10)
        >>   24 LOAD_CONST               2 (None)
             26 RETURN_VALUE

Don't bother trying to understand this, you're not required to do so to be a good Python programmer. You can use the dis module to disassemble Python code into bytecodes if that's something you'd like to do. Alright, let's talk about what happens in the next stage of this process. But if you're feeling curious to learn how bytecode works, then feel free to check out this article

Python Virtual Machine

In this stage, the Python interpreter initiates the Python Virtual Machine (PVM) and passes the generated bytecode to it. The PVM processes the bytecode into machine-specific binary code, tailored to the architecture of the device on which the interpreter is running, such as Windows or Linux. The binary code is then executed by the machine, providing the intended output, assuming there are no bugs in the code. In our example, the output would appear as follows:

1
2
3
4
5
6
7
8
9
10

Alright, let's talk about the areas where you'd most likely make use of Python in the real world.

Uses

Python, as a general-purpose programming language, is widely used in a variety of fields. It is known for its versatility and can be used in various industries, from finance to web development. Some might even argue that Python can be used in everything. Below are a few examples of where Python is commonly used:

  • Game Development using packages and libraries like Pygame, Pyglet, PANDA3D etc.

  • Web Development using packages and frameworks like Flask, Django etc.

  • Mobile Application Development using Kivy as your tool of choice

  • Data Visualization using tools like Matplotlib, Plotly etc.

  • Artificial Intelligence and Machine Learning using tools like Tensorflow, Scikit learn etc.

and the list goes on and on.

Conclusion

Throughout this article, we have delved into the basics of Python, its inner workings, and its various applications. It is my sincere hope that I have convinced you of the value of Python, whether you are just beginning your programming journey or are an experienced developer. If so, I invite you to join me in further exploration of this powerful language by reading my next article. I assure you, it will be an enjoyable and informative experience.

If you would love to learn more about Python, then read this article by the folks at Freecodecamp.

If you found my explanation informative and enjoyable, please consider following me on this platform and sharing this article with anyone who may find it beneficial. Your support is greatly appreciated.
Thanks for reading up to this point, I'll see you at the next one. Till then;

Keep Learning

Keep Growing