Python is a powerful high-level scripting language. It is amongst one of the most popular languages that is widely used in many organizations, like Google, Yahoo, NASA, etc. Besides commercial applications, Python is exhaustively used for research-based projects as well.
Python allows us to write programs quickly since it is dynamically typed, has a simpler syntax, and is interpreted. This makes Python a good programming language for software that have aggressive development timelines (which is true for most of the software!) or for writing software prototypes.
Python has a special emphasis on making the code readable. As an example, it uses space indentation to identify blocks instead of a pair of parenthesis (as in C or C++). Often software is handled by multiple programmers -- it is written by one set of developers and is maintained/enhanced by another set of programmers. Python's clear syntax enables new programmers to read, maintain, and enhance the existing code.
However, since Python is interpreted at run time, it runs slower than C or C++. Hence, this factor must be considered when using the same. Fortunately, Python has the ability to integrate C code well and so one can always design a software such that performance-critical components of the software can be written in C, where as the rest of the software can be written in Python.
There are two ways to run a Python program. The first way is to simply invoke the Python executable in an interactive mode and input statements. The second way and certainly the popular style, is to keep the Python code in a file and then pass the file as an argument to the Python executable. Since Python is run using an interpreter, there is no need to compile these programs!
Now, let us take a look at the first way. On a Linux box, we can invoke Python using the command "python3" and simply pass Python instructions to it; python3 is the latest 3.x version of Python. We can use quit() or Ctrl-D (i.e. EOF) to exit from the interpreter.
[user@codingbison]$ python3 Python 3.2.1 (default, Jul 11 2011, 18:55:33) [GCC 4.6.1 20110627 (Red Hat 4.6.1-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> print("Hello World") Hello World >>> >>> quit() [user@codingbison]$
In the second (and the most common) style, we can keep the Python program in a file and pass the file as an argument to the Python executable. If there are errors, then the Python interpreter will detect them during the run-time. We can keep Python statements in a file (let us say, "hello.py") and pass the file as an argument to the python executable. Here is an example:
[user@codingbison]$ cat hello.py #This is a simple "Hello World" program. print("Hello World") [user@codingbison]$ [user@codingbison]$ python3 hello.py Hello World [user@codingbison]$
The above style is common, since except for some quick reference, it is cumbersome to type a Python program in the interpreter every time. For complex programs, the only (acceptable) way is to keep the code in a file and then run it as and when needed.
The first line in the above example starts with a "#" sign; such lines are Python comments. It is a good idea to provide useful comments that explain the software logic.
Python has come a long way! The latest version of Python is 3.x (Python 3.2.0 was released in Feb, 2011). With Python release 3.x, there would not be any new releases for the earlier versions (Python 2.x). However, support would continue to exist for version 2.x since Python 2.x still remains the most commonly used version.
If you are writing a software from scratch and you don't anticipate dependency on existing libraries, then using version 3.0 is a good idea. This is because it is likely that existing libraries might be written in version 2.x and you might run into backward compatibility issues; version 3.0 is an intentionally incompatible release! Needless to say, if the dependent library is already written in version 3.0, then this would not be an issue.
As an example, one of the changes with Python 3.0 is that the print statement has been made as a function. With Python 2.x we can use print as a statement but not with Python 3.x. Note that the print function works in Python 2.x as well since it has been back-ported!
[user@codingbison]$ python Python 2.7.3 (default, Apr 13 2012, 20:15:24) [GCC 4.6.3 20120306 (Red Hat 4.6.3-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> print "Hello World" Hello World >>> >>> print("Hello World") Hello World >>>
But referring to the print as a statement would simply be an error with Python 3.x.
[user@codingbison]$ python3 Python 3.2.1 (default, Jul 11 2011, 18:55:33) [GCC 4.6.1 20110627 (Red Hat 4.6.1-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> print "Hello World" File "<stdin>", line 1 print "Hello World" ^ SyntaxError: invalid syntax >>>
Here is a Python Docs URL that outlines changes done with version 3.0 as compared to version 2.x: http://docs.python.org/py3k/whatsnew/3.0.html . If you find the terms/concepts used in above link unfamiliar, then you can always revisit the link once you have completed going through this module.