Python
Python
Python
The biggest strength of Python is large library which can be used for the
following:
1. Machine Learning
2. GUI Applications
3. Web frameworks like Django (used by YouTube)
4. Multimedia
5. Image processing
6. Test frameworks
7. Web scraping
8. Scientific computing
9. Text processing
Companies Which Use Python
1. Simple
2. Easy to Learn
3. Open Source
4. High Level Language
5. Dynamically Typed
1|Page
BCA – Sem 5 Python Programming CC-302
6. Platform Independent
7. Portable
8. Procedure and Object Oriented
9. Interpreted
10. Extensible (Ex:- Jython for java integration, Iron Python for .Net)
11. Huge Library
12. Database Connectivity
Python Execution Process
Step 1:
Every Python program is typed , file name will be saved with an extension .py.
Step 2:
After typing the program, the next step is to compile the program using Python
compiler.
The compiler converts the Python program into another code called byte code.
Step 3:
The next step is to run the program.
If we directly give the byte code to the computer, it cannot execute them. Any computer can
execute only binary code which comprises 1s and 0s. Since the binary code is understandable
to the machine (computer), it is also called machine code. It is therefore necessary to convert
the byte code into machine code so that our computer can understand and execute it.
For this purpose, we should use PVM (Python Virtual Machine).
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.
Python Bytecode
Byte code represents a fixed set of instructions that represents all operations like arithmetic
operations, comparison operations, memory related operations etc..
which run on any operating system and hardware. 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.
Bytecode Explanation
Lets consider below program for viewing its byte code:
We can type this program in a text editor like Notepad and then save it as 'first.py'. It means,
the first.py file contains the source code
Now, let's compile the program using Python compiler as
2|Page
BCA – Sem 5 Python Programming CC-302
C:\>python first.py
It will display the result as: sum=20
1 0 LOAD_CONST 0 (10)
2 DUP_TOP
4 STORE_NAME 0 (a)
6 STORE_NAME 1 (b)
2 8 LOAD_NAME 2 (print)
10 LOAD_CONST 1 ('Sum =')
12 LOAD_NAME 0 (a)
14 LOAD_NAME 1 (b)
16 BINARY_ADD
18 CALL_FUNCTION 2
20 POP_TOP
22 LOAD_CONST 2 (None)
24 RETURN_VALUE
Flavors of Python
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:
CPython:
This is the standard Python compiler implemented in C language. This is the Python software
being downloaded and used by programmers directly from CPython . In this, any Python
program is internally converted into byte code using C language functions. This byte code is
run on the interpreter available in Python Virtual Machine (PVM) created in C language. The
advantage is that it is possible to execute C and C++ functions and programs in CPython.
Jython:
This is earlier known as JPython. This is the implementation of Python programming language
which is designed to run on Java platform. Jython compiler first compiles the Python program
into Java byte code. This byte code is executed by Java Virtual Machine (JVM) to produce the
output. Jython contains libraries which are useful for both Python and Java programmers. This
can be downloaded from Jython .
IronPython:
This is another implementation of Python language for .NET framework. This is written in C#
(C Sharp) language. The Python program when compiled gives an intermediate language (IL)
which runs on Common Language Runtime (CLR) to produce the output. This flavor of Python
gives flexibility of using both the .NET and Python libraries. IronPython can be downloaded
from IronPython .
PyPy:
This is Python implementation using Python language. Actually, PyPy is written in a language
called RPython which was created in Python language. RPython is suitable for creating
language interpreters. PyPy programs run very fast since there is a JIT (Just In Time) compiler
added to the PVM. PyPy can be downloaded by visiting the page: PyPy . Since the original
Python uses only an interpreter in the Python Virtual Machine (PVM), the Python programs
3|Page
BCA – Sem 5 Python Programming CC-302
run slowly. To improve the speed of execution, a compiler called JIT (Just In Time) is
introduced into the PVM of PyPy. Hence, PyPy programs run faster than those of Python. We
can download PyPy from PyPy .
RubyPython:
This is a bridge between the Ruby and Python interpreters. It encloses a Python interpreter
inside Ruby applications. This flavor of Python can be downloaded from RubyPython .
StacklessPython:
Small tasks which should run individually are called tasklets. Tasklets run independently on
CPU and can communicate with others via channels. A channel is a manager that takes care of
scheduling the tasklets, controlling them and suspending them. A thread is a process which
runs hundreds of such tasklets. We can create threads and tasklets in StacklessPython which is
reimplementation of original Python language. This can be downloaded
from StacklessPython .
Pythonxy:
This is pronounced as Python xy and written as Python(X,Y). This is the Python
implementation that we get after adding scientific and engineering related packages. We can
download Pythonxy from Pythonxy .
AnacondaPython:
When Python is redeveloped for handling large-scale data processing, predictive analytics and
scientific computing, it is called Anaconda Python. This implementation mainly focuses on
large scale of data. This can be downloaded from AnacondaPython .
The role of Python Virtual Machine (PVM) is to convert the byte code instructions into
machine code so that the computer can execute those machine code instructions and display
the final output. To carry out this conversion, PVM is equipped with an interpreter.
The interpreter converts the byte code into machine code and sends that machine code to the
computer processor for execution. Since interpreter is playing the main role, often the Python
Virtual Machine is also called an interpreter.
Memory Management in Python
4|Page
BCA – Sem 5 Python Programming CC-302
In C or C++, the programmer should allocate and deallocate (or free) memory dynamically,
during runtime. For example, to allocate memory, the programmer may use malloc() function
and to deallocate the memory, he may use the free() function. But inPython, 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.
Memory management
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.
Following Figure shows the allocation of memory by Python's Virtual Machine:
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.
The following are the major differences from the python to C Language
C language Python Language
C is procedure-oriented programming
Python is object-oriented oriented language. It
language.It does not contain the features like
contains features like classes, objects,
classes, objects, inheritance, polymorphism,
inheritance, polymorphism, etc.
etc.
Pointers concept is available in C. Python does not use pointers.
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.
Python programs are slower compared to C.
C programs execute faster. 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 strong.
weak.
C does not have exception handling facility Python handles exceptions and hence Python
and hence C programs are weak. programs are robust.
The programmer should allocate and
Memory allocation and deallocation is done
deallocate memory using malloc(), calloc(),
automatically by PVM.
realloc() or free() functions.
Automatic garbage collector is available in
C does not contain a garbage collector.
Python.
Python supports only single dimensional
C supports single and multi-dimensional arrays. To work with multi-dimensional arrays,
arrays. we should use third party applications like
numpy.
Array index can be positive or negative integer
The array index should be positive integer. number. Negative index represents locations
from the end of the array.
Checking the location outside the allocation Python performs checking outside an array for
of an array is not supported in C. all iterations while looping.
C supports in-line assignments. Python does not support in-line assignments.
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 separator.
separate expressions.
Indentation of statements is not necessary in Indentation is required to represent a block of
C. statements.
6|Page
BCA – Sem 5 Python Programming CC-302
7|Page
BCA – Sem 5 Python Programming CC-302
Simple Program :
num1=10
num2=20
print(“The Sum :”(a+b))
The first two are called interactive modes where we can type the program one line at a time
and the PVM executes it immediately.
The last one is called non-interactive mode where we ask the PVM to execute our program
after typing the entire program.
Comments in Python
There are two types of comments in Python
8|Page