Introduction to Python 3
Python is open source software, which means anybody can freely download it from
[Link] and use it to develop programs. Its source code can be accessed and
modified as required in the projects.
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:
Simple: Python is a simple programming language. When we read a Python program,
we feel like reading English sentences. It means more clarity and less stress on
understanding the syntax of the language. Hence, developing and understanding
programs will become easy.
Easy to learn: Python uses very few keywords. Its programs use very simple
structure. So, developing programs in Python become easy. Also, Python resembles C
language. Most of the language constructs in C are also available in Python. Hence,
migrating from C to Python is easy for programmers.
Open source: There is no need to pay for Python software. Python can be freely
downloaded from [Link] website. Its source code can be read, modified and
can be used in programs as desired by the programmers.
High level language: Programming languages are of two types: low level and high
level. A low level language uses machine code instructions to develop programs.
These instructions directly interact with the CPU. Machine language and assembly
language are called low level languages.
High level languages use English words to develop programs. These are easy to learn
and use. Like COBOL, PHP or Java, Python also uses English words in its programs
and hence it is called high level programming language.
Dynamically typed: In Python, we need not declare anything. An assignment
statement binds a name to an object, and the object can be of any type. If a name is
assigned to an object of one type, it may later be assigned to an object of a different
type. This is the meaning of the saying that Python is a dynamically typed language.
Languages like C and Java are statically typed. In these languages, the variable
names and datatypes should be mentioned properly. Attempting to assign an object
of the wrong type to a variable name triggers error or exception.
Platform independent: When a Python program is compiled using a Python
compiler, it generates byte code. Python’s byte code represents a fixed set of
instructions that run on all operating systems and hardware. Using a Python Virtual
Machine (PVM), anybody can run these byte code instructions on any computer
system. Hence, Python programs are not dependent on any specific operating system.
We can use Python on almost all operating systems like UNIX, Linux, Windows,
4 Chapter 1
Macintosh, Solaris, OS/2, Amiga, AROS, AS/400, etc. This makes Python an ideal
programming language for any network or Internet.
Portable: When a program yields the same result on any computer in the world, then
it is called a portable program. Python programs will give the same result since they
are platform independent. Once a Python program is written, it can run on any
computer system using PVM. However, Python also contains some system dependent
modules (or code), which are specific to operating system. Programmers should be
careful about such code while developing the software if they want it to be completely
portable.
Procedure and object oriented: Python is a procedure oriented as well as an object
oriented programming language. In procedure oriented programming languages (e.g.
C and Pascal), the programs are built using functions and procedures. But in object
oriented languages (e.g. C++ and Java), the programs use classes and objects.
Let’s get some idea on objects and classes. An object is anything that exists physically in
the real world. Almost everything comes in this definition. Let’s take a dog with the name
Snoopy. We can say Snoopy is an object since it physically exists in our house. Objects
will have behavior represented by their attributes (or properties) and actions. For
example, Snoopy has attributes like height, weight, age and color. These attributes are
represented by variables in programming. Similarly, Snoopy can perform actions like
barking, biting, eating, running, etc. These actions are represented by methods
(functions) in programming. Hence, an object contains variables and methods.
A class, on the other hand, does not exist physically. A class is only an abstract idea
which represents common behavior of several objects. For example, dog is a class. When
we talk about dog, we will have a picture in our mind where we imagine a head, body,
legs, tail, etc. This imaginary picture is called a class. When we take Snoopy, she has all
the features that we have in our mind but she exists physically and hence she becomes
the object of dog class. Similarly all the other dogs like Tommy, Charlie, Sophie, etc.
exhibit same behavior like Snoopy. Hence, they are all objects of the same class, i.e. dog
class. We should understand the point that the object Snoopy exists physically but the
class dog does not exist physically. It is only a picture in our mind with some attributes
and actions at abstract level. When we take Snoopy, Tommy, Charlie and Sophie, they
have these attributes and actions and hence they are all objects of the dog class.
As we described in the preceding paragraph, a class indicates common behavior of
objects. This common behavior is represented by attributes and actions. Attributes are
represented by variables and actions are performed by methods (functions). So, a class
also contains variables and methods just like an object does. Figure 1.2 shows
relationship between a class and its object:
Introduction to Python 5
Figure 1.2: A Class and its object
Similarly, parrot, sparrow, pigeon and crow are objects of the bird class. We should
understand that bird (class) is only an idea that defines some attributes and actions. A
parrot and sparrow have the same attributes and actions but they exist physically.
Hence, they are objects of the bird class.
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.
Interpreted: A program code is called source code. After writing a Python program,
we should compile the source code using Python compiler. Python compiler
translates the Python program into an intermediate code called byte code. This byte
code is then executed by PVM. Inside the PVM, an interpreter converts the byte code
instructions into machine code so that the processor will understand and run that
machine code to produce results.
Extensible: The programs or pieces of code written in C or C++ can be integrated into
Python and executed using PVM. This is what we see in standard Python that is
downloaded from [Link]. There are other flavors of Python where programs
from other languages can be integrated into Python. For example, Jython is useful to
integrate Java code into Python programs and run on JVM (Java Virtual Machine).
Similarly, Iron Python is useful to integrate .NET programs and libraries into Python
programs and run on CLR (Common Language Runtime).
Embeddable: We can insert Python programs into a C or C++ program. Several
applications are already developed in Python which can be integrated into other
programming languages like C, C++, Delphi, PHP, Java and .NET. It means
6 Chapter 1
programmers can use these applications for their advantage in various software
projects.
Huge library: Python has a big library which can be used on any operating system
like UNIX, Windows or Macintosh. Programmers can develop programs very easily
using the modules available in the Python library.
Scripting language: A scripting language is a programming language that does not
use a compiler for executing the source code. Rather, it uses an interpreter to
translate the source code into machine code on the fly (while running). Generally,
scripting languages perform supporting tasks for a bigger application or software. For
example, PHP is a scripting language that performs supporting task of taking input
from an HTML page and send it to Web server software. Python is considered as a
scripting language as it is interpreted and it is used on the Internet to support other
software.
Database connectivity: A database represents software that stores and manipulates
data. For example, Oracle is a popular database using which we can store data in the
form of tables and manipulate the data. Python provides interfaces to connect its
programs to all major databases like Oracle, Sybase or MySql.
Scalable: A program would be scalable if it could be moved to another operating
system or hardware and take full advantage of the new environment in terms of
performance. Python programs are scalable since they can run on any platform and
use the features of the new platform effectively.
Batteries included: The huge library of Python contains several small applications
(or small packages) which are already developed and immediately available to
programmers. These small packages can be used and maintained easily. Thus the
programmers need not download separate packages or applications in many cases.
This will give them a head start in many projects. These libraries are called ‘batteries
included’. Some interesting batteries or packages are given here:
argparse is a package that represents command-line parsing library
botois Amazon web services library
CherryPy is anobject-oriented HTTP framework
cryptography offers cryptographic techniques for the programmers
Fiona reads and writes big data files
jellyfish is a library for doing approximate and phonetic matching of strings
mysql-connector-pythonis a driver written in Python to connect to MySQL
database
numpy is a package for processing arrays of single or multidimensional type
Introduction to Python 7
pandas is a package for powerful data structures for data analysis, time series
and statistics
matplotlib is a package for drawing electronic circuits and 2D graphs.
Pillow is a Python imaging library
pyquery represents jquery-like library for Python
scipy is the scientific library to do scientific and engineering calculations
Sphinx is the Python documentation generator
sympy is a package for Computer algebra system (CAS) in Python
w3lib is a library of web related functions
whoosh contains fast and pure Python full text indexing, search and spell
checking library
To know the entire list of packages included in Python, one can visit:
[Link]
Execution of a Python Program
Let’s assume that we write a Python program with the name [Link]. Here, x is the program
name and the .py is the extension name. Every Python program is typed with an
extension name .py. 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.
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. These byte code instructions are
contained in the file [Link]. Here, the [Link] file represents a python compiled file.
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.
8 Chapter 1
An interpreter translates the program source code line by line. Hence, it is slow. The
interpreter that is found inside the PVM runs the Python program slowly. To rectify this
problem, in some flavors of Python, a compiler is added to the PVM. This compiler also
converts the byte code into machine code but faster than the interpreter. This compiler is
called JIT (Just In Time) compiler. The advantage of JIT compiler is to improve speed of
execution of a Python program and thus improving the performance.
We should remember that JIT compiler is not available in all Python environments. For
example, the standard Python software is called CPython which is created using C
language that does not include a JIT compiler. But, PyPy, which is created in Python
language itself uses a JIT compiler in addition to an interpreter in the PVM. So, the PyPy
flavor of Python definitely offers faster execution of the Python programs than CPython.
Figure 1.3 shows the steps for executing a Python program:
Figure1.3: Steps of Execution of a Python Program
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
in the memory and the output is finally visible. For example, if our Python program name
is [Link], we can use Python compiler to compile it as:
C:\>python [Link]
In the preceding statement, python is the command for calling the Python compiler. The
compiler should convert the [Link] file into its byte code equivalent file, [Link]. Instead of
doing this, the compiler directly displays the output or result.
If we observe, we cannot find any files in the directory with the extension .pyc. The
reason is that all the steps in the sequence: source code byte code machine code
output are internally completed and then the final result is displayed. Hence, after
execution, we cannot find any .pyc file in the directory.
To separately create .pyc file from the source code, we can use the following command:
C:\>python –m py_compile [Link]
In the preceding command, we are calling the Python compiler with –m option. -m
represents module and the module name is py_compile. This module generates the .pyc
file for the specified .py file. The compiler creates a separate directory in the current
directory by the name __pycache__ where it stores the .pyc file. The .pyc file name may be
something like: [Link]. The word cpython here indicates that we are using the
Python compiler that was created using C. This is of course, the standard Python
compiler.