Python2 vs. Python3


Guido van Rossum created Python in 1991, he decided to clean up Python 2.x properly, without considering any newer releases to it as well as backward compatibility. The whole Python community was pretty much skeptical when they received Python 3.x. Most of the things written in Python 2.x were not compatible with Python 3.x, as it did not support backward compatibility. Many applications and frameworks needed to rewritten completely due to this, hence, it was very difficult to port to Python3 from Python2. Why was it that difficult? What are the differences?

Let us see some of the key differences between Python 2 and Python 3.


In Python2, print is treated as a statement rather than a function. You don’t need wrap the text in parentheses. But, it was very confusing, as most the actions or work that you do with Python requires arguments to be placed inside the parentheses .In Python3, “print” works like a function, where you wrap the text inside the parentheses. If you don’t use parentheses, there will be a syntax error. Though, many Python2 programmers find this change to be irritating, but it really helps in preventing mistakes as some time if you put parentheses around a comma-separated lists then you would get unexpected outcomes. You can see this difference of print function in the example.

In Python2,

>>> print "Hello World!"
Hello World

In Python3,

>>> print("Hello World!")
Hello World!

In Python3, syntax error, when executed without parentheses,

>>> print "Hello World!"
SyntaxError: Missing parentheses in call to 'print'

Integer Division

In Python2, whenever you type any number without any digit after decimal, it treats that number as integer. This might lead to some unexpected  results during division. We will see it with an example.


>>> 3/2

Here, when we made the division of 3 by 2, we got 1, this is because Python2, assumes that as the programmer entered integer values, the output should be also integer and hence it rounds off the output to the nearest value. If you want, the output as 1.5 then you will have to entered the input in float as shown here.

>>> 3.0/2.0

Thus, in Python2, if you want the output in float then you need to have the input also in float.

Below is the output from Python3, you can see that even though the input is in integer, you get the output in float which very much intuitive for new programmers.

>>> 3/2

List Comprehension-Loop Variables

In Python2, when a variable that is being iterated over a for-loop and a global variable, both are having same name, the value of the global variable changes whenever there is an iteration, this is highly undesirable. I have shown it here in the example that follows.


>>> i=1
>>> print "before i=",i
before i= 1
>>> print "Jekishan", [i for i in range(5)]
Jekishan [0, 1, 2, 3, 4]
>>> print "after i=",i
after i= 4


>>> i=1
>>> print ("before i=",i)
before i= 1
>>> print ("Jekishan",[i for i in range(5)])
Jekishan [0, 1, 2, 3, 4]
>>> print("after i=",i)
after i= 1

Hence, in Python 3.x for-loop variables don’t leak into the global namespace anymore!

Unicode Strings

In Python2, if you want to store a string as Unicode, you need to mark it with “u”, whereas in Python3, all the strings are stored in Unicode by default. Unicode strings are much more versatile compared to ASCII, which is the default standard for Python2. You can store emojis, letters from foreign languages, roman letters as well as numerals in Unicode.

Raising Exceptions

Syntax for raising an error in Python2 and Python3, are different.

In Python2, to raise an IO Error, you would write

>>>raise IOError, "This is an IO Error"

In Python3, to raise an IO Error, you will write

>>>raise IOError ("This is an IO Error")

This example, again reminds you that in Python3, you need to pass arguments in the parentheses, which creates less confusion.

Apart from this, there are many other examples of slight differences, in the syntax of Python2 and Python3. Python3.5 works approximately at the same speed as 2.7, though there are some benchmarks which suggests that Python3 is faster compared to Python2.


In next post, I will write about which version of Python to use where?


Python and Some Other Programming Languages

proglangcompWhenever we see any new programming language, we tend to compare it with others, and as of now Python is one of the most upcoming and used programming language for various applications like IoT, Data Science, etc.

Let us see how python stands with other languages.


Python programs are much slower compared to JAVA, but they take much less time to develop and are nearly 3-5 times shorter than JAVA codes. This is due to Python’s built-in high level datatypes and dynamic typing. For example, a JAVA programmer will waste too much time in declaring variables and assigning datatypes to it, while in Python you don’t require to do anything like this as it has powerful polymorphic list and dictionary types, for which rich syntactic support is built straight into the language. Thus, there is run-time typing in Python and so Python’s runtime must work harder than JAVA’s. For example, when evaluating the expression a+b, it must first inspect the objects a and b to find out their type, which is not known at compile time. It then invokes the appropriate addition operation, which may be an overloaded user-defined method. JAVA, on the other hand, can perform an efficient integer or floating point addition, but requires variable declarations for a and b, and does not allow overloading of the + operator for instances of user-defined classes.

For these reasons, Python is much better suited as a “glue” language, while JAVA is better characterized as a low-level implementation language. A Python implementation written in JAVA is under development, which allows calling Python code from JAVA and vice versa. Combination of both would give excellent performance. Components can be written in JAVA and combined to form applications in Python.


Almost everything said for JAVA, also applies to C++, in fact where Python code is 3-5 times shorter than JAVA code, it is 5-10 shorter than C++ code. What Python programmer can do in two months, can’t be done by a C++ programmer even in a year.


One major thing is that C#.NET is proprietary while Python is open source. Hence, there are going to be lots and lots of open source libraries available for Python compared to C#.NET. It is also easy to learn and useful for cross-platform development.  But, when you think about development process and tools, language features as well as standard libraries, C#.NET would be a winner. Moreover, Python is more readable due to its very simple syntax, while C# is more easier to adopt due to its consistency in syntax. At the same time you need to follow various rules for syntax while coding in C#.NET.

Python is an interpreted language while C#.NET is a compiled language. Due to CLI, C#.NET is much faster compared to Python. With PyPy’s implementation of the JIT compiler, Python’s program execution is improved. While C# wins the performance race, writing and deploying code in C# is slower than Python. Python is well-known for its easy learning curve and rapid development.


While implementation of Python in JAVA is under development, we’ve already got IronPython which is an implementation of Python in C#, so that Python developers can get the power of .NET Framework and existing .NET developers can also use IronPython as a fast and expressive scripting language for embedding, testing, or writing a new application from scratch.

Moreover, IronPython is very tightly integrated with .NET Framework such that it can use .NET Framework and Python libraries and at the same time other .NET languages can also use Python code and its libraries very easily.


Python itself has also two versions, Python2 and Python3, in next post I will discuss about it.





Attaching an SQL Server Database without LOG file

Sometimes due to some hassle or if you are in hurry, you may download a database (.mdf file) directly from somewhere. But, when you try and attach this database in your SQL Server Management Studio you are not able to attach it. This is due to absence of LOG (.ldf) file. But there is a possible workaround for this. You can create or generate a LOG file by executing following query in SQL Server. Here it is:

CREATE DATABASE AdventureWorks2012

ON (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12.JEKISHANMSSQL\MSSQL\DATA\AdventureWorksDW2012_Data.mdf')

Here, I have written the query to recreate a LOG file for AdventureWorksDW2012_Data.mdf file that I had downloaded from codeplex.