Monday, 6 August 2012

Ch 2: The Basics


I hope by now you have a Python command line interpreter up and running on your system. Well if you don't then please download the current stable version of Python from here.

Now, lets quickly get to know the basics of Python language. Unlike any other languages the Python language comes without much too much of fanfare and fuss. The syntax is neat and easy to comprehend. Lets start with the python command line interpreter. If you are on a Linux machine just enter python at the shell prompt to initiate the python command line. For Windows users, you can run the interpreter in the command line if you have set the PATH variable appropriately. Alternatively, you can use the IDLE program. IDLE is short for Integrated DeveLopment Environment. Click on Start -> Programs -> Python 2.7 -> IDLE (Python GUI). The command line interpreter executes the current command on the command line i.e. one line at a time. For the time being lets play around the python command line to get it's feel before jumping in to create a full fledged program.


On a Linux machine Python command line interpreter looks something like this.

neo@neo:~$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>


Lets print HELLO WORLD.

neo@neo:~$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>print "HELLO WORLD"
HELLO WORLD
>>>



You can try things on your own like printing numbers, performing operations on numbers and displaying the result etc.

neo@neo:~$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>print "HELLO WORLD"
HELLO WORLD
>>> print 34
34
>>> print 3 + 7
10
>>> print 11 % 5
1
>>> print 10 * 5
50
>>>



Printing multiple lines using triple quotes """.

neo@neo:~$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print """The two roads diverged in a wood, and I-
... I took the one less traveled by,
... And that has made all the difference."""
The two roads diverged in a wood, and I-
I took the one less traveled by,
And that has made all the difference.
>>>




Understanding Variables

Unlike many other languages that requires the variables to be initialized to their correct data type, Python initialize the variable to it's correct data type automatically. That means the variables need not be declared before using them to store data. However there is a catch. Consider the last statement. What do you think should be the output?



neo@neo:~$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> int(a)
17
>>> a = 17
>>> b = 23
>>> print a + b
40
>>> c = a + b
>>> print c
40
>>> a = a + 1
>>> print a
18
>>> x = x + 1



In last statement the evaluation takes place from left to right of the expression. The statement adds 1 to the current value of x and stores the result in the variable x. However, the variable x is not defined prior to this statement. The Python runtime raises an error.



>>> x = x + 1
Traceback (most recent call last):
File "", line 1, in
NameError: name 'x' is not defined
>>>



Following are the definitions of some of the different data-types.

>>> i = 38 #integer type
>>> f = 12.25 #float type
>>> l = 980089 #long type
>>> d = 1098827.9032 #double type
>>> s = "hello" #string type
>>> b = True #boolean type(either True or False)
>>>



No comments:

Post a Comment