Introduction to Python Web Development
Python Web Development can be defined as a series of processes followed for the creation of a web based application. Python is one of the well-known object oriented programming language used for the application development of a web based application or a website. Coding in python is pretty straightforward, that makes it easy to learn and practice even for an inexperienced person. A few notable features of python web development are pre-defined functions and methods for almost all frequently used operations, manipulation of data is flexible using the data related functions like list, collection and dictionaries, the built-in library is complete to fulfil the integration functions, etc.
Python Programming Language
Python is used everywhere. To be more accurate, there is hardly any programmer who does not know the importance of python web developement. Google uses it in maintaining its 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 web development 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 that 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)
Perl Programming Codes
Perl is for high performance, high-level decoding and dynamic programming language. Let us look at the codes below.
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.
Scripting Language – Python Web Development
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! I am not exaggerating. It is the truth. And this is how Python came up to the top.
Features of Python Programming
So now we know, how python made its place to the top position in developing web applications. Let’s take a deeper look at that. Following are some of the most important features that make python more flexible and sustainable for the 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 knows 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 with 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, the 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.