- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences in between python 2.x and python 3.x versions?
In this article, we look at a few important differences between python 2.x and python 3.x with examples.
- The print function
- Integer division
- Unicode Strings
- Raising Exception
- _future_module
- xrange
Below are key differences between python 2.x and python 3.x
The print function
The print keyword from Python 2.x is substituted with the print() function from Python 3.x in this case. Because the interpreter analyses it as an expression, parentheses work in Python 2 if space is supplied after the print keyword.
Example
This is an example to understand the usage of print function.
print 'Python' print 'Hello, World!' print('Tutorialspoint')
Output
When the above code has complied on both the versions, the outputs are shown
Python 2.x version
Python Hello, World! Tutorialspoint
Python 3.x version
File "main.py", line 1 print 'Python' ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Python')?
The below line of code can be used to print a statement in python.
print('Tutorialspoint')
Output
The outputs obtained in each versions are given below.
Python 2.x version
Tutorialspoint
Python 3.x version
Tutorialspoint
Integer Division
Python 2 interprets numbers without any digits after the decimal point as integers, which might result in some unexpected division results. If you type 5 / 2 in Python 2 code, the result of the evaluation will be 2, not 2.5, as you may expect.
Example
This example depicts the working of integer division in python.
print ('5 / 2 =', 5 / 2) print ('5 // 2 =', 5 // 2) print ('5 / 2.0 =', 5 / 2.0) print ('5 // 2.0 =', 5 // 2.0)
Output
The outputs obtained in both versions are as follows.
In python 2.x version
5 / 2 = 2.5 5 // 2 = 2 5 / 2.0 = 2.5 5 // 2.0 = 2.0
In python 3.x version
5 / 2 = 2.5 5 // 2 = 2 5 / 2.0 = 2.5 5 // 2.0 = 2.0
Unicode
In Python 2, an implicit str type is ASCII. However, the implicit str type in Python 3.x is Unicode.
In Python 2 you have to mark a string with a “u” if you want to store it as Unicode.
In Python 3 you have to mark a string with a “b” if you want to store it as Byte code.
Example 1
The difference in Unicode in python 2 and python 3 can be seen below.
print(type('default string ')) print(type(b'string which is sent with b'))
Output
The outputs obtained for each of the different version are as follows.
In python 2 version
<type 'str'> <type 'str'>
In python 3 version
<class 'str'> <class 'bytes'>
Example 2
Another example for the Unicode in the different versions of python 2 and 3 are as follows.
print(type('default string ')) print(type(u'string which is sent with u'))
Output
The outputs obtained are as follows.
In python 2 version
<type 'str'> <type 'unicode'>
In python 3 version
<class 'str'> <class 'str'>
Raising Exception
Raising exceptions in Python 3 requires a changed syntax. You must use the syntax if you wish to display an error message to the user.
raise IOError(“your error message”)
Above syntax works on python 2 and python 3 both.
However, the following code works only in python 2, not in python 3
raise IOError, “your error message”
_future_module
This isn't really a difference between the two versions, but it's important to note. The __future__ module was created to aid with the migration to Python 3.x.
We can use _future_ imports in our code if we want Python 3.x compatibility in our Python 2.x code.
Example
The following code can be used to import and use the
from __future__ import print_function print('Tutroialspoint')
Output
The output obtained is given below.
Tutorialspoint
- Related Articles
- Differences between Python 2.x and Python 3.x?
- What are the key differences between Python 2.7.x and Python 3.x?
- Important differences between Python 2.x and Python 3.x with examples
- Check whether the following are quadratic equations:(i) ( (x+1)^{2}=2(x-3) )(ii) ( x^{2}-2 x=(-2)(3-x) )(iii) ( (x-2)(x+1)=(x-1)(x+3) )(iv) ( (x-3)(2 x+1)=x(x+5) )(v) ( (2 x-1)(x-3)=(x+5)(x-1) )(vi) ( x^{2}+3 x+1=(x-2)^{2} )(vii) ( (x+2)^{3}=2 xleft(x^{2}-1right) )(viii) ( x^{3}-4 x^{2}-x+1=(x-2)^{3} )
- What are the differences between thunderbolt 2 and thunderbolt 3?
- Show that $(x - 2), (x + 3)$ and $(x - 4)$ are factors of $x^3 - 3x^2 - 10x + 24$.
- If ( 2 x+3 ) and ( x+2 ) are the factors of the polynomial ( g(x)=2 x^{3}+a x^{2}+27 x+b ), find the value of the constants $a$ and $b$.
- Divide the following:$x^3 - 3 x^2 + 5 x - 3$ by $x^2 -2$
- Determine which of the following polynomials has ( (x+1) ) a factor:(i) ( x^{3}+x^{2}+x+1 )(ii) ( x^{4}+x^{3}+x^{2}+x+1 )(iii) ( x^{4}+3 x^{3}+3 x^{2}+x+1 )(iv) ( x^{3}-x^{2}-(2+sqrt{2}) x+sqrt{2} )
- divide the polynomial $p( x)$ by the polynomial $g( x)$ and find the quotient and remainder in each of the following: $( p(x)=x^{3}-3 x^{2}+5 x-3$, $g(x)=x^{2}-2$.
- ( (2 x-3)(x-5)=(x-3)^{2} )
- Check whether the following are quadratic equations:$x^3 -4x^2 -x + 1 = (x-2)^3$
- $( x-2)$ is a common factor of $x^{3}-4 x^{2}+a x+b$ and $x^{3}-a x^{2}+b x+8$, then the values of $a$ and $b$ are respectively.
- Simplify the following :$( 3 x^2 + 5 x - 7 ) (x-1) - ( x^2 - 2 x + 3 ) (x + 4)$
- For which values of ( a ) and ( b ), are the zeroes of ( q(x)=x^{3}+2 x^{2}+a ) also the zeroes of the polynomial ( p(x)=x^{5}-x^{4}-4 x^{3}+3 x^{2}+3 x+b ) ? Which zeroes of ( p(x) ) are not the zeroes of ( q(x) ) ?
