Introduction to Python
Python is a very popular high-level programming language that supports multiple programming paradigms. You use it for object-oriented, functional, procedural, imperative and reflective programming.
It was created by Guido Van Rossum as a pet project during the late 1980s and early 1990s. Initially, Python was not open-source, but later the source code for Python was released under the GNU GPL license.
Over the last 26 years, the language has evolved a lot. There are two major flavors of the language:
- Python 2.x
- Python 3.x.
Python 2.0 was released in the year 2000. It had major features like support for Unicode and a nice garbage collector for optimal memory management. Python 3.0 was released in 2008, but it is not backward compatible with Python 2.0.
To make sure that the features of Python 3.x are available for Python 2.x, various releases of the latter were done after the first release of the former with some features backported to 2.x versions. These changes were done in phases so that code written in Python 2.x could be easily run using Python 3.x before the "end of life" of Python 2.x is reached.
Features of the Python Programming Language
Since the language uses English keywords frequently and defines scope based on indentation, code written in Python is highly readable. Some of the salient features of the language are:
Interpreted and interactive language
Python interprets the code at runtime. It supports writing code directly onto the interpreter console. Alternatively, we can add our code into a Python module and feed that file as input to interpreter. A Python module is a file with .py extension that contains code written in Python. For instance, if you have a file named Test.py, we can run the code using:
python Test.py
The output for the above code will be printed to the console.
Supports many paradigms
Python supports creating Objects that encapsulate code. But it is possible to write code that consists of functions only and no objects and you can still run the code.
Easy to learn
Python has few keywords and is a complete beginner’s programming language. The syntax is clearly defined and simple, making it easier for a newbie to learn it.
Better readability and maintainability
Unlike other programming languages like C, C++, Java, etc, the scope of code is not defined using curly braces. Instead, it is defined by indentation. This should ensure that developers can read through the code without much effort.
I don't agree that much with this. Most people using other languages like Java also structure their code for readability and use indentation. The curly braces make it a little more verbose maybe, but it's still very readable.
For example, a simple for loop in Java to iterate over a list of strings and print each of them would be:
for(String s: strList) {
System.out.println(s);
}
In case of Python, the same piece of code will be written as follows:
for s in strList:
print(s)
Note that if the print(s) line is treated as a separate statement outside for loop if the indentation is not proper. This makes sure that the code will be readable at the end.
Python code can be organized properly and spaghetti code can be avoided. This adds to better maintainability of code written using Python.
Availability of a standard built-in library
A very broad built-in library is available for the language. The library files are again written in Python. These files can be found under /Lib.
Portable
Code written using an interpreter on a Windows machine is runnable on a Linux or Mac environment
Extendable
It is possible to add low-end modules to the language as add-ons for specific applications making it easier for developers to customize tools available with the language.
Scalable
It supports a large number of files than shell scripting.
Uses for Python
Some of the major areas in which Python is used are:
Application Programming
Since the language is scalable, Python was the language of choice for building many applications—be it for desktop, small devices or cloud.
Database and GUI programming
Python provides interfaces for almost all major databases and supports the creation of GUI tools that can be run in Windows, Linux or Mac OS.
Scripting
Python is used for scripting while building large applications with scale-out architecture.
Testing
Python is used to build test frameworks that can be integrated with Continuous Integration Builds in many large organizations. Tools like Robot Framework can be used for this purpose.
Cloud computing
Since the language is scalable, it is used in cloud computing. A very good example would be OpenStack which is a cloud-based operating system fully written using Python.
Setting Up The Python Development Environment
To install, we need to download the binaries or the installers from the official Python website link, depending on the operating system that we use.
Installation and Set Up on Windows
Download and install Python as indicated in the previous section. After installation. access system properties.
Under the "Advanced" tab of the System Properties dialog box, click on Environment Variables.
Click on the system variable named Path and Select Edit.
Add the path to python installation folder here and save it.
Open a new command prompt window and type python. It should show Python interpreter console where we can directly add the code.
Installation and Set Up on Linux
In Linux, the .bashrc file can be edited to include the following:
export pyhome=/usr/bin/python export path=$path:$pyhome
After saving the environment variables and logging in again, simply typing python onto a new terminal should launch the python interpreter console where we can directly write code.
Python Development Using Eclipse
Since Python is an interpreted language, we can directly code into the interpreter console and see the output for each statement that we add into the console.
To develop large applications, we may want to use another application for ease of organizing the code and working with multiple files. The application can be a text editor like Notepad++ or Sublime Text. It can also be an IDE like Eclipse or IntelliJ.
Let us see how Eclipse can be used for Python development.
Prerequisites
The latest PyDev plugin available in Eclipse Marketplace works with Eclipse Oxygen and Eclipse Neon. It would also be great if JRE 1.8 is installed as Eclipse uses javaw.exe available in the JRE folder for running the application.
Installing Pydev in Eclipse IDE
On the Eclipse menu bar, select Help -> Eclipse marketplace.
Search for Pydev in Eclipse Marketplace and choose install.
Follow the steps in the next few windows, accept the license agreement and select all the features as follows to install.
After installation, we need to add python interpreter to Eclipse preferences so that python code can be run in the console.
After the path is set correctly, the preferences should look as follows:
Create Our First PyDev Project
Let us create our first Python project in eclipse.
Add a new package to the project.
Create a new .py file under the package.
Add the following code to the new python file:
print("Hello World!")
The new Python file can be run by simply right-clicking on the editor window and selecting Run As -> Python Run.
This should print “Hello World!” to the console.
© 2019 Sam Shepards