Thursday, 17 March 2016

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.

Python Language features:

The main thing that the new python programmer need to know about indention rule.
Indentation rules in Python.
There are no curly braces, no begin and end keywords, no need for semicolons at the ends of lines - the only thing that organizes code into blocks, MyFuns, or classes is indentation. If something is indented, it forms a block with everything indented at the same level until the end of the file or a line with less indentation.
While there are several options for indentation, the common standard is 4 spaces per level:

def isFieldExist (DBConn, MyList):
DBCursor = DBConn.cursor()   
    bAvailable = False       
    str_Command = "select * from " + MyList     
DBCursor .execute (str_Command)
    FieldNames = [f[0] for f in DBCursor .description]
for Field in FieldNames:
if "test" in Field:
            bAvailable = True
            bAvailable = False
    return bAvailable

Comments and Docstrings

To mark a comment from the current location to the end of the line, use a pound sign, '#'.

#This MyFun
is used to get the table names 
For longer comments and more complete documentation, especially at the beginning of a module or of a MyFun or class, use a triple quoted string. You can use 3 single or 3 double quotes. Triple quoted strings can cover multiple lines and any unassigned string in a Python program is ignored. Such strings are often used for documentation of modules, MyFuns, classes and methods. By convention, the "docstring" is the first statement in its enclosing scope. Following this convention allows automated production of documentation using the pydoc module.
This MyFun is used to get the table names

 In general, you use one line comments for commenting code from the point of view of a developer trying to understand the code itself. Docstrings are more properly used to document what the code does, more from the point of view of someone who is going to be using the code
Python is the sort of language that you can just dive into, so let's dive in with this example Python script:
#! /usr/bin/env python
""" An example Python script
    Note that triple quotes allow multiline strings
# single line comments are indicated with a "#"
import sys         # loads the sys (system) library
def main_MyFun(parameter):
    """ This is the docstring for the MyFun """
    print " here is where we do stuff with the parameter"
                print parameter
    return a_result     # this could also be multiples
if __name__ == "__main__":
    """ this will only be true if the script is called 
        as the main program """
    # command line parameters are numbered from 0 
    # sys.argv[0] is the script name
    param = sys.argv[1]    # first param after script name
    # the line below calls the main_MyFun and 
    # puts the result into MyFun_result

    MyFun_result = main_MyFun(param)

No comments: