Introduction to Python

1.1 INTRODUCTION

Python is a strong, procedural, object-oriented, functional language crafted in the late 1980s by Guido Van Rossum. The language is named after Monty Python, a comedy group. The language is currently being used in diverse application domains. These include software development, web development, Desktop GUI development, education, and scientific applications. So, it spans almost all the facets of development. Its popularity is primarily owing to its simplicity and robustness, though there are many other factors too which are discussed in the chapters that follow.


There are many third-party modules for accomplishing the above tasks. For example, Django, an immensely popular Web framework dedicated to clean and fast development, is developed on Python. This, along with the support for HTML, emails, FTP, etc., makes it a good choice for web development.


Third-party libraries are also available for software development. One of the most common examples is Scions, which is used for build controls. When joined with the inbuilt features and support, Python also works miracles for GUI development and for developing mobile applications, e.g., Kivy is used for developing multi-touch applications.


1.2 FEATURES OF PYTHON

As stated earlier, Python is a simple but powerful language. Python is portable. It has built-in object types, and many libraries and is free. This section briefly discusses the features and strengths of Python.


1.2.1 Easy

Python is easy to learn and understand. As a matter of fact, if you are from a programming background you will find it elegant and uncluttered. The removal of braces and parentheses makes the code short and sweet. Also, some of the tasks in Python are pretty easy. For example, swapping numbers in Python is as easy as writing (a, b)= (b, a).


It may also be stated here that learning something new is an involved and intricate task. However, the simplicity of Python makes it almost a cakewalk. Moreover, learning advanced features in Python is a bit intricate, but is worth the effort. It is also easy to understand a project written in Python. The code, in Python, is concise and effective and therefore understandable and manageable.


1.2.2 Type and Run

In most projects, testing something new requires scores of changes and therefore recompilations and re-runs. This makes testing of code a difficult and time-consuming task. In Python, a code can be run easily. As a matter of fact, we run scripts in Python.


As we will see later in this chapter, Python also provides the user with an interactive environment, in which one can run independent commands.


1.2.3 Syntax

The syntax of Python is easy; this makes the learning and understanding process easy. According to most the authors, the three main features which make Python attractive are that it’s simple, small, and flexible.


1.2.4 Mixing

If one is working on a big project, with perhaps a large team, it might be the case that some of the team members are good in other programming languages. This may lead to some of the modules in some other languages wanting to be embedded with the core Python code. Python allows and even supports this.


1.2.5 Dynamic Typing

Python has its own way of managing memory associated with objects. When an object is created in Python, memory is dynamically allocated to it. When the life cycle of the object ends, the memory is taken back from it. This memory management of Python makes the programs more efficient.


1.2.6 Built-in Object Types

As we will see in the next chapter Python has built-in object types. This makes the task to be accomplished easy and manageable. Moreover, the issues related to these objects are beautifully handled by the language.


1.2.7 Numerous Libraries and Tools

In Python, the task to be accomplished becomes easy—really easy. This is because most of the common tasks (as a matter of fact, not so common tasks too) have already been handled in Python. For example, Python has libraries which help users to develop GUI’s, write mobile applications, incorporate security features and even read MRI’s. As we will see in the following chapters, the libraries and supporting tools make even the intricate tasks like pattern recognition easy.


1.2.8 Portable

A program written in Python can run in almost every known platform, be it Windows, Linux, or Mac. It may also be stated here that Python is written in C.


1.2.9 Free

Python is not propriety software. One can download Python compilers from among the various available choices. Moreover, there are no known legal issues involved in the distribution of the code developed in Python.


1.3 THE PARADIGMS

1.3.1 Procedural

In a procedural language, a program is actually a set of statements that execute sequentially. The only option a program has, in terms of manageability, is dividing the program into small modules. “C,” for example, is a procedural language. Python supports procedural programming. The first section of this book deals with procedural programming.


1.3.2 Object-Oriented

This type of language primarily focuses on the instance of a class. The instance of a class is called an object. A class is a real or a virtual entity that has importance to the problem at hand and has sharp physical boundaries. For example in a program that deals with student management, “student” can be a class. Its instances are made and the task at hand can be accomplished by communicating via methods. Python is object-oriented. Section 2 of this book deals with object-oriented programming.


1.3.3 Functional

Python also supports functional programming. Moreover, Python supports immutable data, tail optimization, etc.


So Python is a procedural, object-oriented, and functional language.


1.4 CHRONOLOGY AND USES

Having seen the features, let us now move onto the chronology and uses of Python. This section briefly discusses the development and uses of Python and will motivate the reader to bind with the language.


1.4.1 Chronology

Python is written in C. It was developed by Guido Van Rossum, who is now the Benevolent Director for Life of Python. The reader is expected to take note of the fact that Python has got nothing to do with pythons or snakes. The name of the language comes from the show “Monty Python’s Flying Circus,” which was one of the favorite shows of the developer, Guido van Rossum. Many people attribute the fun part of the language to the inspiration.


