Python Programming
SY EXTC
Python is a high level language
Programming languages like C, Pascal or FORTRAN concentrate more on the functional aspects of programming.
In these languages, there will be more focus on writing the code using functions.
For example, we can imagine a C program as a combination of several functions.
Object Oriented Programming languages like Java and .NET where programming is done through classes and objects.
In Java, a programmer should express his logic through classes and objects only. It is not possible to write a program
without writing at least one class! This makes programming lengthy.
Nowadays, programmers want C style coding as well as the Java style object orientation.
When they want to develop functional aspects like calculations or processing, they want to use C style coding and when
they are in need of going for classes and objects, they will use Java style coding.
The only answer for their requirement is Python!
Python is a programming language that combines the features of C and Java.
It offers elegant style of developing programs like C. When the programmers want to go for object orientation, Python
offers classes and objects like Java.
In Python, the program to add two numbers will be as follows:
The preceding code is easy to understand and develop.
Python was developed by Guido Van Rossum in the year 1991 at the Center for Mathematics and Computer Science
managed by the Dutch Government.
Van Rossum picked the name Python for the new language from the TV show, Monty Python’s Flying Circus.
1991.
The logo of Python shows two intertwined snakes as shown in Figure 1.1:
Python is open source software.
Features of Python
There are various reasons why Python is gaining good popularity in the programming
community. The following are some of the important features of Python:
Object oriented languages like Python, Java and .NET use the concepts of classes and objects in their programs.
Since class does not exist physically, there will not be any memory allocated when the class is created. But, object exists
physically and hence, a separate block of memory is allocated when an object is created.
In Python language, everything like variables, lists, functions, arrays etc. are treated as objects.
Execution of a Python Program
• Let’s assume that we write a Python program with the name [Link].
• Program, the next step is to compile the program using Python compiler. The compiler converts the Python
program into another code called byte code.
• Byte code represents a fixed set of instructions that represents all operations like arithmetic operations,
comparison operations, memory related operations, etc.,
• It means the byte instructions are system independent or platform independent. The size of each byte code
instruction is 1 byte and hence they are called with the name byte code. These byte code instructions are
contained in the file [Link]. Here, the [Link] file represents a python compiled file.
• PVM (Python Virtual Machine) uses an interpreter which understands the byte code and executes it.
• PVM uses an interpreter which understands the byte code and converts it into machine code.
• PVM first understands the processor and operating system in our computer. Then it converts the byte code
into machine code understandable to that processor and into that format understandable to that operating
system. These machine code instructions are then executed by the processor and results are displayed.
Normally, when we compile a Python program, we cannot see the .pyc file produced by the Python compiler and the
machine code generated by the PVM. This is done internally.
Viewing the Byte Code
• Flavors of Python refer to the different types of Python compilers.
• These flavors are useful to integrate various programming languages into Python. The following are some of
them:
Hot portions of the
code: functions
that are called
many times, loops
etc.
A module represents Python code that performs a specific task. Garbage collector is a module in Python that is
useful to delete objects from memory which are not used in the program.
The module that represents the garbage collector is named as gc.
Garbage collector in the simplest way to maintain a count for each object regarding how many times that
object is referenced (or used). When an object is referenced twice, its reference count will be 2.
When an object has some count, it is being used in the program and hence garbage collector will not remove it
from memory.
When an object is found with a reference count 0, garbage collector will understand that the object is not used
by the program and hence it can be deleted from memory. Hence, the memory allocated for that object is
deallocated or freed.
Garbage collector can detect reference cycles. A reference cycle is a cycle of references pointing to the first
object from last object.
Even if the objects A, B and C are no longer used in the Python program, still these objects contain 1 reference to each
one. Since the reference count for each object is 1, the garbage collector will not remove these objects from memory.
These objects stay in memory even after the program execution completes. To get around this, garbage collector uses
an algorithm (logic) for detecting reference cycles and removing objects in the cycle.
Garbage collector classifies the objects into three generations. The newly created objects are considered as generation
0 objects.
First time, when the garbage collector examines the objects in memory and does not remove an object from memory
due to the reason that the object is used by the program, then that object is placed into next generation, say
generation 1.
When the garbage collector intends to delete the objects for the second time and the object also survives for the
second time, then it is placed into generation 2.
Thus, older objects belong to generation 2. Garbage collector tries to delete younger objects which are not referenced
in the program rather than the old objects.
Garbage collector runs automatically. Python schedules garbage collector depending upon a number called
threshold. This number represents the frequency of how many times the garbage collector removed (or collected)
the objects. When the number of allocations minus the number of de-allocations is greater than the threshold
number, the garbage collector will run automatically.
When more and more objects are created and if the system runs out of memory, then the automatic garbage
collector will not run. Instead, the Python program will throw exception (runtime error). When the programmer
is sure that his program does not contain any reference cycles, then automatic garbage collector is best suitable.
In some cases, where reference cycles are found in the program, it is better to run the garbage collector manually.