- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differences between Python 2.x and Python 3.x?
There is always a debate in the coding community on which python version was the best one to learn: Python 2.x or Python 3.x.
Below are key differences between pyton 2.x and python 3.x
1. The print function
In python 2.x, “print” is treated as a statement and python 3.x explicitly treats “print” as a function. This means we need to pass the items inside your print to the function parentheses in the standard way otherwise you will get a syntax error.
#Python 2.7 print 'Python', python_version() print 'Hello, World!' print('Hello, World!') print "text", ; print 'some more text here'
Python 2.7.6 Hello, World! Hello, World! text print some more text here Python 3 import sys print("Python version is %s.%s.%s" %sys.version_info[:3]) print('Hello, World!') print("some text,", end="") print('some more text here')
Python version is 3.6.1 Hello, World! some text,some more text here >>> print "Hello" Syntax Error: Missing parentheses in call to 'print'
2. Integer division
Python 2 treats numbers that you type without any digits after the decimal point as integers, which can lead to some unexpected results during division. For example, if you type the expression 3 / 2 in Python 2 code, the result of the evaluation will be 1, not 1.5 as you might expect. It is recommended to use either float(x) instead of using only x in your python 3 code (incase codebase port to python 2) or use from __future__ import division in your python 2 scripts.
print 'Python', python_version() print '3 / 2 =', 3 / 2 print '3 // 2 =', 3 // 2 print '3 / 2.0 =', 3 / 2.0 print '3 // 2.0 =', 3 // 2.0
Python 2.7.6 3 / 2 = 1 3 // 2 = 1 3 / 2.0 = 1.5 3 // 2.0 = 1.0
import sys print('Python %s.%s.%s' %sys.version_info[:3]) print('3 / 2 =', 3 / 2) print('3 // 2 =', 3 // 2) print('3 / 2.0 =', 3 / 2.0) print('3 // 2.0 =', 3 // 2.0)
Python 3.6.1 3 / 2 = 1.5 3 // 2 = 1 3 / 2.0 = 1.5 3 // 2.0 = 1.0
3. Unicode Strings
By default, Python 3 stores strings as Unicode whereas Python 2 requires you to mark a string with a “u” if you want to store it as Unicode. Unicode strings are more versatile than ASCII strings, which are the Python 2 default, as they can store letters from foreign languages as well as emoji and the standard Roman letters and numerals.
>>> print type(unicode('this is like a python3 str type')) <type 'unicode'> >>> print type(b'byte type does not exist') <type 'str'> >>> print 'they are really' + b' the same' they are really the same
import sys print('Python %s.%s.%s' %sys.version_info[:3]) print('strings are now utf-8 \u03BCnico\u0394é!') print('Python %s.%s.%s' %sys.version_info[:3], end="") print(' has', type(b' bytes for storing data')) print('Python %s.%s.%s' %sys.version_info[:3], end="") print(' also has', type(bytearray(b'bytearrays')))
Python 3.6.1 strings are now utf-8 μnicoΔé! Python 3.6.1 has <class 'bytes'> Python 3.6.1 also has <class 'bytearray'>
“string” + b”bytes of data” will through an error.
>>> print ('they are really' + b' the same') Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> print ('they are really' + b' the same') TypeError: must be str, not bytes
4. Raising Exception
Python 3 requires different syntax for raising exceptions. If you want to output an error message to the user, you need to use the syntax −
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”
5. List Comprehension Loop Variables
In python 2, giving the variable that is iterated over in a “for loop” the same name as a global variable may leads to the value of the global variable being changed – which generally we don’t want. This issue has been fixed in Python 3, so you can use a variable name you already used for the control variable in your “for loop” without worrying about it leaking out and messing with the values of the variables in the rest of your code.
#Python 2 print 'Python', python_version() i = 1 print 'before: i =', i print 'comprehension: ', [i for i in range(5)] print 'after: i =', i
Python 2.7.6 before: i = 1 comprehension: [0, 1, 2, 3, 4] after: i = 4
import sys print('Python %s.%s.%s' %sys.version_info[:3]) i = 1 print('before: i =', i) print('comprehension:', [i for i in range(5)]) print('after: i =', i)
Python 3.6.1 before: i = 1 comprehension: [0, 1, 2, 3, 4] after: i = 1
- Important differences between Python 2.x and Python 3.x with examples
- What are the differences in between python 2.x and python 3.x versions?
- What are the key differences between Python 2.7.x and Python 3.x?
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Difference between x++ and x = x+1 in Java
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
- Difference between x++ and x= x+1 in Java programming
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- What are the differences between thunderbolt 2 and thunderbolt 3?
- Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x,x*x).
- Count Elements x and x+1 Present in List in Python
- Sqrt(x) in Python
- Evaluate a 2-D polynomial at points (x, y) in Python
- Difference between NumberLong(x) and NumberLong(“x”) in MongoDB?