Image Source: pixabay.com
Hi Folks. So today I would be talking about the infamous Python. If you are a programmer, then I don’t think I need to explain the importance of python in our day-to-day programming. But, assuming that there will be people who don’t know anything about python, and since this blog targets at both noobs and experts, I think I would need to explain the importance of it and how often it is used and why.
If you are already into programming and know the basic application of python on the web, then you can skip to the second section. And no pun intended, but if you think that being a noob is insulting, then trust me it is not. In our world filled with technology, I think everyone is a noob about some or the other thing. I too, inspite of being in the technological world for more than 5+ years, I still consider myself as a noob. Because there is always something that one does not know or one can learn. So, should we get started?
Python is used everywhere. To be more accurate, there is hardly any programmer who does not know the importance of python. Google uses it in maintaining their own database. Hard coders use it in combination with Linux since its extremely compatible with Linux and you can even cross-create a program written half in bash and half in python. Besides creating databases, it is used for creating high end games, for creating CGI Effects in Movies and Animations.
If you think that is it, it’s not. It’s even used for creating Gnome applications, for automated tasks in UNIX and Linux. The reason for python being so explicitly used is because it is extremely User-friendly. It’s also easy for a beginner to understand a code written in python, because it has a specific format in which it needs to be written.
Unlike Ruby or Perl, python needs to be coded in a proper way. If you have any experience in ruby or Perl, then you know it can be a mess since it’s an extremely forgiving language. But it’s not the same in case of Python. Python needs to be indented in a proper way. That is the reason all Perl web applications are being replaced by python nowadays.
Most likely, by the end of this decade, Perl will be completely replaced by python. Here is an example I have written to calculate compound interest in both Python and Perl. Mess yourself up and check which one do you find easier.
Calculating compound interest in Python:
So, before I proceed, Compound interest is that type of interest which accrues over a period of time on the initial amount and the accumulated interest of a principal deposited. In Compound interest, interest grows faster than Simple Interest. The following is the basic formula for calculating Compound Interest:
Compound Interest = Principal (1+ Interest/100)^Period
In the following Code:
P = principal
R = Rate of Interest
T = Time duration
>>>print ': Compound Interest :'
amount = input('Enter the principal amount')
amount = float(amount)
rate = input('Enter rate percentage')
x = input('Press \'1\' for duration of time in days \n\'2\' for time in months and \n\'3\' for time in years\n')
if(x == 1):
time = input('Enter number of days')
time = time /(12*30)
elif(x == 2):
time = input('Enter number of months')
time = time / 12
time = input(' Enter number of years')
total_amount = (amount * (1 + (float(rate)/100))**time)
print('\nTotal Amount is %f' %total_amount)
compound_interest = total_amount - amount
print('\nCompound Interest = %f' %compound_interest)
print('\nTotal amount = %f' %total_amount)
So, that was for python. Now lets take a look at a Perl code:
print “The principal amount is $principal\n”;
print “The annual interest rate is $percent percent\n”;
print “The time duration of the investment is $time\n\n”;
print “The future value of the investment is $futurevalue\n\n”;
So, it may seem Perl code is smaller than python, but python is much easier to understand. And there are other ways in which it can be written in a much more compact way. Thus Python exceeds Perl in every other way.
Now you may be thinking, maybe I have gone off-topic just to describe the importance of python. Nope, I didn’t. Previously Perl was the most used language, even on the web. But today, it has been replaced by Python and I just wanted to convey my message about the same.
And Now, it’s about time that we see how python has squeezed the web in the past one Decade.
The hardest part is using C as a beginner language. If you are a noob, 90% of the C stuff will go over and above your head. And, by the time you understand the basics in C, the other person will already have learned python, ruby and Perl. Yeah! Am not exaggerating. It is the truth. And this is how Python came up to the top.
So now we know, how python made its place to the top position in developing web applications. Let’s take a deeper look into that.
Following are some of the most important features that makes python more flexible and sustainable for long term in real day life:
1. Swapping variables
>>> a, b = 1, 2
>>> a, b = b, a
2. Slicing and Negative Indexing
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[-4: -2]
3. Naming Slices
>>> a = [0, 1, 2, 3, 4, 5]
>>> LASTTHREE = slice(-3, None)
slice(-3, None, None)
>>> a[LASTTHREE] [3, 4, 5]
4. Zip and Iterators
>>> from itertools import islice
>>> def n_grams(a, n):
... z = (islice(a, i, None) for i in range(n))
... return zip(*z)
>>> a = [1, 2, 3, 4, 5, 6] >>> n_grams(a, 3)
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)] >>> n_grams(a, 2)
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] >>> n_grams(a, 4)
[(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)]
5. Generator Expressions
>>> g = (x ** 2 for x in xrange(10))
>>> sum(x ** 3 for x in xrange(10))
>>> sum(x ** 3 for x in xrange(10) if x % 3 == 1)
6. Queue with Maximum Lengths
>>> last_three = collections.deque(maxlen=3)
>>> for i in xrange(10):
... print ', '.join(str(x) for x in last_three)
0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
4, 5, 6
5, 6, 7
6, 7, 8
7, 8, 9
Now these are some of the most basic but important know to stuff which you would be using most of the timing to make your programs efficient and compact. Besides, if you are trying to make python work with the web, then here are some important point you might consider gathering information on:
1. Setting up CGI (Common Gateway Interface) on your own Server
2. Learning the difference between mod_python and mod_php
3. Learning Libraries which help integrate Python and HTML and creating templates on that basis
4. Learning Django Framework or TurboGears
So, above are my experiences shared to you guys in detail. And trust me when I say Django is the best and widely used. If you know Django, then I don’t think there would be a need to learn TurboGears, reason being Django covers every aspect of TurboGears and might probably even replace Turbo in a few years.
Here are some articles that will help you to get more detail about the Python Squeezes the Web so just go through the link.