TechAlpine – The Technology world

How to write your first Python program?

Overview: Python is one of the most popular programming languages used for general purpose programming. It is open source, powerful, flexible and easy to learn/use/implement. Python contains powerful utilities and libraries for data processing, manipulation and analytics work. It is similar to C++, Java and Matlab, so developers can learn it easily if they have some experience in these languages. Now a day, Python is used widely in big data applications, analytics and quantitative programming.

In this article, we will cover technical details of Python and its importance in big data applications. We will also have a brief look at some of the success stories based on Python.

Some background of Python: Python was first conceived in late 1980s and implementation started in 1989 by Guido Van Rossum. It is a descendant of ABC language. The name ‘Python‘ is derived from ‘Monty Python’s Flying Circus‘ – a British television series. Python was developed as an open source (OSI-approved) project which can be used and distributed even in commercial environment. The philosophy of Python is to make the code more readable and write fewer lines of code for implementing concepts. It supports different programming paradigms like object-oriented, structured, procedural and functional etc.

Python 2.0 came in the market in the year 2000 and the version 3.0 was introduced in the year 2008. Python’s greatest strength is its standard library containing tools for performing various tasks. In Jan.2016, we have got Python Package Index, which is a third party software repository for Python. It contains more than 72,000 packages for various tasks.

Features of Python

In the above sections, we have got some idea about Python and its history. Now we will look into the features of Python which have made it popular for various applications including big data and analytics. Let’s check it!

Interpreted language: Python is interpreted language. It means, you as a developer do not need to compile the program and then run it. Rather, an interpreter will parse you program and generate the output. So, developing applications by using Python is much easier compared to other compiled languages.

Dynamically typed language:  The type of a variable in ‘Python’ is automatically defined. So, the developers do not need to write it explicitly. For example, if we write the following syntax, then the type of the variable ‘pyvar’ is automatically defined as ‘String’.

[code] pyvar = “Hello TechAlpine” [/code]

Now, if we change the line as shown below, then the variable is of type ‘int’.

[code] pyvar = 1 [/code]

As a result, it helps the development much easier.

Note: Python statement does not end with a semicolon (;)

Strongly typed language: Python is a strongly typed language. So the conversion from one data type to another data type is not allowed. If you write the following statement, then it will show an error. Because here the conversion from ‘int’ to ‘String’ it not done automatically. So, the developers need to convert it explicitly before concatenating them.

[code]

“TechAlpine is number” + 1 + “in e-learning space”

[/code]

Less code more use: The biggest advantage of Python is ‘code less and do more’. It means developers can perform the same task by writing less code compared to the other languages. So it helps rapid application development. For example, the following code snippet is sufficient to read a file in Python.

[code]

with open(“newFile.txt”) as file:

print(file.read())

[/code]

Multi paradigm programming support: Python supports different programming paradigms like object oriented programming, functional programming, structured programming and many more. So the developers can follow any programming style as per requirement.

Standard library: Python has a very rich standard library with various tools and APIs. It covers almost all the functionalities for application development.

Portable, extendable and scalable: Python can run on various hardware/software platforms. It can also be extended for customization or adding new features. Python is also scalable to support large volume of traffic.

How to download, install and setup Python?

In this article we will be using Python 3 version. In most of the Linux distribution, python 2 and 3 comes as a default installation. So, in those environments you do not need to install it separately. Following is the download link to install python.

Download link: https://www.python.org/downloads/

During installation in Windows environment, please check ‘Add Python.exe to path’. It will add the (.exe) file in the path automatically.

Python setup

Python setup

Image1: Select ‘Add Python.exe to path’ as marked

Once the installation is complete, open the command prompt and just type ‘python’ as shown in the picture below. It will show some details about the version, installation etc. Now you can ensure that the installation is successful and working fine.

Python command prompt

Python command prompt

Image2: Python command prompt

Now one more task is left before we move forward to taste one python application. Install any good text editor for writing python program. You can install notepad++ or any other editor of your choice.

Sample application: To run a python program, following are two different options

  • Typing python commands directly in python shell
  • Run program written is a file with (.py) extension

We will try a python program written in a file. But we will not write a simple ‘Hello world’ program; rather we will try one interactive application as shown below. First create a file named as ‘pyfirst.py’; it will have the code as shown below.

Listing 1: Sample python program (pyfirst.py)

[code]

print(” Welcome to TechAlpine —- Lets see first Python program”)

# This python application will add two numbers in an interactive way

# Get input numbers

number1 = input(‘Please enter your 1st number: ‘)

number2 = input(‘Please enter your 2nd number: ‘)

# Now adding two numbers

total = float(number1) + float(number2)

# Print the output

print (‘So the sum of {0} and {1} is {2}’.format(number1, number2, total))
[/code]

Now, run the script file from the command prompt. Following screen shot shows the output.

application output

application output

Image3: Python sample application output

Why Python is important in big data and analytics?

Python is a very powerful language and it has been used in different domains for a long time. It has very powerful libraries for performing data analysis tasks easily. It is also preferred to build scalable web applications. So, python is a preferred choice for big data and analytics applications which are mainly integrated with web applications.

Conclusion: In this article we have discussed about python, its back ground and also important features. We have also covered the download and installation part in details so that the developers can easily follow it and start their python application. At the end we have shown one sample application for better understanding. So, we can conclude that python is going to come in a big way as far as the big data and analytics applications are concerned. And it will also evolve with more tools and features.

How to write user defined functions in Python?

Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *


6 × = 54

TechAlpine Books
-----------------------------------------------------------