Python Programming Language
Python Programming Language
functional programming;
Python has top the charts in the recent years over other programming languages like
C, C++ and Java and is widely used by the programmers. The language has
undergone a drastic change since its release 25 years ago as many add-on features
are introduced. The Python 1.0 had the module system of Modula-3 and interacted
with Amoeba Operating System with varied functioning tools. Python 2.0 introduced
in the year 2000 had features of garbage collector and Unicode Support. Python 3.0
introduced in the year 2008 had a constructive design that avoids duplicate modules
and constructs. With the added features, now the companies are using Python 3.5.
Interactive
Interpreted
Modular
Dynamic
Object-oriented
Portable
High level
It provides large standard libraries that include the areas like string operations,
Internet, web service tools, operating system interfaces and protocols. Most of the
highly used programming tasks are already scripted into it that limits the length of
the codes to be written in Python.
Integration Feature
The language has extensive support libraries and clean object-oriented designs that
increase two to ten fold of programmer’s productivity while using the languages like
Java, VB, Perl, C, C++ and C#.
Productivity
With its strong process integration features, unit testing framework and enhanced
control capabilities contribute towards the increased speed for most applications and
productivity of applications. It is a great option for building scalable multi-protocol
network applications.
Limitations or Disadvantages of Python
Python has varied advantageous features, and programmers prefer this language to
other programming languages because it is easy to learn and code too. However, this
language has still not made its place in some computing arenas that includes
Enterprise Development Shops. Therefore, this language may not solve some of the
enterprise solutions, and limitations include-
As compared to the popular technologies like JDBC and ODBC, the Python’s
database access layer is found to be bit underdeveloped and primitive. However, it
cannot be applied in the enterprises that need smooth interaction of complex legacy
data.[2]
Unlike human languages, the Python vocabulary is actually pretty small. We call
this “vocabulary” the “reserved words”. These are words that have very special
meaning to Python. When Python sees these words in a Python program, they have
one and only one meaning to Python. Later as you write programs you will make up
your own words that have meaning to you called variables. You will have great
latitude in choosing your names for your variables, but you cannot use any of
Python’s reserved words as a name for a variable. When we train a dog, we use
special words like “sit”, “stay”, and “fetch”. When you talk to a dog and don’t use
any of the reserved words, they just look at you with a quizzical look on their face
until you say a reserved word. For example, if you say, “I wish more people would
walk to improve their overall health”, what most dogs likely hear is, “blah blah blah
walk blah blah blah blah.” That is because “walk” is a reserved word in dog
language. Many might suggest that the language between humans and cats has no
reserved words1 . The reserved words in the language where humans talk to Python
include the following:
That is it, and unlike a dog, Python is already completely trained. When you say
“try”, Python will try every time you say it without fail. We will learn these reserved
words and how they are used in good time, but for now we will focus on the Python
equivalent of “speak” (in human-to-dog language). The nice thing about telling
Python to speak is that we can even tell it what to say by giving it a message in
quotes:
And we have even written our first syntactically correct Python sentence. Our
sentence starts with the function print followed by a string of text of our choosing
enclosed in single quotes.
Not surprisingly, strings belong to the type strand integers belong to the type int.
Less obviously, numbers with a decimal point belong to a type called float, because
these numbers are represented in a format called floating-point.
What about values like '17'and '3.2'? They look like numbers, but they are in
quotation marks like strings.
Variables
One of the most powerful features of a programming language is the ability to
manipulate variables. A variable is a name that refers to a value. An assignment
statement creates new variables and gives them values:
This example makes three assignments. The first assigns a string to a new variable
named message; the second gives the integer 17to n; the third assigns the
(approximate) value of π to pi. A common way to represent variables on paper is to
write the name with an arrow pointing to the variable’s value. This kind of figure is
called a state diagram because it shows what state each of the variables is in (think
of it as the variable’s state of mind). Figure 2.1 shows the result of the previous
example. The type of a variable is the type of the value it refers to[3].
Variable names and keywords
Programmers generally choose names for their variables that are meaningful—they
document what the variable is used for. Variable names can be arbitrarily long. They
can contain both letters and numbers, but they have to begin with a letter. It is legal
to use uppercase letters, but it is a good idea to begin variable names with a lowercase
letter (you’ll see why later).
The underscore character, _, can appear in a name. It is often used in names with
multiple words, such as my_nameor airspeed_of_unladen_swallow. If you give a
variable an illegal name, you get a syntax error:
76trombonesis illegal because it does not begin with a letter. more@is illegal
because it contains an illegal character, @. But what’s wrong with class? It turns out
that classis one of Python’s keywords. The interpreter uses keywords to recognize
the structure of the program, and they cannot be used as variable names. Python 2
has 31 keywords:
A statement is a unit of code that the Python interpreter can execute. We have seen
two kinds of statement: print and assignment. Technically an expression is also a
statement, but it is probably simpler to think of them as different things. The
important difference is that an expression has a value; a statement does not [1].
Functions Not all operators use the binary operator syntax. An alternative syntax is
termed the function call notation. In this notation the name of the operation is given
first, followed by a list of the arguments surrounded by parenthesis. For example,
the abs operation returns the absolute value of the argument:
>>> abs(-3)
>>> abs(2 – 3 * 7)
# first calculate 2-3*7, which is -19 19
The function len returns the number of characters (that is, the length) of a string
>>> len(‘abc’)
>>> len(‘ha’ * 4)
NumPy and SciPy are open-source add-on modules to Python that provide common
mathematical and numerical routines in pre-compiled, fast functions. These are
growing into highly mature packages that provide functionality that meets, or
perhaps exceeds, that associated with common commercial software like MATLAB.
The NumPy (Numeric Python) package provides basic routines for manipulating
large arrays and matrices of numeric data. The SciPy (Scientific Python) package
extends the functionality of NumPy with a substantial collection of useful
algorithms, like minimization, Fourier transformation, regression, and other applied
mathematical techniques.
If you installed Python(x,y) on a Windows platform, then you should be ready to go.
If not, then you will have to install these add-ons manually after installing Python,
in the order of NumPy and then SciPy. Installation files are available for both at:
http://www.scipy.org/Download
Follow links on this page to download the official releases, which will be in the
form of .exe install files for Windows and .dmg install files for MacOS. [5]
Importing the NumPy module
There are several ways to import NumPy. The standard approach is to use a simple
import statement:
However, for large amounts of calls to NumPy functions, it can become tedious to
write numpy.X over and over again. Instead, it is common to import under the briefer
name np:
The central feature of NumPy is the array object class. Arrays are similar to lists in
Python, except that every element of an array must be of the same type, typically a
numeric type like float or int. Arrays make operations with large amounts of numeric
data very fast and are generally much more efficient than lists. An array can be
created from a list:
>>> a[:2]
array([ 1., 4.])
>>> a[3] 8.0
>>> a[0] = 5.
>>> a
array([ 5., 4., 5., 8.])
Arrays can be multidimensional. Unlike lists, different axes are accessed using
commas inside bracket notation. Here is an example with a two-dimensional array
(e.g., a matrix):
Conditionals
The if construct
executes a block of statements (which must be indented) if the condition returns true.
If the condition returns false, the block skipped. The if conditional can be followed
by any number of elif (short for “else if”) constructs
can be used to define the block of statements which are to be executed if none of the
if-elif clauses are true. The function sign of a below illustrates the use of the
conditionals.
Running the program results in the output
Loops
can be used to define the block of statements which are to be executed if condition
is false. Here is an example that creates the list [1, 1/2, 1/3,...]:
The output of the program is
Plotting in Python
import numpy as np
import matplotlib.pyplot as plt
[3]. Allen Downey, “Think Python”, Green Tea Press, Needham, MA, USA
[4]. Timothy A. Budd, “Exploring Python”, PythonAnywhere.com.