Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Programming in Python: Learn the Powerful Object-Oriented Programming
Programming in Python: Learn the Powerful Object-Oriented Programming
Programming in Python: Learn the Powerful Object-Oriented Programming
Ebook439 pages3 hours

Programming in Python: Learn the Powerful Object-Oriented Programming

Rating: 0 out of 5 stars

()

Read preview

About this ebook

This Book is meant for wide range of readers who wish to learn the basics of Python programming language. It can be helpful for students, programmers, researchers, and software developers. The basic concepts of python programming are dealt in detail. The various concepts of python language such as object-oriented features, operators, native data types, control structures, functions, exception handling, file handling, etc are discussed in detail with the authentic programming illustration of each.
presently, python programming is a hot topic among academician’s researchers, and program developers. As a result, the book is designed to give an in-depth knowledge of programming in python. This book can be used as handbook as well as a guide for students of all computer science stream at any grade beginning from 10+1 to Research in PhD. To conclude, we hope that the readers will find this book a helpful guide and valuable source of information about python programming.
LanguageEnglish
Release dateNov 21, 2019
ISBN9789387284869
Programming in Python: Learn the Powerful Object-Oriented Programming

Related to Programming in Python

Related ebooks

Programming For You

View More

Reviews for Programming in Python

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Programming in Python - Pooja Dr Sharma

    CHAPTER 1

    Introduction to Python Language

    Highlights

    Introduction and history of Python language

    Features of Python

    Applications of Python

    Python interactive help

    Installing and executing Python

    How Python differ from other languages

    We see that computers are capable of solving numerous problems of the real world. The problems can be as simple as to multiply two numbers or as cumbersome as to design and launch a space shuttle. This would be incorrect to assume that the computer can do all the tasks on its own. Any problem whose solution is not identified cannot be solved by a computer. The computer merely works on the set of instructions given to it by a programmer. If the computer does not understand the instructions then errors may occur and solution cannot be obtained. Therefore, it is keen responsibility of the programmer to devise a solution by giving correct instructions to the computer.

    1.1. Programming Language

    In order to solve a problem using computer, the programmer writes the instructions which are understandable by the computer. The computer understands only digital data either ‘0’ or ‘1’. The most basic language is the machine language that uses binary ‘0’ and ‘1’., which a computer can understand and execute very fast without using any translator (compiler or interpreter). However, it is quite difficult to code a program in machine language. The high level languages such as (C, C++, Java) are very simple to understand by humans because these languages use English language like statements. However, an additional program such as a compiler or interpreter is required to convert the high level language into machine language. Therefore, high level languages are slower than machine languages.

    In other words, we can say that people express themselves using a language that has many words. Computers use a simple language that consists of strings of 0s and 1s, with a 1 representing on and a 0 representing off.. The programming language works as a translator between you and the computer. Rather than learning the computer's native language (called as machine language), one can make use of a programming language to instruct the computer in a way that is easier to learn and understand.

    Every programming language has a particular structure with a specific syntax and semantics. Programming language is something like a code for writing down the instructions that a computer will follow. In fact, programmers often refer to their programs as computer code, or the process of implementing an algorithm. Alike other high level languages such as C, C++, Java, Python is also a programming language. In this book, we will learn Python programming language in detail.

    1.2. History of Python Language

    Python is a very powerful high-level, object oriented programming language. It was developed by Guido Van Rossum during 1985-1990 at the national research institute for mathematics and computer science in Netherlands. It is the derivative of several other languages such as ABC, Modulo-3, C, C++, Algol-68, SmallTalk and Unix Shell. ABC is a general-purpose programming language or programming environment, which had been developed in Netherlands, Amsterdam, at the CWI (Centrum Wiskunde & Informatica). The greatest achievement of ABC was to influence the design of Python.

    The source code of Python language is available under GNU i.e., General Public License (GPL). The Python is a very easy to use language with simple syntax, which makes it perfect language for beginners. It is an interpreter based language. Interpreter is a program that converts high level program into low level program, i.e., machine code. It is designed to be highly readable because it uses English keywords very frequently whereas other languages use punctuations. Moreover, it has fewer syntactical constructions than other languages.

    Guido Van Rossum published the first version of Python code (version 0.9.0) in February 1991. This release included already exception handling, functions, and the core data types of list, dict, str and others. Python was developed to be module as well as object oriented language. Another version 1.0 of Python was released in January 1994. The major new features included in this release were the functional programming tools lambda, map, filter and reduce, which Guido Van Rossum never liked. Six and a half years later in October 2000, Python 2.0 was introduced. This release included list comprehensions, a full garbage collector and it had the support for Unicode. Python flourished for another 8 years in the versions 2.x before the next major release as Python 3.0 (also known as Python 3000 and Py3K). Python 3 is not backwards compatible with Python 2.x. The emphasis in Python 3 had been on the removal of duplicate programming constructs and modules.

    1.3. Origin of Python Programming Language

    At the time when he began implementing Python, Guido van Rossum was reading the published scripts from Monty Python's Flying Circus (a BBC comedy series from the seventies). It occurred to him that he needed a name that should be short, unique, and slightly mysterious, so he decided to call the language Python.

    1.4. Features of Python

    The Python language exhibits numerous features, which are detailed as under:

    Beginner 's Language: Python is a great language for beginner level programmers, which supports the development of a wide range of applications from simple text processing to web browsers to games.

    Easy to Learn: Python has simple structure, a few keywords, and clearly defined syntax.

    Easy to Read: It is clearly defined language that is a non-programmer understands it very easily.

    Easy to Maintain: The source code of Python language is quite easy to maintain.

    Interpreted: It is interpreter based language. The programmer does not need to compile the code before executing the program similar to PERL and PHP. Python has a built-in debugging feature.

    Interactive: Python programs can be directly written to the Python prompt by which user directly interacts with the interpreter.

    Object Oriented: It supports object oriented style of programming that encapsulates code within objects. OOP breaks up code into several units that pass messages back and forth using classes.

    Versatile: Python modules are capable to work with multiple operating systems and user interfaces including images and sounds.

    Broad Standard Library: Python language 's bulk library is portable and cross platform compatible with UNIX, Windows and Macintosh.

    Interactive Mode: Python language has support for interactive mode, which allows interactive testing and debugging of code.

    Portable: Python language can be executed on wide variety of hardware platforms and has similar interface on all the platforms.

    Databases: Python language provides the facility of interfaces to all major commercial databases.

    GUI Programming: Python provides graphical user interface (GUI) applications that can be created and ported to many system calls, libraries and Windows systems such as Windows MFC, MAC, and the X Window system of Unix.

    Scalable: It provides a better structure and support for large programs.

    1.5. Limitations of Python

    As we see, Python language has many features, it has some limitations also as under:

    Python is an interpreter based language. Therefore, it is bit slower than compiler based languages.

    Since, Python is a high level language like C/C++/Java, it also uses many layers to communicate with the operating system and the computer hardware.

    Graphics intensive applications such as games make the program to run slower.

    1.6. Major Applications of Python

    The list of companies that use Python include the following:

    Yahoo! (for Yahoo! Maps)

    Google (for its spider and search engine)

    Linux Weekly News (published by using a web application written in Python)

    Industrial Light & Magic (used in the production of special effects for movies such as The Phantom Menace and The Mummy Returns).

    1.7. Getting Python

    The most up-to-date and current source code, binaries, documentation, news, etc., is available on the official website of Python: http://www.python.org/

    The complete Python documentation can be obtained from the following the site www.python.org/doc/. The documentation is available in HTML, PDF and PostScript formats.

    1.8. Installing Python

    Python distribution is available for a wide variety of platforms. You need to download only the binary code applicable for your platform and install Python.You need a C compiler to compile the source code manually, if the binary code for your platform is not available. The installation of Python at various platforms like Unix and Linux is given as follows

    1.8.1. Unix and Linux Installation

    Here are the simple steps to install Python on Unix/Linux machine.

    Open a Web browser and go to http://www.python.org/download/ as presented in Fig. 1.1.

    Follow the link to download zipped source code available for Unix/Linux.

    Download and extract files.

    Edit the Modules/Setup file if you want to customize some options.

    run ./configure script

    make

    make install

    This will install python in a standard location /usr/local/bin and its libraries at /usr/local/lib/pythonXX where XX is the version of Python that you are using.

    Fig. 1.1. Python Download

    1.8.2. Windows Installation

    Here are the steps to install Python on Windows machine.

    Open a Web browser and go to http://www.python.org/download/ as shown in the Fig. 1.1.

    Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you need to install.

    To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Save the installer file to your local machine and then run it to find out if your machine supports MSI.

    Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just accept the default settings, wait until the install is finished, and you are done.

    1.8.3. Macintosh Installation

    Recent Macs come with Python installed, but it may be several years out of date. See http://www.python.org/download/mac/ for instructions on getting the current version along with extra tools to support development on the Mac as shown in the Fig. 1.2. For older Mac OS's before Mac OS X 10.3 (released in 2003), MacPython is available."

    Fig. 1.2. Python Installation for MAC OS

    1.9. Setting up Path

    Many directories contain programs and other executable files, therefore, the operating system Windows, Unix/Linux, or MAC search for executables by providing a search path for directories. The path is a named string maintained by the operating system and is stored in an environment variable. The environment variable contains information available to the command shell and other programs.

    The Unix is a case sensitive operating system. However, Windows does not follow case sensitivity. So, path variable is named as PATH in Unix or Path in Windows. On the other hand, in Mac OS, all the path details are handled by the installer.The path variable of your operating system is needed to be set before invoking the Python interpreter from any particular directory.

    1.9.1. Setting up Path at Unix/Linux

    Since many shells are available in Unix/Linux OS, the path set up is slightly different for different shells. For csh, bash, sh, and ksh shells the Python path can be set as follows for a particular session in Unix:

    In the csh shell: type setenv PATH $PATH:/usr/local/bin/python and press Enter.

    In the bash shell (Linux): type export PATH=$PATH:/usr/local/bin/python and press Enter.

    In the sh or ksh shell: type PATH=$PATH:/usr/local/bin/python and press Enter.

    Note: /usr/local/bin/python is the path of the Python directory

    1.9.2. Setting up Path at Windows

    To add the Python directory to the path for a particular session in Windows:

    At the command prompt: type path %path%;C:\Python and press Enter.

    Note: C:\Python is the path of the Python directory

    1.10. Python Environment Variables

    Here are important environment variables, which can be recognized by Python:

    PYTHONPATH: This variable plays the similar role as PATH. It is sometimes preset by the Python installer. Its task is to tell the Python interpreter where to locate the module files imported into a program. It must include the source library directory and directories containing Python source codes.

    PYTHONSTARTUP: It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it comprises of commands that load utilities or modify PYTHONPATH.

    PYTHONCASEOK: It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. In order to activate this variable set it to any value.

    PYTHONHOME: It is another module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.

    1.11. Running Python

    There are three different ways to start Python:

    Interactive Interpreter

    Script from the Command Line

    Integrated Development Environment

    1.11.1. Interactive Interpreter

    You can start Python from DOS, Unix, or any other system that provides you a command-line interpreter or shell window. Enter python at the command line, which opens python interpreter as displayed in Fig. 1.3. It displays information about Python version, i.e, 3.5.1., the date it was released and a few options of what can be pursued next.

    Fig. 1.3. Python interpreter version 3.5.1.

    Here is the list of all the available command line options in Table 1.1.:

    Table. 1.1. List of command line options

    1.11.2. Script from the Command-line

    A Python script can be executed at command line also. This can be done by invoking the interpreter on your application, as displayed below:

    $python script.py #Unix/Linux

    or

    python % script.py #Unix/Linux

    or

    c:>python script.py #Windows/DOS

    1.11.3. Integrated Development Environment

    Python can be run from a Graphical User Interface (GUI) environment as well, if you have a GUI application on your system that supports Python.

    Unix: IDLE is the very first Unix IDE for Python.

    Windows: PythonWin is the first Windows interface for Python.

    Macintosh: The Macintosh version of Python is available from the main website along with the IDLE IDE, downloadable as either MacBinary.

    1.12. First Python Program

    We can execute the Python program in two different modes of programming viz. interactive mode programming and script mode programming. Each of them is elaborated as follows:

    1.12.1. Interactive Mode Programming

    In interactive mode programming, interpreter is invoked and the programmer can code statements directly to the interpreter without passing a script file as a parameter. Open up the Python interpreter and it brings up with the following information:

    $ python

    Python2.4.3(#1,Nov112010,13:34:43)

    [GCC 4.1.220080704(RedHat4.1.2-48)] on linux2

    Typehelp,copyright,creditsorlicensefor more information.

    >>>

    The simplest program to print Hello Python can be typed and run as follows:

    >>>printHello, Python!

    If you are running new version of Python, then you would need to use print statement with parenthesis as in print (Hello, Python!);as displayed in Fig. 1.4. However in the Python version 2.4.3, this produces the following result:

    Hello, Python!

    Fig. 1.4. A typical Python program in the Python version 3.5.1.

    1.12.2. The Script Mode Programming

    In script mode programming, the complete script is written in an editor such as Notepad in Windows and then interpreter is invoked with a script parameter. It begins execution of the script and continues until the script is finished. Let us write a simple Python program in a script. Python files have extension.py. Type the following source code in a first.py file as shown in Code 1.1.

    Code: 1.1. Illustration of Python first program (Script mode)

    print (Hello, Python!)

    We have saved the script in a directory under C drive. Then at the command prompt set the path as follows and run the script by calling the python interpreter. Eventually, we will obtain the desired output as Hello,Python! The graphic representation for this method is given in Fig. 1.5.

    C:\>prog>path %path%;c:\python

    C:\>prog> python first.py

    Output

    Hello, Python!

    Fig. 1.5. Execution of python script file first.py

    1.13. Python 's Interactive Help

    Python comes with built-in help utility, which is one of the major feature and support of Python language. The pre-requisite of using the built-in help of Python, you must have a little knowledge of programming. For a new programmer, it could be bit off-putting. Once, a programmer becomes familiar with programming terminology then he can make great use of built-in help provided by Python. Python programming help can be obtained in the following ways:

    Interactive mode help

    Getting help online through a web browser

    1.13.1. Interactive mode help

    The programmer can obtain help by running help system inside the Python interactive mode environment. The help system has its own prompt. For this, type help() at the Python prompt and you will get the help prompt (help>) with welcome message and some suggestions to

    Enjoying the preview?
    Page 1 of 1