Wednesday, August 31, 2016

Major difference between Python 2 and Python 3



1. Print is now became function

The print statement has been replaced with a print() function. Example :


Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!

2.Unicode and Strings


In Python 2, you had to mark every single Unicode string with a u at the beginning, like u'Hello'. So, with Python 3, they fixed it. All strings are now Unicode by default and you have to mark byte sequences with b. Using Unicode is a much more common scenario so it has reduced development time for everyone that does Python 3.

If you have or want to support both, you can still mark strings with u in Python 3, though.

3. Division With Integers


One of Python’s core values is to never do anything implicitly. You shouldn’t turn a number into a string unless the programmer tells you to, for example. But Python 2 took this a bit too far. Consider this problem:

5 / 2 

For most of us, our immediate answer is 2.5, which is, of course, the right answer. But Python 2 said "oh, you only gave me integers so you must want an integer back" and happily returned 2. Well, yes, I did give you integers, but I’d rather have a correct answer than an answer that matches my data types.

Again, Python 3 fixed this. Python 3 will give 2.5 as the answer to that question. In fact, it gives a float (a number with a decimal in it) to every division operation. But, if you are expecting an integer (round value), then probably you can use double division operator (//) , which will return integer value:

5//2  will print 2


4. input() is Now Safe to Use


In Python 2, there was a raw_input() function and an input() function. raw_input() was the one you always wanted to use. input() was a great way to have your code do things you didn’t want it to. The reason for this is that input() evaluated whatever came in. So good users would send in 123 and Python, trying to be helpful, would make that into an integer instead of a string. Bad users would send in little, or not so little, bits of Python which would then be evaluated, or run. Cue your software doing things you didn’t ask it to do.

In Python 3, raw_input() is replaced with input(), which no longer evaluates the data it receives. You always get back a string.

5. Performance


The net result of the 3.0 generalizations is that Python 3.0 runs the pystone benchmark around 10% slower than Python 2.5. Most likely the biggest cause is the removal of special-casing for small integers. There’s room for improvement, but it will happen after 3.0 is released!


No comments:

Post a Comment