Insert Python Icon

Python Review Topics

Exam block #1: Basic Concepts (17%)

Fundamental Concepts

[SOURCE]

When you write a program in C/C++, you have to compile it. Compilation involves translating your human understandble code to machine understandable code, or Machine Code. Machine code is the base level form of instructions that can be directly executed by the CPU. Upon succesfull compilation, your code generates an executable file.

For most part, Python is an interpreted language and not a compiled one. Although compilation is a step. Python code, written in a .py file is first compiled to what is called a bytecode which is stored with a .pyc or .pyo format. Instead of of translating code to machine code, Python translates it to bytecode. This bytecode is a low-level set of instructions that can be executed by an interpreter. Most PC's has their own python interpreter located at the /usr/local/bin/python3.8 address and you will usualy find a different numerical depending on your installed python version.

Why Interpreted

One popular advantage of interpreted languages is that they are platform-independent. As long as the Python bytecode and the Virtual Machine have the same version, Python bytecode can be executed on any platform.

Dynamic typing is another advantage. In static-typed languages like C++, you have to declare the variable type and any discrepancy like adding a string and an integer is checked during compile time. In strongly typed languages like Python, it is the job of the interpreter to check the validity of the variable types and operations performed.

Disadvantages of Interpreted languages

Dynamic typing provides a lot of freedom, but simultaneously it makes your code risky and sometimes difficult to debug. Python is often accused of being ‘slow’. Now while the term is relative and argued a lot, the reason for being slow is because the interpreter has to do extra work to have the bytecode instruction translated into a form that can be executed on the machine. A StackOverflow post makes it easier to understand using an analogy

If you can talk in your native language to someone, that would generally work faster than having an interpreter having to translate your language into some other language for the listener to understand.

Stackoverflow comment by Lasse V. Karlsen

What exactly is Garbage Collection?

In older programming languages, memory allocation was quite manual. Many times when you use variables that are no longer in use or referenced anywhere else in the program, they need to be cleaned from the memory. Garbage Collector does that for you. It automatically frees up space without you doing anything. The memory management works in two ways —

In a simplified way, it keeps track of the number of references to an object. When that number goes down to zero, it deletes that object. This is called reference counting. This cannot be disabled in Python.

In cases where object references itself or two objects refer each other, a process called generation garbage collection helps. This is something traditional reference counting cannot take care of.