Sunday, 27 March 2016

List in Python with code samples

List in Python:
The list is one of the data type available in Python. The items in the list are separated by comma values and which are placed between square brackets.
NOTE: The items in a list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square brackets. For example −
My_list1 = ['String', 'String2', 12, 232]
My_list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
My_list3 = ["a", "b", "c", "d", "e", "f", "g"];

Accessing Values in Lists

By using the square brackets for slicing along with the index we can access the values in the list or we can use indices to obtain value available at that index.

Friday, 25 March 2016

Dictionary in Python with code examples

Introduction to dictionary:
In the Dictionary each key is separated from its value by a colon (:), and the  items are separated by commas, the dictionary is enclosed in curly braces.
The dictionary is empty when there are no items in it. It is represented with just two curly braces {}.
The keys are unique within a dictionary and values may not be unique. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
Accessing Values in Dictionary:
To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. Following is a simple example −
if __name__ == '__main__':
    Ex_Dictionary = {'Name': 'Sandy', 'Age': 15, 'Class': 'tenth'};

    print "Ex_Dictionary['Name']: ", Ex_Dictionary['Name']
    print "Ex_Dictionary['Age']: ", Ex_Dictionary['Age']
    print "Ex_Dictionary['Class']: ", Ex_Dictionary['Class']

The output of the above code is
Ex_Dictionary['Name']:  Sandy
Ex_Dictionary['Age']:  15
Ex_Dictionary['Class']:  tenth

When we try to access the val;ues with the key that is not availabele in the above diracoty the below error may get appeared

Wednesday, 23 March 2016

Tuple in python with code examples

Tuple in python with code examples:

Tuples are sequences, just like lists in python. A tuple is a sequence of immutable objects.

Differences between the touple and list:

  • ·       The tuples cannot be changed unlike list.
  • ·        Tuples use parentheses, and lists use square brackets.

The below are the examples of the tuples:

EX_tup1 = ('String1', 'String2', 1919, 2100);

EX_tup2 = (1, 2, 3, 4, 5, 6, 7 );

EX_tup3 = "a", "b", "c", "d", "e", "f";

A tuple creating is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also.
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.
The empty tuple is written as two parentheses containing nothing
EX_tup1 = ()
To write a tuple containing a single value you have to include a comma, even though there is only one value

= (50,);

Accessing Values in Tuples:

Tuesday, 22 March 2016

Packages in Python with example

Packages in Python with example:

A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and sub packages and sub-sub packages, and so on.

Consider a file available in TestVal directory.

I am writing the below code in that file
def Test():
   print "I'm Test TestVal"
Similar way, we have another two files having different functions with the same name as above
·        TestVal/ file having function Isdn()
·        TestVal/ file having function FUN()

The in python:

Now, create one more file in TestVal directory
To make all of your functions available when you've imported TestVal, you need to put explicit import statements in as follows −

Monday, 21 March 2016

Locating Modules in Python with code examples

Locating Modules in Python with code examples:

When you import a module, the Python interpreter searches for the module in the following sequences −
·        The current directory.
·        If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.
·        If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.
The module search path is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default.


The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH.
Here is a typical PYTHONPATH from a Windows system:
set PYTHONPATH=c:\python27\lib;
And here is a typical PYTHONPATH from a UNIX system:
set PYTHONPATH=/usr/local/lib/python

Namespaces and Scoping:

Sunday, 20 March 2016

Modules in python

Modules in python with examples:

In python, modules play a vital role.

A module is a Python object with arbitrarily named attributes that you can bind and reference. A module can define functions, classes and variables. A module can also include runnable code.Python module allows us to logically organize our Python code. Grouping related code into a module makes the code easier to understand and use. In other words, a module is a file consisting of Python code.

Example for the module in python:

The Python code for a module named aname normally resides in a file named
Let us see the below module named as

def DUMP_func( Var ):

   print "Hello : ", Var

The import Statement in python:

Friday, 18 March 2016

Built-in Object types in Python with examples

Built-in Object types in Python with examples

Python also has built-in object types that are closely related to the data types mentioned above. Once you are familiar with these two sets of tables, you will know how to code almost anything!

Mutable sequence, always in square brackets: [1, 2, 3]
Immutable sequence, always in parentheses: (a, b, c)
Dictionary - key, value storage. Uses curly braces: {key:value}
Collection of unique elements – unordered, no duplicates
String - sequence of characters, immutable
Sequence of Unicode encoded characters

Python Operators:

Thursday, 17 March 2016

Branching, Looping, and Exceptions in Python with code examples

Branching, Looping, and Exceptions in Python with code examples


Python has a very straightforward set of if/else statements:

if ("4.0" in GC_Ver_1):               
                Finalsessionanme = os.path.dirname(SQLiteName) + "\\" + SQLite_Prefix +"_"+ sessiondate
            elif ("4.5" in GC_Ver_1):
        Finalsessionanme = os.path.dirname(SQLiteName) + "\\" + sessionanme

The expressions that are part of if and elif statements can be comparisons (==, <, >, <=, >=, etc) or they can be any python object. In general, zero and empty sequences are False, and everything else is True. Python does not have a switch statement.


Python has two loops. The for loop iterates over a sequence, such as a list, a file, or some other series:

Major differences between Python 2.x and Python 3.x

Major differences between Python 2.x and Python 3.x

In this post I am just defining the python versions and their differences
Python is available in two versions– Python 2.7 and Python 3.x, the current version of the python is 3.3.

The code that is implemented in one version will not work on another version. This is the major difference between the two versions. But most of the code is interchangeable

 The below are some of the key differences:
Python 2.x
Python 3.x
print “hello” (print is a keyword)
print(“hello”) (print is a MyFun)
except Exception, e: # OR
except Exception as e
except Exception as e:  # ONLY
Naming of Libraries and APIs are frequently inconsistent with PEP 8
Improved (but still imperfect) consistency with PEP 8 guidelines
Strings and unicode
Strings are all unicode and bytes type is for unencoded 8 bit values
In market there are some utilities are available to convert code form one version to another version. while the '-3' command line switch in 2.x enables additional deprecation warnings for cases the automated converter cannot handle.

Wednesday, 16 March 2016

Python overview and Python Features

Overview Of the Python:

Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.

·        Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP.
·        Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.
·        Python is Object-Oriented: Python supports Object-Oriented style or technique of programming that encapsulates code within objects.
·        Python is a Beginner's Language: Python is a great language for the beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.