Switching To Python From Another Programming Languages

I have been asked many times by my peers, How difficult is it to code in Python? but the answer is quite the opposite. Switching to Python is very simple. If you already know how to code in any programming language then learning Python is a piece of cake. I will be using examples based on Linux distribution as the base operating system, why? because I <3 Linux. 

Before we start, if you are coming from a different programming language which needs you to write class and methods for its execution like Java, C or even C++ then let me tell you Python doesn't need those ways for its execution but you definitely can make classes and methods to ease your work. 

Getting started is very easy by using terminal and typing simply python for direct executing commands in the shell or creating a <filename>.py file which can be used as a script and can be re-executed n times. The following command will start Python shell in your terminal.

$ python

or you can create a Python file with the following command and use any text editor as in this case vim to edit and save the file.

$ touch example.py
$ vim example.py

Indentations are very important in Python, unlike other languages it does not use {} brackets to define a block of code. This makes code easier to look and understand. Let us take a small example of calculating factorial of a number by two different approaches i.e without method creation and the same using classes and methods. Below is a small example of calculating factorial of a number without any method declaration. 

result = 1
no = 5
for x in range(1, no + 1):
    result *= x
print(result)

The first two lines simply declare and define a variable called "result" and "no" with an int value of 1 and 5 respectively where variable "result" will be used to store the result of the factorial and variable "no" defines the number of which factorial is to be taken. As you can see, we do not need to declare the type of data we are assigning. Python already takes care of it for you. The third and fourth line is a loop block where the structural difference is visible from other languages. The third line defines the condition for variable "x" which will take values in the range of 1 and no+1 i.e 1 <= x < no+1 and fourth line is used to calculate the product of all numbers in the range defined and store it in the variable "result". The last line simply prints the value of variable "result" in the terminal.

To run the file we simply execute the following command in the terminal and the result is printed as intended in the following line.

$ python example.py
120

If you are familiar with classes and objects like in Java then next approach is going to be very easy to understand. We will make use of a constructor to initialize and print the answer of factorial and a simple recursive function to calculate the factorial of a number. Below is the example of calculating factorial of a number using classes and methods.

class factorial(object):
    no = 0
    product = 1

    def __init__(self, no):
        self.no = no
        product = self.get_factorial()
        print(product)

    def get_factorial(self):
        if self.no < 1:
            return self.product

        self.product *= self.no
        self.no -= 1

        return self.get_factorial()

factorial(5)

The first line simply declares the class with name factorial which is inheriting a super class named object. A standalone class in Python >= 2.7 needs to inherit object as a super class but in the case of multiple classes where you want to inherit another class then that is also fine and has to be done in a similar way. The second and third line simply declares and defines a value to a corresponding variable. A method in Python is defined by the syntax "def <method_name>" where the first argument has to be self which is a reference to the current instance of the class similar to "this" in Java as seen on the fourth line. However, the word self is not reserved by Python but is a strong convention to be followed by developers. Accessing or using a global variable or a method of the current instance of the class has to be referenced by the word self (as taken in the parameter of the function) which can be observed on the line fifth and sixth. 

We have created another method called "get_factorial" which is a recursive method to calculate the factorial and return the answer. As you can observe we do not have to declare the function return data type as we are required to do in Java. The last line of the code simply initializes the class factorial with parameter "5" of which factorial has to be calculated.

If you want to go through some good Python tutorial, click here. It covers most of the basic to advanced sections in Python with examples, so you can simply follow the tutorial and learn Python within few days. I hope it encourages more developers to switch to Python which I feel is better than many other languages and can be used for any type of application environment IOT, desktop or web applications. It also has an awesome support of third party libraries, tools, and framework. Comment below if you want me to cover some topics or frameworks in Python in the future.