Computer >> Computer tutorials >  >> Programming >> Python

Important differences between Python 2.x and Python 3.x with examples


In this article, we will learn about the major changes in Python 2.x. & Python 3.x.

  • Input Methodology
  • Output Methodology
  • Division Operator
  • Exception Handling

Input Methodology

Earlier we used to have the raw_input() method but in newer versions, it is replaced by the input() method as shown below

Here we take an integer input in a by using two different syntaxes

# in python 2.x.
a=int(raw_input())

# in python 3.x.
a=int(input())

Output Methodology

Earlier the print statement has not many functionalities available. IN the newer versions two fu\unctionalities namely separator and end value are added to make formatting a bit easier.

# in python 2.x.
print “tutorialspoint”

# in python 3.x.
print (“tutorialspoint”,sep=””,end=”\n”)

The above statements will give the same output. In case we need to have a inline output i.e. without line break we need to add a “,” at the end of the print statement in 2.x. Version and end=” “ in 3.x. Version

Division Operator

Earlier we used have values in the form of integer after division operator is used and in newer versions, this error is rectified. So when division is operated it is returned in the real domain in the newer version.

# in python 2.x.
a=7/4 #1

# in python 3.x.
a=7/4 #1.75

Exception Handling

Earlier we used to have exception handling by specifying the error variable separated by a “,” which is replaced by a keyword in the newer version.

# in python 2.x.
try:
   pass
except NameError, err:
   print err, 'Error Caused'

# in python 3.x.
try:
   pass
except NameError as err:
   print (err, 'Error Caused')

Conclusion

In this article, we learned about Important differences between Python 2.x and Python 3.x with examples.