Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elaborated the differences and added a link for more information. #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Important differences between Python 2.x and Python 3.x with examples:-


1. Division operator

If we are porting our code or executing the python 3.x code in python 2.x, it can be dangerous if integer division changes go unnoticed (since it doesn�t raise any error). It is preferred to use the floating value (like 7.0/5 or 7/5.0) to get the expected result when porting our code.

print 7 / 5
print -7 / 5

'''
Output in Python 2.x:
1
-2
Output in Python 3.x :
1.4
-1.4
'''


2. print function

This is the most well known change. In this the print function in Python 2.x is replaced by print() function in Python 3.x, i.e, to print in Python 3.x an extra pair of parenthesis is required.

print 'Hello, World!' # Python 3.x doesn't support
print('Hope You like these facts')

'''
Output in Python 2.x :
Hello, Geeks
Hope You like these facts

Output in Python 3.x :
File "a.py", line 1
print 'Hello, Geeks'
^
SyntaxError: invalid syntax

'''


3. xrange

xrange() of Python 2.x doesn�t exist in Python 3.x. In Python 2.x, range returns a list i.e. range(3) returns [0, 1, 2] while xrange returns a xrange object i. e., xrange(3) returns iterator object which work similar to Java iterator and generates number when needed.
If we need to iterate over the same sequence multiple times, we prefer range() as range provides a static list. xrange() reconstructs the sequence every time. xrange() doesn�t support slices and other list methods. The advantage of xrange() is, it saves memory when task is to iterate over a large range.

In Python 3.x, the range function now does what xrange does in Python 2.x, so to keep our code portable, we might want to stick to using range instead. So Python 3.x�s range function is xrange from Python 2.x.

for x in xrange(1, 5):
print(x),

for x in range(1, 5):
print(x),

'''
Output in Python 2.x
1 2 3 4 1 2 3 4

Output in Python 3.x
NameError: name 'xrange' is not defined
'''

4. Unicode

In Python 2, implicit str type is ASCII. But in Python 3.x implicit str type is Unicode.

print(type('default string '))
print(type(b'string with b '))

'''
Output in Python 2.x (Bytes is same as str)
<type 'str'>
<type 'str'>

Output in Python 3.x (Bytes and str are different)
<class 'str'>
<class 'bytes'>
'''
Python 2.x also supports Unicode

print(type('default string '))
print(type(u'string with b '))

'''
Output in Python 2.x (Unicode and str are different)
<type 'str'>
<type 'unicode'>

Output in Python 3.x (Unicode and str are same)
<class 'str'>
<class 'str'>
'''

For more information visit python wiki:-
https://wiki.python.org/moin/Python2orPython3