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:
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 = Falsestr_Command = "select * from " + MyListDBCursor .execute (str_Command)FieldNames = [f for f in DBCursor .description]for Field in FieldNames:if "test" in Field:bAvailable = Truebreak;else:bAvailable = Falsereturn bAvailable
Comments and DocstringsTo 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
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
""" This is the docstring for the MyFun """
print " here is where we do stuff with the 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 is the script name
param = sys.argv # first param after script name
# the line below calls the main_MyFun and
# puts the result into MyFun_result
MyFun_result = main_MyFun(param)