Python is easy to learn as the core of the language is pretty concise. The simplicity of Python can also be attributed to the desire of the developers to make a language that was very simple, easy to learn but quite powerful.


The continuous betterment of the language has been possible because of a dedicated group of people, committed to supporting the cause of providing the world with an easy yet powerful language. The growth of the language has given rise to the creation of many interest groups and forums for Python. A change in the language can be brought about by what is generally referred to as the PEP (Python Enhancement Project). The PSF (Python Software Foundation) takes care of this.


1.4.2 Uses

Python is being used to accomplish many tasks, the most important of which are as follows:


Graphical User Interface (GUI) development

Scripting web pages

Database programming

Prototyping

Gaming

Component based programming


If you are working in Unix or Linux, you don’t need to install Python. This is because in Unix and Linux systems, Python is generally pre-installed. However, if you work in Windows or Mac then you need to download Python. Once you have decided to download Python, look for its latest version. The reader is requested to ensure that the version he/she intends to download is not an alpha or a beta version. Reference 1 at the end of the book gives a brief overview of distinctions between two of the most famous versions. The next section briefly discusses the steps for downloading Anaconda, an open source distribution software.


Many development environments are available for Python. Some of them are as follows:


PyDev with Eclipse

Emacs

Vim

TextMate

Gedit

Idle

PIDA (Linux)(VIM based)

NotePad++ (Windows)

BlueFish (Linux)


There are some more options available. However, this book uses IDLE and Anaconda. The next section presents the steps involved in the installation of the Anaconda.


1.5 INSTALLATION OF ANACONDA

In order to install Anaconda, go to https://docs.continuum.io/anaconda/install and select the installer (Windows or Mac OS or Linux). This section presents the steps involved in the installation of Anaconda on the Windows Operating System.


First of all, one must choose the installer (32 bit or 64 bit). In order to do so, click on the selected installer and download the .exe file. The installer will ask you to install it on the default location. You can provide a location that does not contain any spaces or Unicode characters. It may happen that during the installation you might have to disable your anti-virus software.


1.6 CONCLUSION

Before proceeding any further, the reader must take note of the fact that some things in Python are different when compared to any other language. The following points must be noted to avoid any confusion.

In Python, statements do not end with any special characters. Python considers the newline character as an indication of the fact that the statement has ended. If a statement is to span more than a single line, the next line must be preceded with a (\).

In Python, indentation is used to detect the presence of loops. The loops in Python do not begin or end with delimiters or keywords.

A file in Python is generally saved with a .py extension.

The shell can be used as a handy calculator.

The type of a variable need not be mentioned in a program.

The choice at every step is good but can also be intimidating. As stated earlier, Python’s core is small, and therefore it is easy to learn. Moreover, there are some things like (if/else), loops, and exception handling which are used in almost all the programs.


1.7 EXERCISES

MULTIPLE CHOICE QUESTIONS

1.Python can subclass a class made in

Python only

Python, C++

Python, C++, C#, Java

None of the above


2. Who created Python?

Monty Python

Guido Van Rossum

Dennis Richie

None of the above


3. Monty Python was

Creator of Python Programming Language

British Comedy Group

American Band

Brother of Dosey Howser


4. In Python, libraries, and tools

Not supported

Supported but not encouraged

Supported and encouraged

Supported (only that of PSF’s)


5. Python has

Built in object types

Data types

Both

None of the above


6.Python is a

Procedural language

object-oriented Language

Functional

All of the above


7. There is no data type, so a code in Python is applicable to a whole range of Objects. This is called

Dynamic Binding

Dynamic Typing

Dynamic Leadership

None of the above


8. Which of the following is automatic memory management?

Automatically assigning memory to objects

Taking back the memory at the end of life cycle

Both

None of the above


9. PEP is

Python Ending Procedure

Python Enhancement proposal

Python Endearment Project

none of the above


10.PSF is

Python Software Foundation

Python Selection Function

Python segregation function

None of the above


11.What can be done in Python

GUI

Internet scripting

Games

All of the above


12. What can be done using Python?

System programming

Component based programming

Scientific programming

All of the above


13.Python is used in

Google

Raspberry Pi

Bit Torrent

All of the above


14.Python is used in

App Engine

YouTube sharing

Real time programming

All of the above


15. Which is faster?

PyPy

IDLE

Both are equally good

depends on the task


1.8 THEORY


Write the names of three projects which are using Python.

Explain a few applications of Python.

What type of language is Python? (Procedural, object-oriented or functional)

What is PEP?

What is PSF?

Who manages Python?

Is Python open source or proprietary?

What languages can be supported by Python?

Explain the chronology of the development of Python.

Name a few editors for Python.

What are the features of Python?

What is the advantage of using Python over other languages?

What is Dynamic Typing?

Does Python have data types?

How is Python different from Java?