Python3.x vs Python2.x

Here I am trying to list some of the differences between Python3.x and Python2.x. Only those differences which I came through are listed. I expect more differences as I go deep into Python3.x. Python3.x is a backward incompatible Python release. i.e codes written in Python2.x may or ma not run in Python3.x and vice versa.

  • print

print statement in Python2.x was a function and it remains a function in Python3.x. The difference is in its syntax. In Python2.x it didn’t met with the consistency model other functions follow. Usually functions have a () to enter the parameters. In Python2.x there was no parenthesis. In Python3.x the parenthesis is a part of syntax.

Python2.x                                 Python3.x

  • Arithmetic Division

In Python3.x we get the solution of an arithmetic division as we had expected. As we had expected in the sense, if we divide 1 by 2 we would expect the answer to be 0.5. But in Python2.x we get the answer 0, whereas in Python we get the answer 0.5, the one we expected.

Python2.x           Python3.x

  • range Function

In Python3.x we get a new option for range function. We can extract the first term, last term and rest of the list separately. This option was not available in Python2.x. We had to write separate code to extract these values.

Python3.x

This code section will generate a syntax error in Python2.x.

  • not equal to

‘<>’ symbol used in Python2.x to represent not equal to is no longer available in Python3.x. If we try it it will generate a syntax error. The symbol ‘!=’ is used instead, which was available in Python2.x along with ‘<>’.

Python2.x                                  Python3.x

  • dict Methods

In Python2.x if we call the method dict.keys() or dict.values() or dict.items(), we get a list as the result. But in Python3.x instead of list we get ‘views’. i.e . in Python3.x expressions like k = dict.sort(), k.sort() does not work. We have to use k = sorted(dict) instead.

Python2.x                            Python3.x

  • has_key in dict

has_key method on dictionary is removed in Python3.x, can only use ‘in’ operator.

Python2.x                                               Python3.x

The differences listed above are only some basic differences that I came through in first usage. There are many more differences.

Advertisements

About Odol Shinu

I've completed my B Tech in Information Technology in 2010 from Government Engineering College Sreekrishnapuram Palakkad under Calicut University.

Posted on November 1, 2010, in Python. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: