Introduction to Python 13
Python, memory allocation and deallocation are done during runtime automatically. The
programmer need not allocate memory while creating objects or deallocate memory when
deleting the objects. Python’s PVM will take care of such issues.
Everything is considered as an object in Python. For example, strings are objects. Lists
are objects. Functions are objects. Even modules are also objects. For every object,
memory should be allocated. Memory manager inside the PVM allocates memory required
for objects created in a Python program. All these objects are stored on a separate
memory called heap. Heap is the memory which is allocated during runtime. The size of
the heap memory depends on the Random Access Memory (RAM) of our computer and it
can increase or decrease its size depending on the requirement of the program.
We know that the actual memory (RAM) for any program is allocated by the underlying
Operating system. On the top of the Operating system, a raw memory allocator oversees
whether enough memory is available to it for storing objects. On the top of the raw
memory allocator, there are several object-specific allocators operate on the same heap.
These memory allocators will implement different types of memory management policies
depending on the type of the objects. For example, an integer number should be stored in
memory in one way and a string should be stored in a different way. Similarly, when we
deal with tuples and dictionaries, they should be stored differently. These issues are
taken care of by object-specific memory allocators. Figure 1.5 shows the allocation of
memory by Python’s Virtual Machine:
Figure 1.5: Allocation of memory by Python’s Virtual Machine (PVM)
14 Chapter 1
Garbage Collection in Python
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. For example, take three objects A, B and C.
The object A refers to the object B whereas the object B holds a reference to the object C.
Now if the object C refers to the first object A, it will form a reference cycle. Figure 1.6
shows the reference cycle of three objects:
Figure1.6: A Reference Cycle of Three Objects
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
Introduction to Python 15
garbage collector will run automatically. One can know the threshold number by using
the method get_threshold() of gc module.
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. For this purpose, collect() method of gc module can be used.
Manual garbage collection can be done in two ways: time-based and event-based. If the
garbage collector is called in certain intervals of time, it is called time-based garbage
collection. If the garbage collector is called on the basis of an event, for example, when
the user disconnects from an application, it is called event-based garbage collection.
However, running the garbage collector too frequently will slow down the program
execution.
Comparisons between C and Python
In Table 1.1, we will compare some of the important features of C and Python languages:
Table 1.1: Differences between C and Python
C Python
C is procedure-oriented programming Python is object-oriented oriented language.
language. It does not contain the features It contains features like classes, objects,
like classes, objects, inheritance, inheritance, polymorphism, etc.
polymorphism, etc.
C programs execute faster. Python programs are slower compared to C.
PyPy flavor or Python programs run a bit
faster but still slower than C.
It is compulsory to declare the datatypes of Type declaration is not required in Python.
variables, arrays etc. in C.
C language type discipline is static and Python type discipline is dynamic and
weak. strong.
Pointers concept is available in C. Python does not use pointers.
C does not have exception handling facility Python handles exceptions and hence Python
and hence C programs are weak. programs are robust.
C has do… while, while and for loops. Python has while and for loops.
C has switch statement. Python does not have switch statement.
The variable in for loop does not increment The variable in the for loop increments
automatically. automatically.
16 Chapter 1
C Python
The programmer should allocate and Memory allocation and deallocation is done
deallocate memory using malloc(), calloc(), automatically by PVM.
realloc() or free() functions.
C does not contain a garbage collector. Automatic garbage collector is available in
Python.
C supports single and multi-dimensional Python supports only single dimensional
arrays. arrays. To work with multi-dimensional
arrays, we should use third party
applications like numpy.
The array index should be positive integer. Array index can be positive or negative
integer number. Negative index represents
locations from the end of the array.
Checking the location outside the allocation Python performs checking outside an array
of an array is not supported in C. for all iterations while looping.
Indentation of statements is not necessary Indentation is required to represent a block
in C. of statements.
A semicolon is used to terminate the New line indicates end of the statements and
statements in C and comma is used to semicolon is used as an expression
separate expressions. separator.
C supports in-line assignments. Python does not support in-line
assignments.
Comparisons between Java and Python
In Table 1.2, we will compare some of the important features of Java and Python
languages:
Table 1.2: Differences between Java and Python
Java Python
Java is object-oriented programming Python blends the functional programming
language. Functional programming features with object-oriented programming features.
are introduced into Java 8.0 through Lambdas are already available in Python.
lambda expressions.
Java programs are verbose. It means they Python programs are concise and compact. A
contain more number of lines. big program can be written using very less
number of lines.
It is compulsory to declare the datatypes of Type declaration is not required in Python.
variables, arrays etc. in Java.
Java language type discipline is static and Python type discipline is dynamic and
weak. strong.
Introduction to Python 17
Java Python
Java has do… while, while, for and for each Python has while and for loops.
loops.
Java has switch statement. Python does not have switch statement.
The variable in for loop does not increment The variable in the for loop increments
automatically. But in for each loop, it will automatically.
increment automatically.
Memory allocation and deal location is done Memory allocation and deal location is done
automatically by JVM (Java Virtual automatically by PVM (Python Virtual
Machine). Machine).
Java supports single and multi-dimensional Python supports only single dimensional
arrays. arrays. To work with multi-dimensional
arrays, we should use third party
applications like numpy.
The array index should be a positive integer. Array index can be positive or negative
integer number. Negative index represents
locations from the end of the array.
Checking the location outside the allocation Python performs checking outside an array
of an array is not supported in Java. for all iterations while looping.
Indentation of statements is not necessary Indentation is required to represent a block
in Java. of statements.
A semicolon is used to terminate the New line indicates end of the statements and
statements and comma is used to separate semicolon is used as an expression
expressions. separator.
In Java, the collection objects like Stack, Python collection objects like lists and
LinkedList or Vector store only objects but dictionaries can store objects of any type,
not primitive datatypes like integer including numbers and lists.
numbers.
Points to Remember
Python was developed by Guido Van Rossum in the year 1991.
Python is a high level programming language that contains features of functional
programming language like C and object oriented programming language like Java.
In object oriented terminology, an object represents a physical entity that contains
behavior. The behavior of an object is represented by attributes (or properties) and
actions. The attributes are represented by variables and actions are performed by
functions or methods.
In object oriented terminology, a class is an abstract idea which represents common
behavior of several objects. Class represents behavior and does not exist physically.
18 Chapter 1
Behavior is represented by attributes (variables) and actions (functions). So, a class
also contains variables and functions.
A group of objects having same behavior comes under the same class.
The standard Python compiler is written in C language and hence called CPython.
There are other flavors of Python, namely Jython, IronPython and PyPy.
A Python program contains source code that is first compiled by Python compiler to
produce byte code. This byte code is given to Python Virtual Machine (PVM) which
converts the byte code to machine code so that the processor will execute it and
display the results.
Python’s byte code is a set of instructions created by the Python development team to
represent all type of operations. Each byte code occupies 1 byte of memory and hence
the name byte code.
Python Virtual Machine (PVM) is the software containing an interpreter that converts
the byte code into machine code depending on the operating system and hardware of
the computer system where the Python program runs.
The standard PVM contains only an interpreter and hence Python is called an
interpreted language.
PVM is most often called Python interpreter.
The PVM of PyPy contains a compiler in addition to the interpreter. This compiler is
called Just In Time (JIT) compiler which is useful to speed up the execution of the
Python program.
The programmer need not allocate or deallocate memory in Python. It is the duty of
the PVM to allocate or deallocate memory for Python programs.
Memory manager is a module (or sub program) in PVM which will allocate memory for
objects. Garbage collector is another module in PVM that will deallocate (or free)
memory for the unused objects.
The programmer need not call the garbage collector. It will execute automatically
when the Python program is running in memory. In addition, the programmer can
also call the garbage collector whenever needed.
The files that contain Python programs along with Python compiler and libraries that
can be executed directly are called frozen binaries.
The ‘py_compile’ module converts a Python source file into a .pyc file that contains
byte code instructions. Generally, the .pyc files are provided to the end user.
When the Python source file is given, the ‘dis’ module displays the equivalent byte
code instructions in human readable format.
Chapter
WRITING OUR
FIRST PYTHON
PROGRAM 2
I
n this chapter, we will learn to write a simple Python program and execute it using
Python software. For this purpose, it is necessary to first install the Python software
in our computer system. So, we will first learn how to install Python on Windows
Operating system and then we will see how to execute a Python program using Python
compiler and PVM.
Installing Python for Windows
The latest version of Python (at the beginning of 2018) is Python 3.6.4. This version has
again got two variations, a 32-bit version and a 64-bit version. Depending upon our
operating system, we can choose a version. Nowadays most people use 64-bit operating
system and hence we can use the 64-bit version of Python by visiting the following link:
[Link]
At the bottom of this page, click the ‘Windows x86-64 executable installer’ link. The file
by the name ‘[Link]’ will be downloaded in our computer. By double
clicking and following the instructions, we can easily install the Python software latest
version in our system.
Let’s perform the following steps to install Python:
1. Double click the ‘[Link]’ file. The Setup dialog box appears
(Figure 2.1).
2. Select the Install launcher for all users and Add Python 3.6 to PATH checkboxes.
20 Chapter 2
3. Click the ‘Install Now’ link, as shown in Figure 2.1:
Figure 2.1: Running the Python Setup file
The Setup Progress bar will appear as shown in Figure 2.2:
Figure 2.2: Python installation progress
When Python installation is complete, we can see ‘Setup was successful’ message, as
shown in Figure 2.3.
Writing our first Python program 21
4. Click the ‘Close’ button, as shown in Figure 2.3:
Figure 2.3: Python installation complete window
Testing the Installation in Windows 10
1. Click the ‘Start’ button on the task bar of the Windows 10 operation system.
It displays all applications available in your system in alphabetical order.
Go to ‘P’ and view the ‘Python 3.6’ folder. In this folder, you can see the following
icons:
IDLE (Python 3.6 64-bit)
Python 3.6 (64-bit)
Python 3.6 anuals (64-bit)
Python 3.6 Module Docs (64-bit)
This is shown in Figure 2.4:
22 Chapter 2
Figure 2.4: Windows start button displaying Python installation
Writing our first Python program 23
2. Click the ‘IDLE (Python 3.6 64-bit)’ option. The Python’s IDLE (Integrated
Development Environment)’s Graphical user interface window opens (Figure 2.5).
At the bottom of the screen, we can see a white icon displayed on the taskbar.
3. Right click on the icon and click the ‘Pin to taskbar’ option. Clicking on this taskbar
icon will be sufficient to open Python IDLE whenever we want, as shown in
Figure 2.5:
Figure 2.5: The Python IDLE Window
4. Click the Python prompt, i.e., triple greater than symbol and type quit() to close
the Python window (Figure 2.6). A prompt asking ‘Do you want to kill it?’
appears.
5. Click the ‘OK’ button to quit from the IDLE window, as shown in Figure 2.6:
Figure 2.6: Closing the Python IDLE Window
6. Click the windows ‘Start’ button and then click ‘Python 3.6 (64 - bit)’ to open the
‘Python command line window’ which displays a black screen (Figure 2.7).