PYTHON PROGRAMING OVERVIEW
Overview of Python
Python
is a high-level, multi-paradigm programming language.
As
Python is an interpreter-based language, it is easier to learn compared to some
of the other mainstream languages. Python is a dynamically typed language with
very intuitive data types.
Python
is an open-source and cross-platform programming language. It is available for
use under Python Software Foundation License (compatible to GNU General Public License) on
all the major operating system platforms Linux, Windows and Mac OS.
The
design philosophy of Python emphasizes on simplicity, readability and
unambiguity. Python is known for its batteries included approach as Python
software is distributed with a comprehensive standard library of functions and
modules.
Python's
design philosophy is documented in the Zen
of Python. It consists of nineteen aphorisms
such as −
·
Beautiful is better than ugly
·
Explicit is better than implicit
·
Simple is better than complex
·
Complex is better than complicated
To
obtain the complete Zen of Python document, type import this in
the Python shell −
>>> import
this
The
Zen of Python, by Tim Peters
Beautiful
is better than ugly.
Explicit
is better than implicit.
Simple
is better than complex.
Complex
is better than complicated.
Flat
is better than nested.
Sparse
is better than dense.
Readability
counts.
Special
cases aren't special enough to break
the rules.
Although
practicality beats purity.
Errors
should never pass
silently.
Unless
explicitly silenced.
In
the face of ambiguity, refuse the temptation to guess.
There
should be one-- and preferably only one --obvious
way to do it.
Although
that way may not be
obvious at first unless you're Dutch.
Now
is better than never.
Although
never is
often better than *right* now.
If
the implementation is
hard to explain, it's a bad idea.
If
the implementation is
easy to explain, it may be a good idea.
Namespaces
are one honking great idea --
let's do more of those!
Python
supports imperative, structured as well as object-oriented programming
methodology. It provides features of functional programming as well.
Guido
Van Rossum, a Dutch programmer, created Python
programming language. In the late 80's, he had been working on the development
of ABC language in a computer science research institute named Centrum Wiskunde & Informatica (CWI) in the Netherlands. In 1991, Van Rossum
conceived and published Python as a successor of ABC language.
For
many uninitiated people, the word Python is related to a species of snake.
Rossum though attributes the choice of the name Python to a popular comedy
series "Monty Python's Flying Circus" on BBC.
Being
the principal architect of Python, the developer community conferred upon him
the title of "Benevolent Dictator for Life (BDFL). However, in 2018, Rossum relinquished
the title. Thereafter, the development and distribution of the reference
implementation of Python is handled by a nonprofit organization Python Software Foundation.
Important
stages in the history of Python −
Python 0.9.0
Python's
first published version is 0.9. It was released in February 1991. It consisted
of support for core object-oriented programming principles.
Python 1.0
In
January 1994, version 1.0 was released, armed with functional programming
tools, features like support for complex numbers etc.
Python 2.0
Next
major version − Python 2.0 was launched in October 2000. Many new features such
as list comprehension, garbage collection and Unicode support were included
with it.
Python 3.0
Python
3.0, a completely revamped version of Python was released in December 2008. The
primary objective of this revamp was to remove a lot of discrepancies that had
crept in Python 2.x versions. Python 3 was backported to Python 2.6. It also
included a utility named as python2to3 to facilitate automatic translation of Python
2 code to Python 3.
EOL for Python 2.x
Even
after the release of Python 3, Python Software Foundation continued to support
the Python 2 branch with incremental micro versions till 2019. However, it
decided to discontinue the support by the end of year 2020, at which time
Python 2.7.17 was the last version in the branch.
Current Version
Meanwhile,
more and more features have been incorporated into Python's 3.x branch. As of
date, Python 3.11.2 is the current stable version, released in
February 2023.
What's New in Python 3.11?
One
of the most important features of Python's version 3.11 is the significant
improvement in speed. According to Python's official documentation, this
version is faster than the previous version (3.10) by up to 60%. It also states
that the standard benchmark suite shows a 25% faster execution rate.
·
Python 3.11 has a better exception
messaging. Instead of generating a long traceback on the occurrence of an
exception, we now get the exact expression causing the error.
·
As per the recommendations of PEP 678,
the add_note() method is added to the BaseException class.
You can call this method inside the except clause and pass a custom error
message.
·
It also adds the cbroot() function
in the maths module.
It returns the cube root of a given number.
·
A new module tomllib is
added in the standard library. TOML (Tom's Obvious Minimal Language) can be
parsed with tomlib module function.
In
this chapter, let's highlight some of the important features of Python that
make it widely popular.
Python is Easy to Learn
This
is one of the most important reasons for the popularity of Python. Python has a
limited set of keywords. Its features such as simple syntax, usage of
indentation to avoid clutter of curly brackets and dynamic typing that doesn't
necessitate prior declaration of variable help a beginner to learn Python
quickly and easily.
Python is Interpreter Based
Instructions
in any programming languages must be translated into machine code for the
processor to execute them. Programming languages are either compiler based or
interpreter based.
In
case of a compiler, a machine language version of the entire source program is
generated. The conversion fails even if there is a single erroneous statement.
Hence, the development process is tedious for the beginners. The C family
languages (including C, C++, Java, C Sharp etc) are compiler based.
Python
is an interpreter based language. The interpreter takes one instruction from
the source code at a time, translates it into machine code and executes it.
Instructions before the first occurrence of error are executed. With this
feature, it is easier to debug the program and thus proves useful for the
beginner level programmer to gain confidence gradually. Python therefore is a
beginner-friendly language.
Python is Interactive
Standard
Python distribution comes with an interactive shell that works on the principle
of REPL (Read − Evaluate − Print − Loop). The shell presents a Python prompt
>>>. You can type any valid Python expression and press Enter. Python
interpreter immediately returns the response and the prompt comes back to read
the next expression.
>>> 2*3+1
7
>>> print ("Hello World")
Hello
World
The
interactive mode is especially useful to get familiar with a library and test
out its functionality. You can try out small code snippets in interactive mode
before writing a program.
Python is MultiParadigm
Python
is a completely object-oriented language. Everything in a Python program is an
object. However, Python conveniently encapsulates its object orientation to be
used as an imperative or procedural language − such as C. Python also provides
certain functionality that resembles functional programming. Moreover, certain
third-party tools have been developed to support other programming paradigms
such as aspect-oriented and logic programming.
Python's Standard Library
Even
though it has a very few keywords (only Thirty Five), Python software is
distributed with a standard library made of large number of modules and
packages. Thus Python has out of box support for programming needs such as
serialization, data compression, internet data handling, and many more. Python
is known for its batteries included approach.
Python is Open Source and Cross
Platform
Python's
standard distribution can be downloaded from https://www.python.org/downloads/ without any
restrictions. You can download pre-compiled binaries for various operating
system platforms. In addition, the source code is also freely available, which
is why it comes under open source category.
Python
software (along with the documentation) is distributed under Python Software
Foundation License. It is a BSD style permissive software license and
compatible to GNU GPL (General Public License).
Python
is a cross-platform language. Pre-compiled binaries are available for use on
various operating system platforms such as Windows, Linux, Mac OS, Android OS.
The reference implementation of Python is called CPython and is written in C.
You can download the source code and compile it for your OS platform.
A
Python program is first compiled to an intermediate platform independent byte
code. The virtual machine inside the interpreter then executes the byte code.
This behaviour makes Python a cross-platform language, and thus a Python
program can be easily ported from one OS platform to other.
Python for GUI Applications
Python's
standard distribution has an excellent graphics library called TKinter. It is a
Python port for the vastly popular GUI toolkit called TCL/Tk. You can build
attractive user-friendly GUI applications in Python. GUI toolkits are generally
written in C/C++. Many of them have been ported to Python. Examples are PyQt,
WxWidgets, PySimpleGUI etc.
Python's Database Connectivity
Almost
any type of database can be used as a backend with the Python application.
DB-API is a set of specifications for database driver software to let Python
communicate with a relational database. With many third party libraries, Python
can also work with NoSQL databases such as MongoDB.
Python is Extensible
The
term extensibility implies the ability to add new features or modify existing
features. As stated earlier, CPython (which is Python's reference
implementation) is written in C. Hence one can easily write modules/libraries
in C and incorporate them in the standard library. There are other
implementations of Python such as Jython (written in Java) and IPython (written
in C#). Hence, it is possible to write and merge new functionality in these
implementations with Java and C# respectively.
Python's Active Developer Community
As
a result of Python's popularity and open-source nature, a large number of
Python developers often interact with online forums and conferences. Python
Software Foundation also has a significant member base, involved in the
organization's mission to "promote, protect, and advance the Python
programming language"
Python
also enjoys a significant institutional support. Major IT companies Google,
Microsoft, and Meta contribute immensely by preparing documentation and other
resources.
Python vs C++
Both
Python and C++ are among the most popular programming languages. Both of them
have their advantages and disadvantages. In this chapter, we shall take a look
at their characteristic features.
Compiled vs Interpreted
Like
C, C++ is also a compiler-based language. A compiler translates the entire code
in a machine language code specific to the operating system in use and
processor architecture.
Python
is interpreter-based language. The interpreter executes the source code line by
line.
Cross platform
When
a C++ source code such as hello.cpp is compiled on Linux, it can be only run on
any other computer with Linux operating system. If required to run on other OS,
it needs to be compiled.
Python
interpreter doesn't produce compiled code. Source code is converted to byte
code every time it is run on any operating system without any changes or
additional steps.
Portability
Python
code is easily portable from one OS to other. C++ code is not portable as it
must be recompiled if the OS changes.
Speed of Development
C++
program is compiled to the machine code. Hence, its execution is faster than
interpreter based language.
Python
interpreter doesn't generate the machine code. Conversion of intermediate byte
code to machine language is done on each execution of program.
If
a program is to be used frequently, C++ is more efficient than Python.
Easy to Learn
Compared
to C++, Python has a simpler syntax. Its code is more readable. Writing C++
code seems daunting in the beginning because of complicated syntax rule such as
use of curly braces and semicolon for sentence termination.
Python
doesn't use curly brackets for marking a block of statements. Instead, it uses
indents. Statements of similar indent level mark a block. This makes a Python
program more readable.
Static vs Dynamic Typing
C++
is a statically typed language. The type of variables for storing data need to
be declared in the beginning. Undeclared variables can't be used. Once a
variable is declared to be of a certain type, value of only that type can be
stored in it.
Python
is a dynamically typed language. It doesn't require a variable to be declared
before assigning it a value. Since, a variable may store any type of data, it
is called dynamically typed.
OOP Concepts
Both
C++ and Python implement object oriented programming concepts. C++ is closer to
the theory of OOP than Python. C++ supports the concept of data encapsulation
as the visibility of the variables can be defined as public, private and
protected.
Python
doesn't have the provision of defining the visibility. Unlike C++, Python
doesn't support method overloading. Because it is dynamically typed, all the
methods are polymorphic in nature by default.
C++
is in fact an extension of C. One can say that additional keywords are added in
C so that it supports OOP. Hence, we can write a C type procedure oriented
program in C++.
Python
is completely object oriented language. Python's data model is such that, even
if you can adapt a procedure oriented approach, Python internally uses
object-oriented methodology.
Garbage Collection
C++
uses the concept of pointers. Unused memory in a C++ program is not cleared
automatically. In C++, the process of garbage collection is manual. Hence, a
C++ program is likely to face memory related exceptional behavior.
Python
has a mechanism of automatic garbage collection. Hence, Python program is more
robust and less prone to memory related issues.
Application Areas
Because
C++ program compiles directly to machine code, it is more suitable for systems
programming, writing device drivers, embedded systems and operating system
utilities.
Python
program is suitable for application programming. Its main area of application
today is data science, machine learning, API development etc.
No comments:
Post a Comment