Tuesday, May 12, 2009

Commenting in Python
 
Single Line Comments:
 
"#" is used to  any line python programs.  Whenever Python finds a "#" in a line (except it is inside a string), the line from that character will be treated as comment. The comment ends with the line.  This is as same as "//" comment in CPP.
 
Examples:
>>> print "Hello" # Comment1
>>> # entire line is comment here.
>>>
 
 
Multi Line Comments: 
 
Multi line comments can be placed between """ (three single/double quotes).  See example below. The lines between these set of quotes are treated as comments.  
There is one more difference between single and multi line comments in Python.  That is multi line comments are not encrypted in the compiled/interpreted runtime file(.pyc).  So users can read them by simply opening the .pyc file.  But single line coments are not readable from .pyc files.  Thats why generally multi line comments are used for documentation/Help. 
Take care while using multi line comments, these should always start in a new line,  other wise this will be treated as strings.
 
Examples:
test.py
-------
""" This program demonstrates the multi line comment usage.
the code do nothing except assigning a value to a variable and printing back.
Run the code with following command
Python test.py"""
a = 10
print a
 
Summary:
~ Two types of commenting available in Python
~ Single Line with # or Multi Line with """ or '''
~ Multi Line comments are generally used for documentation or help because these are available even in runtime/compiled .pyc files.
~ Always start a multi line comment in a new line
 

Wednesday, May 06, 2009

Python Variables -- Declaring & Using
 
Variable is a name for a memory location where we store/keep some value. It is very easy to work with variables in Python.  Python does not need any "Declaration" of variables before using them.  Python is "not stongly typed".  I mean, in a languages like C/Jave we need to declare a vairiable specifing the type of the variable before using it.  In Python a variable is created whenever some value is assigned to it.  The type of variable is also dynamic.  We can assign any type of value (string/number/list/etc.) to the same variable in the same program.  This gives a great flexibility of using variables, but sometimes wrong values can be assigned to a wrong vairable.  Here are some points to note about variables:
 
~ No Declaration needed, assign value to create a variable
~ Any type of value can be assigned to a variable
~ Alphabets (a..z, A..Z), Digits(0..9) and underscore (_) can be used in the variable names.
~ Variable names should start with alphabets or underscore.
~ Don't know how long a variable name can be. 
~ Variable names are case sensitive
~ There is some special meaning of using underscores at the starting of a vairable name, we will discuss that in later posts.
~ Can not refer a variable without creating it.
 
Examples:
 
>>> a = 10
>>> print a
>>> a = 'Python'
>>> print a
>>> a = [1,'a',2,'b']
>>> print a

Sunday, May 03, 2009

Python Data Types

Now we will see what data types does Python supports, the list is here.   The 2nd part of the post talks about how to declare and use the variables.  Please read till the end if you want to do any experiments with different data types. 

1.     Numbers 

This data type is used to work with numbers. I don;t call this as Integer data type, as many books say.  The number data type supports both whole numbers (Integers) and also the real numbers. There are no different data types for integers and real number.  It is easy to define a number variable,  just assign the number as shown below
>>> i = 10
>>> r = 1.2
>>> print i*r

2.     Strings

This data type is used to work with text.  There is no special data type in python to declare characters.  Strings are enclosed with in single or double quotes both works in python.  But do not start with single quote and end with double or vice versa. Both these quotes can be clubbed and used to if a string contains a quote, for example "What's you name?" . 

~The same thing can be done by using escape character also.  \ (back slash) works as a escape character in Python.  For example "What\'s your name?",  In the same way below escape sequences can be used inside strings 
\n -- new line
\t -- tab
\' -- Single Quote
\" -- Double Quote
\\ -- Back Slash 

~Multiple lines also can be specified using triple quotes like below (it is easier if you don;t want to use \n)
"""1st line
   2nd line
   3rd line"""

~If you are working with very big string,  you can type the string in multiple lines with out breaking the string in different line like shown below by using \ at the end of the line.
>>> bigstring = "hello world!!! \
                      Python is a good language" 

~Raw Strings are used if you do not want to process a string for any escape characters.  A raw string holds the characters that you typed.  Raw strings are defined as below, an r plus quote. 
>>> rawstring = r"\n is used for new line characters"

~Unicode Strings are used if the string contains any special character sets like French, Hindi, Arabic, etc. Unicode strings are defined with u plus quotes. 
>>> unistring = u"This is unicode string."

We will talk about the string functions in coming posts.   

3.     Lists

This is a new data type for C/C++ programmers, but It works like a dynamic array. Dynamic means we can add/remove elements whenever required using it's built-in functions and you can add any type of values in the same list.  Find the below examples to see how to use lists.   Individual list elements can be accessed as same as array elements using the index value like ListVar[0].

Ex: >>> ListVar = ['abc', 'pqr', 'xyz', 123]
     >>> print ListVar

4.     Dictionaries

Dictionary type variable contains set of keys and their values.  It is like a phone book, name-number (of course one can have more numbers :) )  Find the below example how dictionaries can be defined. We can retrieve values by using by keys, but not in the other way.  

Ex: >>> DictVar = {"
India":"NewDelhi", "Sweden":"Stockholm", "Denmark":"Copenhagen"}
     >>> print DictVar["
India"]

~ Can not have duplicate keys. 
~ Can add new key-values at any time.
~ keys are case sensitive. 
~ these can be sorted on keys. 

5.     Tuples

Tuple is an immutable list.  This data type is almost same as List,  but it does not allow any addition/deletion/modification of tuple elements once a tuple is defined.  It is like const keyword in C/C++ language.  Tuple is defined also in the same way as List,  but instead of square braces normal braces should be used like below.   Tuples are faster than lists. 

Ex: >>> TupVar = ('xyz','pqr')
     >>> print TupVar