When a new version of a programming language is released, it usually supports the features and syntax of the previous version, making it easier for projects to move to the newer version. However, when it comes to Python, the two versions, Python 2 and Python 3, are vastly different.
A list of differences between Python 2 and Python 3 are given below:
- The print statement in Python 2 is used like print "something" to output a string on the terminal. Print is a function in Python 3, and it is used as print("something") to print something on the console.
- Raw input() is a function in Python 2 that accepts human input. It returns a string that represents the value entered by the user. The int() method in Python is used to convert it to an integer. Python 3 on the other hand, employs the input() function, which automatically interprets the user's input. Using primitive operations (int(), str(), etc.), we can cast this value to any type.
- The implicit string type in Python 2 is ASCII, whereas the implicit string type in Python 3 is Unicode.
- The xrange() method from Python 2 is not available in Python 3. The xrange() function is a variation of range() that returns an xrange object that functions similarly to a Java iterator. The range() function, for example, returns a list with the values 0, 1, and 2.
- In Python 3, there is also a minor change in the way exceptions are handled. It defines a keyword as one that must be utilised. We'll go over it in the Python programming tutorial's exception handling section.
Learn Python Training today to improve your skills and expand your work options.